Skip to content

Commit

Permalink
fix: conversion from JSON Pointer to GJSON Path
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed May 7, 2023
1 parent 6aa85d3 commit 73540d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
54 changes: 27 additions & 27 deletions differ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ func TestArrayCases(t *testing.T) { runCasesFromFile(t, "testdata/tests/array.j
func TestObjectCases(t *testing.T) { runCasesFromFile(t, "testdata/tests/object.json") }
func TestRootCases(t *testing.T) { runCasesFromFile(t, "testdata/tests/root.json") }

func TestDiffer_Reset(t *testing.T) {
d := &Differ{
ptr: pointer{
buf: make([]byte, 15, 15),
end: 15,
},
hashmap: map[uint64]jsonNode{
1: {},
},
patch: make([]Operation, 42, 42),
}
d.Reset()

if l := len(d.patch); l != 0 {
t.Errorf("expected empty patch collection, got length %d", l)
}
if l := len(d.hashmap); l != 0 {
t.Errorf("expected cleared hashmap, got length %d", l)
}
if d.ptr.end != 0 {
t.Errorf("expected reset ptr")
}
if l := len(d.ptr.buf); l != 0 {
t.Errorf("expected empty ptr buf, got length %d", l)
}
}

func TestOptions(t *testing.T) {
makeopts := func(opts ...Option) []Option { return opts }

Expand Down Expand Up @@ -127,30 +154,3 @@ func runTestCase(t *testing.T, tc testcase, pc patchGetter, opts ...Option) {
}
}
}

func TestDiffer_Reset(t *testing.T) {
d := &Differ{
ptr: pointer{
buf: make([]byte, 15, 15),
end: 15,
},
hashmap: map[uint64]jsonNode{
1: {},
},
patch: make([]Operation, 42, 42),
}
d.Reset()

if l := len(d.patch); l != 0 {
t.Errorf("expected empty patch collection, got length %d", l)
}
if l := len(d.hashmap); l != 0 {
t.Errorf("expected cleared hashmap, got length %d", l)
}
if d.ptr.end != 0 {
t.Errorf("expected reset ptr")
}
if l := len(d.ptr.buf); l != 0 {
t.Errorf("expected empty ptr buf, got length %d", l)
}
}
9 changes: 4 additions & 5 deletions pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ var (
// https://tools.ietf.org/html/rfc6901
rfc6901Escaper = strings.NewReplacer("~", "~0", "/", "~1")

// dotPathReplacer converts a RFC6901 JSON pointer to
// a JSON path, while also escaping any existing dot
// characters present in the original pointer.
dotPathReplacer = strings.NewReplacer(".", "\\.", "/", ".")
// pointerToGJSONPath converts a RFC6901 JSON pointer to a GJSON path.
// See https://github.com/tidwall/gjson/blob/master/SYNTAX.md
pointerToGJSONPath = strings.NewReplacer(".", "\\.", "*", "\\*", "?", "\\?", "/", ".", "~0", "~", "~1", "/")
)

// pointer represents an RFC 6901 JSON Pointer.
Expand Down Expand Up @@ -93,7 +92,7 @@ func (p pointer) reset() pointer {

func toJSONPath(s string) string {
if len(s) != 0 {
return dotPathReplacer.Replace(s[1:])
return pointerToGJSONPath.Replace(s[1:])
}
// @this is a special modifier that can
// be used to retrieve the root path.
Expand Down
20 changes: 20 additions & 0 deletions pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ func Test_toJSONPath(t *testing.T) {
"/a/b/c",
"a.b.c",
},
{
"/a/b~1c/",
"a.b/c.",
},
{
"/a~0/b~0/~1",
"a~.b~./",
},
{
"/foo//a~1b",
"foo..a/b",
},
{
"/a.b~1/c//d",
"a\\.b/.c..d",
},
{
"/a/b?/c*",
"a.b\\?.c\\*",
},
{
"",
"@this",
Expand Down

0 comments on commit 73540d8

Please sign in to comment.