Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support parsing of int #78

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ func isString(obj interface{}) bool {
}

func isNumber(obj interface{}) bool {
return is(obj, reflect.Float64)
switch obj.(type) {
case int, float64:
return true
default:
return false
}
}

func isPrimitive(obj interface{}) bool {
Expand Down Expand Up @@ -68,12 +73,22 @@ func toNumber(value interface{}) float64 {
return w
}

return value.(float64)
switch value.(type) {
case int:
return float64(value.(int))
default:
return value.(float64)
}
}

func toString(value interface{}) string {
if isNumber(value) {
return strconv.FormatFloat(value.(float64), 'f', -1, 64)
switch value.(type) {
case int:
return strconv.FormatInt(int64(value.(int)), 10)
default:
return strconv.FormatFloat(value.(float64), 'f', -1, 64)
}
}

if value == nil {
Expand Down
119 changes: 115 additions & 4 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,124 @@ func TestHelperIsTrue(t *testing.T) {
assert.False(t, isTrue(nil))
}

func TestToString(t *testing.T) {
assert.Equal(t, "", toString(nil))
}

func TestToSliceOfNumbers(t *testing.T) {
json_parsed := []interface{}{"0.0", "-10.0"}
input := interface{}(json_parsed)
expected := []float64{0.0, -10.0}
assert.Equal(t, expected, toSliceOfNumbers(input))
}

func TestIsNumber(t *testing.T) {
tests := []struct {
name string
input interface{}
expect bool
}{
{
name: "int max",
input: 9223372036854775807,
expect: true,
},
{
name: "int min",
input: -9223372036854775808,
expect: true,
},
{
name: "float",
input: 1.121,
expect: true,
},
{
name: "string",
input: "123",
expect: false,
},
{
name: "boolean",
input: true,
expect: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expect, isNumber(test.input))
})
}
}

func TestToNumber(t *testing.T) {
tests := []struct {
name string
input interface{}
expect float64
}{
{
name: "int max value",
input: 9223372036854775807,
expect: 9223372036854775807,
},
{
name: "int min value",
input: -9223372036854775808,
expect: -9223372036854775808,
},
{
name: "int min value",
input: -9223372036854775808,
expect: -9223372036854775808,
},
{
name: "int as a string",
input: "123",
expect: 123,
},
{
name: "float",
input: 1.121,
expect: 1.121,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expect, toNumber(test.input))
})
}
}

func TestToString(t *testing.T) {
var tests = []struct {
name string
input interface{}
expect string
}{
{
name: "nil value",
input: nil,
expect: "",
},
{
name: "string value",
input: "value",
expect: "value",
},
{
name: "int value",
input: 9223372036854775807,
expect: "9223372036854775807",
},
{
name: "float value",
input: 1.21,
expect: "1.21",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expect, toString(test.input))
})
}
}
Loading