diff --git a/cmd/patch.go b/cmd/patch.go index 0ad931b6..f4f034a3 100644 --- a/cmd/patch.go +++ b/cmd/patch.go @@ -303,8 +303,20 @@ func addValue(parentMap map[string]interface{}, keys []string, value interface{} if lengthKeys == 0 { return fmt.Errorf("invalid map key (nil)") } - nextNodeKey := keys[0] - nextNode := parentMap[nextNodeKey] + + // if lengthKeys + var nextNodeKey string // := keys[0] + var nextNode interface{} // := parentMap[nextNodeKey] + + switch lengthKeys { + case 0: + return fmt.Errorf("invalid map key (nil)") + case 1: // special case of adding new key/value to document root + nextNode = parentMap + default: // adding keys/values along document path + nextNodeKey := keys[0] + nextNode = parentMap[nextNodeKey] + } switch typedNode := nextNode.(type) { case map[string]interface{}: diff --git a/cmd/patch_test.go b/cmd/patch_test.go index e92bc2e9..3e84ace4 100644 --- a/cmd/patch_test.go +++ b/cmd/patch_test.go @@ -254,6 +254,22 @@ func TestPatchCdx15SliceAdd(t *testing.T) { getLogger().Tracef("%s\n", buffer.String()) } +func TestPatchRFC6902AppendixA1Patch1(t *testing.T) { + ti := NewPatchTestInfo( + TEST_PATCH_RFC_6902_APPX_A_1_BASE, + TEST_PATCH_RFC_6902_APPX_A_1_PATCH_1, nil) + ti.IsInputJSON = true + buffer, _, err := innerTestPatch(t, ti) + if err != nil { + t.Error(err) + } + getLogger().Tracef("%s\n", buffer.String()) + // lineNum, _ := bufferLineContainsValues(buffer, -1, "qux") + // if lineNum != 3 { + // t.Errorf("invalid output. Expected added value: \"qux\" at line: 3") + // } +} + func TestPatchRFC6902AppendixA2Patch1(t *testing.T) { ti := NewPatchTestInfo( TEST_PATCH_RFC_6902_APPX_A_2_BASE,