Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ func parsePatch(patch []byte) ([]patchOperation, error) {
}
op.from = m.Value.Value.(Literal).String()
case "value":
m.Value.BeforeExtra = obj.beforeExtraAt(j + 0).extractLeadingComments(true)
leading := obj.beforeExtraAt(j + 0).extractLeadingComments(true)
// Comments between the colon and value belong to the value too.
if m.Value.BeforeExtra.hasComment() {
leading = append(leading, m.Value.BeforeExtra...)
}
m.Value.BeforeExtra = leading
m.Value.AfterExtra = obj.beforeExtraAt(j + 1).extractTrailingcomments(true)
op.value = m.Value
}
Expand Down
39 changes: 39 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -519,3 +520,41 @@ func TestPatch(t *testing.T) {
})
}
}

func TestPatchCommentsAfterValueColon(t *testing.T) {
for _, tt := range []struct{ name, input, op, path, want string }{
{"array add", `[]`, "add", "/-", `[42]`},
{"object add", `{}`, "add", "/item", `{"item":42}`},
{"array replace", `[0]`, "replace", "/0", `[42]`},
{"object replace", `{"item":0}`, "replace", "/item", `{"item":42}`},
{"root add", `null`, "add", "", `42`},
} {
for _, comment := range []string{"/* after colon */", "\n// after colon\n"} {
t.Run(tt.name+comment, func(t *testing.T) {
v, err := Parse([]byte(tt.input))
if err != nil {
t.Fatal(err)
}
patch := fmt.Sprintf(`[{"op":%q,"path":%q,
// before value member
"value":%s 42}]`, tt.op, tt.path, comment)
if err := v.Patch([]byte(patch)); err != nil {
t.Fatal(err)
}
v.Format()
got := string(v.Pack())
if !strings.Contains(got, "after colon") || !strings.Contains(got, "before value member") {
t.Fatalf("lost patch comments: %s", got)
}
v.Standardize()
want, err := Parse([]byte(tt.want))
if err != nil {
t.Fatal(err)
}
if !equalValue(v, want) {
t.Fatalf("patched value = %s; want %s", v.Pack(), tt.want)
}
})
}
}
}