Skip to content

Commit

Permalink
fix set within Map child element (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
geseq authored Aug 19, 2021
1 parent 4ff3852 commit 27373ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func access(current interface{}, selector string, value interface{}, isSet bool)
}

_, ok := curMSI[thisSel].(map[string]interface{})
if !ok {
_, ok = curMSI[thisSel].(Map)
}

if (curMSI[thisSel] == nil || !ok) && len(indexes) == 0 && isSet {
curMSI[thisSel] = map[string]interface{}{}
}
Expand Down
20 changes: 20 additions & 0 deletions accessors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ func TestAccessorsSet(t *testing.T) {
assert.Equal(t, "Mat", m.Get("name").Data())
}

func TestAccessorsSetWithinObjxMapChild(t *testing.T) {
m, err := objx.FromJSON(`{"a": {"b": 1}}`)
assert.NoError(t, err)

m.Set("a.c", 2)
jsonConverted, err := m.JSON()
assert.NoError(t, err)

m = objx.New(map[string]interface{}{
"a": map[string]interface{}{
"b": 1,
},
})
m.Set("a.c", 2)
jsonNewObj, err := m.JSON()

assert.NoError(t, err)
assert.Equal(t, jsonConverted, jsonNewObj)
}

func TestAccessorsNested(t *testing.T) {
d := objx.MustFromJSON(`{"values":[["test", "test1"], ["test2", {"name":"Mat"}, {"names": ["Captain", "Mat"]}]]}`)

Expand Down

0 comments on commit 27373ce

Please sign in to comment.