Skip to content

Commit

Permalink
Fix bug introduced with #17
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei committed Jan 29, 2018
1 parent e89b2c1 commit 470269f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 15 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ func TestConversionJSONInt(t *testing.T) {
assert.Equal(t, []interface{}{1}, m.Get("d").InterSlice()[0])
}

func TestJSONSlice(t *testing.T) {
jsonString :=
`{
"a": [
{"b": 1},
{"c": 2}
]
}`
m, err := objx.FromJSON(jsonString)

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

func TestMapFromBase64String(t *testing.T) {
base64String := "eyJuYW1lIjoiTWF0In0="
o, err := objx.FromBase64(base64String)
Expand Down
22 changes: 17 additions & 5 deletions type_specific_codegen.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package objx

import "log"

/*
Inter (interface{} and []interface{})
*/
Expand Down Expand Up @@ -276,20 +278,30 @@ func (v *Value) MustObjxMap() Map {
// ObjxMapSlice gets the value as a [](Map), returns the optionalDefault
// value or nil if the value is not a [](Map).
func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
slice, ok := v.data.([]interface{})
if s, ok := v.data.([]Map); ok {
return s
}
s, ok := v.data.([]interface{})
if !ok {
if len(optionalDefault) == 1 {
log.Println("Return default")
return optionalDefault[0]
} else {
return nil
}
}

result := make([]Map, len(slice))
for i := range slice {
result[i] = New(slice[i])
result := make([]Map, len(s))
for i := range s {
switch s[i].(type) {
case Map:
result[i] = s[i].(Map)
log.Printf("s[i]= %v\n", s[i])
log.Printf("result[i]= %v\n", result[i])
default:
return nil
}
}

return result
}

Expand Down

0 comments on commit 470269f

Please sign in to comment.