Skip to content

Commit

Permalink
Restore m['str'] where m map[interface{}]interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Aug 8, 2017
1 parent e39a1fe commit 9852226
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions values/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func (v arrayValue) IndexValue(index Value) Value {
func (v mapValue) IndexValue(index Value) Value {
rv := reflect.ValueOf(v.basis)
iv := reflect.ValueOf(index.Interface())
if iv.IsValid() && rv.Type().Key() == iv.Type() {
ev := rv.MapIndex(iv)
if iv.IsValid() && iv.Type().ConvertibleTo(rv.Type().Key()) {
ev := rv.MapIndex(iv.Convert(rv.Type().Key()))
if ev.IsValid() {
return ValueOf(ev.Interface())
}
Expand Down
16 changes: 12 additions & 4 deletions values/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ func TestValue_IndexValue(t *testing.T) {
require.Equal(t, "second", av.IndexValue(ValueOf(1.1)).Interface())
require.Nil(t, av.IndexValue(ValueOf(nil)).Interface())

// hash
// string map
hv := ValueOf(map[string]interface{}{"key": "value"})
require.Equal(t, "value", hv.IndexValue(ValueOf("key")).Interface())
require.Nil(t, hv.IndexValue(ValueOf("missing_key")).Interface())
require.Nil(t, hv.IndexValue(ValueOf(nil)).Interface())

// hash ptr
// interface map
hv = ValueOf(map[interface{}]interface{}{"key": "value"})
require.Equal(t, "value", hv.IndexValue(ValueOf("key")).Interface())

// ptr to map
hashPtr := ValueOf(&map[string]interface{}{"key": "value"})
require.Equal(t, "value", hashPtr.IndexValue(ValueOf("key")).Interface())
require.Nil(t, hashPtr.IndexValue(ValueOf("missing_key")).Interface())
Expand All @@ -90,13 +94,17 @@ func TestValue_PropertyValue(t *testing.T) {
require.Equal(t, "third", av.PropertyValue(ValueOf("last")).Interface())
require.Nil(t, av.PropertyValue(ValueOf(nil)).Interface())

// hash
// string map
hv := ValueOf(map[string]interface{}{"key": "value"})
require.Equal(t, "value", hv.PropertyValue(ValueOf("key")).Interface())
require.Nil(t, hv.PropertyValue(ValueOf("missing_key")).Interface())
require.Nil(t, hv.PropertyValue(ValueOf(nil)).Interface())

// hash ptr
// interface map
hv = ValueOf(map[interface{}]interface{}{"key": "value"})
require.Equal(t, "value", hv.PropertyValue(ValueOf("key")).Interface())

// ptr to map
hashPtr := ValueOf(&map[string]interface{}{"key": "value"})
require.Equal(t, "value", hashPtr.PropertyValue(ValueOf("key")).Interface())
require.Nil(t, hashPtr.PropertyValue(ValueOf("missing_key")).Interface())
Expand Down

0 comments on commit 9852226

Please sign in to comment.