Skip to content
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
29 changes: 29 additions & 0 deletions type_specific.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ func (v *Value) IsMSISlice() bool {
_, ok := v.data.([]map[string]interface{})
if !ok {
_, ok = v.data.([]Map)
if !ok {
s, ok := v.data.([]interface{})
if ok {
for i := range s {
switch s[i].(type) {
case Map:
case map[string]interface{}:
default:
return false
}
}
return true
}
}
}
return ok
}
Expand Down Expand Up @@ -241,7 +255,22 @@ func (v *Value) IsObjxMapSlice() bool {
_, ok := v.data.([](Map))
if !ok {
_, ok = v.data.([]map[string]interface{})
if !ok {
s, ok := v.data.([]interface{})
if ok {
for i := range s {
switch s[i].(type) {
case Map:
case map[string]interface{}:
default:
return false
}
}
return true
}
}
}

return ok
}

Expand Down
31 changes: 31 additions & 0 deletions type_specific_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func TestMSISlice(t *testing.T) {
assert.Panics(t, func() {
m.Get("nothing").MustMSISlice()
})

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, 1, len(o.Get("d").MSISlice()))

i := objx.MustFromJSON(`{"d":[{"author":"abc"},[1]]}`)
assert.Nil(t, i.Get("d").MSISlice())
}

func TestIsMSI(t *testing.T) {
Expand All @@ -62,6 +69,14 @@ func TestIsMSISlice(t *testing.T) {

assert.True(t, m.Get("data").IsMSISlice())
assert.True(t, m.Get("data2").IsMSISlice())

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.True(t, o.Has("d"))
assert.True(t, o.Get("d").IsMSISlice())

o = objx.MustFromJSON(`{"d":[{"author":"abc"},[1]]}`)
assert.True(t, o.Has("d"))
assert.False(t, o.Get("d").IsMSISlice())
}

func TestEachMSI(t *testing.T) {
Expand Down Expand Up @@ -259,6 +274,13 @@ func TestObjxMapSlice(t *testing.T) {
assert.Panics(t, func() {
m.Get("nothing").MustObjxMapSlice()
})

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").MustObjxMapSlice()[0].Get("id").Int())
assert.Equal(t, 1, len(o.Get("d").ObjxMapSlice()))

i := objx.MustFromJSON(`{"d":[{"author":"abc"},[1]]}`)
assert.Nil(t, i.Get("d").ObjxMapSlice())
}

func TestIsObjxMap(t *testing.T) {
Expand All @@ -273,6 +295,15 @@ func TestIsObjxMapSlice(t *testing.T) {

assert.True(t, m.Get("data").IsObjxMapSlice())
assert.True(t, m.Get("data2").IsObjxMapSlice())

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.True(t, o.Has("d"))
assert.True(t, o.Get("d").IsObjxMapSlice())

//Valid json but not MSI slice
o = objx.MustFromJSON(`{"d":[{"author":"abc"},[1]]}`)
assert.True(t, o.Has("d"))
assert.False(t, o.Get("d").IsObjxMapSlice())
}

func TestEachObjxMap(t *testing.T) {
Expand Down