Skip to content

Commit 4ff3852

Browse files
authored
fix int to float conversions (#108)
1 parent 0ea374a commit 4ff3852

File tree

4 files changed

+14
-45
lines changed

4 files changed

+14
-45
lines changed

map.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ func FromJSON(jsonString string) (Map, error) {
114114
if err != nil {
115115
return Nil, err
116116
}
117-
m.tryConvertFloat64()
118117
return m, nil
119118
}
120119

@@ -128,49 +127,9 @@ func FromJSONSlice(jsonString string) ([]Map, error) {
128127
if err != nil {
129128
return nil, err
130129
}
131-
for _, m := range slice {
132-
m.tryConvertFloat64()
133-
}
134130
return slice, nil
135131
}
136132

137-
func (m Map) tryConvertFloat64() {
138-
for k, v := range m {
139-
switch v.(type) {
140-
case float64:
141-
f := v.(float64)
142-
if float64(int(f)) == f {
143-
m[k] = int(f)
144-
}
145-
case map[string]interface{}:
146-
t := New(v)
147-
t.tryConvertFloat64()
148-
m[k] = t
149-
case []interface{}:
150-
m[k] = tryConvertFloat64InSlice(v.([]interface{}))
151-
}
152-
}
153-
}
154-
155-
func tryConvertFloat64InSlice(s []interface{}) []interface{} {
156-
for k, v := range s {
157-
switch v.(type) {
158-
case float64:
159-
f := v.(float64)
160-
if float64(int(f)) == f {
161-
s[k] = int(f)
162-
}
163-
case map[string]interface{}:
164-
t := New(v)
165-
t.tryConvertFloat64()
166-
s[k] = t
167-
case []interface{}:
168-
s[k] = tryConvertFloat64InSlice(v.([]interface{}))
169-
}
170-
}
171-
return s
172-
}
173-
174133
// FromBase64 creates a new Obj containing the data specified
175134
// in the Base64 string.
176135
//

map_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ func TestConversionJSONInt(t *testing.T) {
110110
assert.Equal(t, 1, m.Get("b.data").Int())
111111

112112
assert.True(t, m.Get("c").IsInterSlice())
113-
assert.Equal(t, 1, m.Get("c").InterSlice()[0])
113+
assert.Equal(t, float64(1), m.Get("c").InterSlice()[0])
114114

115115
assert.True(t, m.Get("d").IsInterSlice())
116-
assert.Equal(t, []interface{}{1}, m.Get("d").InterSlice()[0])
116+
assert.Equal(t, []interface{}{float64(1)}, m.Get("d").InterSlice()[0])
117117
}
118118

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

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

134134
func TestJSONSliceMixed(t *testing.T) {

type_specific_codegen.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ func (v *Value) Int(optionalDefault ...int) int {
385385
if s, ok := v.data.(int); ok {
386386
return s
387387
}
388+
if s, ok := v.data.(float64); ok {
389+
if float64(int(s)) == s {
390+
return int(s)
391+
}
392+
}
388393
if len(optionalDefault) == 1 {
389394
return optionalDefault[0]
390395
}
@@ -395,6 +400,11 @@ func (v *Value) Int(optionalDefault ...int) int {
395400
//
396401
// Panics if the object is not a int.
397402
func (v *Value) MustInt() int {
403+
if s, ok := v.data.(float64); ok {
404+
if float64(int(s)) == s {
405+
return int(s)
406+
}
407+
}
398408
return v.data.(int)
399409
}
400410

type_specific_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestMSISlice(t *testing.T) {
4949
})
5050

5151
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}`)
52-
assert.Equal(t, 9879, o.Get("d").MustMSISlice()[0]["id"])
52+
assert.Equal(t, float64(9879), o.Get("d").MustMSISlice()[0]["id"])
5353
assert.Equal(t, 1, len(o.Get("d").MSISlice()))
5454

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

0 commit comments

Comments
 (0)