Skip to content

Commit

Permalink
New tests for type_specific.go and fixed code (#75)
Browse files Browse the repository at this point in the history
* Added new tests for type_specific.go and fixed the code to work for both MSI and Map
  • Loading branch information
geseq authored and hanzei committed May 31, 2018
1 parent f031c85 commit 0ab728f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
25 changes: 9 additions & 16 deletions type_specific.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (v *Value) MSISlice(optionalDefault ...[]map[string]interface{}) []map[stri
return s
}

s, ok := v.data.([]Map)
if !ok {
s := v.ObjxMapSlice()
if s == nil {
if len(optionalDefault) == 1 {
return optionalDefault[0]
}
Expand All @@ -55,16 +55,11 @@ func (v *Value) MSISlice(optionalDefault ...[]map[string]interface{}) []map[stri
//
// Panics if the object is not a []map[string]interface{}.
func (v *Value) MustMSISlice() []map[string]interface{} {
s, ok := v.data.([]Map)
if !ok {
return v.data.([]map[string]interface{})
if s := v.MSISlice(); s != nil {
return s
}

result := make([]map[string]interface{}, len(s))
for i := range s {
result[i] = s[i].Value().MustMSI()
}
return result
return v.data.([]map[string]interface{})
}

// IsMSI gets whether the object contained is a map[string]interface{} or not.
Expand Down Expand Up @@ -213,6 +208,8 @@ func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
switch s[i].(type) {
case Map:
result[i] = s[i].(Map)
case map[string]interface{}:
result[i] = New(s[i])
default:
return nil
}
Expand All @@ -224,12 +221,8 @@ func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
//
// Panics if the object is not a [](Map).
func (v *Value) MustObjxMapSlice() [](Map) {
if s, ok := v.data.([]map[string]interface{}); ok {
result := make([]Map, len(s))
for i := range s {
result[i] = s[i]
}
return result
if s := v.ObjxMapSlice(); s != nil {
return s
}
return v.data.([](Map))
}
Expand Down
21 changes: 19 additions & 2 deletions type_specific_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ func TestMSI(t *testing.T) {

func TestMSISlice(t *testing.T) {
val := map[string]interface{}(map[string]interface{}{"name": "Tyler"})
m := objx.Map{"value": []map[string]interface{}{val}, "value2": []objx.Map{val}, "nothing": nil}
m := objx.Map{
"value": []map[string]interface{}{val},
"value2": []objx.Map{val},
"value3": []interface{}{val},
"nothing": nil,
}

assert.Equal(t, val, m.Get("value").MSISlice()[0])
assert.Equal(t, val, m.Get("value2").MSISlice()[0])
assert.Equal(t, val, m.Get("value3").MSISlice()[0])
assert.Equal(t, val, m.Get("value").MustMSISlice()[0])
assert.Equal(t, val, m.Get("value2").MustMSISlice()[0])
assert.Equal(t, val, m.Get("value3").MustMSISlice()[0])
assert.Equal(t, []map[string]interface{}(nil), m.Get("nothing").MSISlice())
assert.Equal(t, val, m.Get("nothing").MSISlice([]map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"})})[0])
assert.Panics(t, func() {
Expand Down Expand Up @@ -230,13 +237,23 @@ func TestObjxMap(t *testing.T) {

func TestObjxMapSlice(t *testing.T) {
val := (objx.Map)(objx.New(1))
m := objx.Map{"value": [](objx.Map){val}, "value2": []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Taylor"})}, "nothing": nil}
m := objx.Map{
"value": [](objx.Map){val},
"value2": []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Taylor"})},
"value3": []interface{}{val},
"value4": []interface{}{map[string]interface{}(map[string]interface{}{"name": "Taylor"})},
"nothing": nil,
}
valMSI := objx.Map{"name": "Taylor"}

assert.Equal(t, val, m.Get("value").ObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value2").ObjxMapSlice()[0])
assert.Equal(t, val, m.Get("value3").ObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value4").ObjxMapSlice()[0])
assert.Equal(t, val, m.Get("value").MustObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value2").MustObjxMapSlice()[0])
assert.Equal(t, val, m.Get("value3").MustObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value4").MustObjxMapSlice()[0])
assert.Equal(t, [](objx.Map)(nil), m.Get("nothing").ObjxMapSlice())
assert.Equal(t, val, m.Get("nothing").ObjxMapSlice([](objx.Map){(objx.Map)(objx.New(1))})[0])
assert.Panics(t, func() {
Expand Down

0 comments on commit 0ab728f

Please sign in to comment.