Skip to content

Commit

Permalink
issue 77 - support parsing of int (#78)
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <[email protected]>
  • Loading branch information
Kavindu-Dodan authored Apr 3, 2024
1 parent 601b008 commit ea950da
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 7 deletions.
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))
})
}
}

0 comments on commit ea950da

Please sign in to comment.