diff --git a/accessors.go b/accessors.go index b193d51..4c60455 100644 --- a/accessors.go +++ b/accessors.go @@ -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{}{} } diff --git a/accessors_test.go b/accessors_test.go index c43c18c..6eaab60 100644 --- a/accessors_test.go +++ b/accessors_test.go @@ -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"]}]]}`)