Skip to content

Commit

Permalink
fix int to float conversions (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
geseq authored Jul 16, 2021
1 parent 0ea374a commit 4ff3852
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 45 deletions.
41 changes: 0 additions & 41 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func FromJSON(jsonString string) (Map, error) {
if err != nil {
return Nil, err
}
m.tryConvertFloat64()
return m, nil
}

Expand All @@ -128,49 +127,9 @@ func FromJSONSlice(jsonString string) ([]Map, error) {
if err != nil {
return nil, err
}
for _, m := range slice {
m.tryConvertFloat64()
}
return slice, nil
}

func (m Map) tryConvertFloat64() {
for k, v := range m {
switch v.(type) {
case float64:
f := v.(float64)
if float64(int(f)) == f {
m[k] = int(f)
}
case map[string]interface{}:
t := New(v)
t.tryConvertFloat64()
m[k] = t
case []interface{}:
m[k] = tryConvertFloat64InSlice(v.([]interface{}))
}
}
}

func tryConvertFloat64InSlice(s []interface{}) []interface{} {
for k, v := range s {
switch v.(type) {
case float64:
f := v.(float64)
if float64(int(f)) == f {
s[k] = int(f)
}
case map[string]interface{}:
t := New(v)
t.tryConvertFloat64()
s[k] = t
case []interface{}:
s[k] = tryConvertFloat64InSlice(v.([]interface{}))
}
}
return s
}

// FromBase64 creates a new Obj containing the data specified
// in the Base64 string.
//
Expand Down
6 changes: 3 additions & 3 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func TestConversionJSONInt(t *testing.T) {
assert.Equal(t, 1, m.Get("b.data").Int())

assert.True(t, m.Get("c").IsInterSlice())
assert.Equal(t, 1, m.Get("c").InterSlice()[0])
assert.Equal(t, float64(1), m.Get("c").InterSlice()[0])

assert.True(t, m.Get("d").IsInterSlice())
assert.Equal(t, []interface{}{1}, m.Get("d").InterSlice()[0])
assert.Equal(t, []interface{}{float64(1)}, m.Get("d").InterSlice()[0])
}

func TestJSONSliceInt(t *testing.T) {
Expand All @@ -128,7 +128,7 @@ func TestJSONSliceInt(t *testing.T) {

assert.Nil(t, err)
require.NotNil(t, m)
assert.Equal(t, []objx.Map{{"b": 1}, {"c": 2}}, m.Get("a").ObjxMapSlice())
assert.Equal(t, []objx.Map{{"b": float64(1)}, {"c": float64(2)}}, m.Get("a").ObjxMapSlice())
}

func TestJSONSliceMixed(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions type_specific_codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ func (v *Value) Int(optionalDefault ...int) int {
if s, ok := v.data.(int); ok {
return s
}
if s, ok := v.data.(float64); ok {
if float64(int(s)) == s {
return int(s)
}
}
if len(optionalDefault) == 1 {
return optionalDefault[0]
}
Expand All @@ -395,6 +400,11 @@ func (v *Value) Int(optionalDefault ...int) int {
//
// Panics if the object is not a int.
func (v *Value) MustInt() int {
if s, ok := v.data.(float64); ok {
if float64(int(s)) == s {
return int(s)
}
}
return v.data.(int)
}

Expand Down
2 changes: 1 addition & 1 deletion type_specific_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMSISlice(t *testing.T) {
})

o := objx.MustFromJSON(`{"d":[{"author":{"displayName":"DemoUser3","id":2},"classes":null,"id":9879,"v":{"code":"","created":"2013-09-19T09:38:50+02:00","published":"0001-01-01T00:00:00Z","updated":"2013-09-19T09:38:50+02:00"}}],"s":200}`)
assert.Equal(t, 9879, o.Get("d").MustMSISlice()[0]["id"])
assert.Equal(t, float64(9879), o.Get("d").MustMSISlice()[0]["id"])
assert.Equal(t, 1, len(o.Get("d").MSISlice()))

i := objx.MustFromJSON(`{"d":[{"author":"abc"},[1]]}`)
Expand Down

0 comments on commit 4ff3852

Please sign in to comment.