Skip to content

Commit 16b3351

Browse files
author
childish-sambino
authored
feat: add unmarshaling helper for float32 (#158)
1 parent a4ed12f commit 16b3351

File tree

29 files changed

+1325
-0
lines changed

29 files changed

+1325
-0
lines changed

client/unmarshal.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
func UnmarshalFloat32(input *interface{}) (*float32, error) {
9+
if input == nil {
10+
return nil, nil
11+
}
12+
13+
switch value := (*input).(type) {
14+
case float32:
15+
return &value, nil
16+
case float64:
17+
value32 := float32(value)
18+
return &value32, nil
19+
case int:
20+
value32 := float32(value)
21+
return &value32, nil
22+
case string:
23+
parsed, err := strconv.ParseFloat(value, 32)
24+
if err != nil {
25+
return nil, err
26+
}
27+
value32 := float32(parsed)
28+
return &value32, nil
29+
default:
30+
return nil, fmt.Errorf("unhandled input type for float32: %T %#v", value, value)
31+
}
32+
}

client/unmarshal_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package client_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/twilio/twilio-go/client"
8+
)
9+
10+
func TestUnmarshalFloat32(t *testing.T) {
11+
unmarshalFloat32 := func(t *testing.T, input interface{}) float32 {
12+
value, err := client.UnmarshalFloat32(&input)
13+
assert.Nil(t, err)
14+
return *value
15+
}
16+
17+
assert.Equal(t, float32(0), unmarshalFloat32(t, "0"))
18+
assert.Equal(t, float32(1), unmarshalFloat32(t, "1"))
19+
assert.Equal(t, float32(123), unmarshalFloat32(t, 123))
20+
assert.Equal(t, float32(123.456), unmarshalFloat32(t, "123.456"))
21+
assert.Equal(t, float32(123.456), unmarshalFloat32(t, 123.456))
22+
assert.Equal(t, float32(123.456), unmarshalFloat32(t, float32(123.456)))
23+
assert.Equal(t, float32(7), unmarshalFloat32(t, float64(7)))
24+
}
25+
26+
func TestUnmarshalFloat32_Nil(t *testing.T) {
27+
value, err := client.UnmarshalFloat32(nil)
28+
assert.Nil(t, value)
29+
assert.Nil(t, err)
30+
}
31+
32+
func TestUnmarshalFloat32_ErrorInvalid(t *testing.T) {
33+
var input interface{} = map[string]interface{}{}
34+
value, err := client.UnmarshalFloat32(&input)
35+
assert.Nil(t, value)
36+
assert.NotNil(t, err)
37+
}
38+
39+
func TestUnmarshalFloat32_ErrorParse(t *testing.T) {
40+
var input interface{} = "ugh"
41+
value, err := client.UnmarshalFloat32(&input)
42+
assert.Nil(t, value)
43+
assert.NotNil(t, err)
44+
}

rest/api/v2010/model_api_v2010_call_feedback_summary.go

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest/api/v2010/model_api_v2010_call_recording.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest/api/v2010/model_api_v2010_recording_transcription.go

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest/api/v2010/model_api_v2010_transcription.go

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)