Skip to content

Commit 004bd17

Browse files
committed
fix: allow for empty json pointer str to select the root of the JSON document
1 parent 305b70b commit 004bd17

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

pointer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ func NewJSONPointer(jsonPointer string) (*JSONPointer, error) {
3232

3333
func parseJSONPointerString(jsonPointer string) ([]string, error) {
3434
if jsonPointer == JSONPointerEmptyPointer {
35-
return nil, fmt.Errorf(
36-
"jsonpointer: the jsonpointer is empty",
37-
)
35+
return []string{}, nil
3836
}
3937
if !strings.HasPrefix(jsonPointer, JSONPointerSeparatorToken) {
4038
return nil, fmt.Errorf(

pointer_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jsonpointergo
22

33
import (
4+
"reflect"
45
"testing"
56
)
67

@@ -10,7 +11,7 @@ func TestNewJSONPointer(t *testing.T) {
1011
wantErr bool
1112
}{
1213
{"/foo/bar", false},
13-
{"", true},
14+
{"", false},
1415
{"foo/bar", true},
1516
}
1617

@@ -39,6 +40,7 @@ func TestGetValue(t *testing.T) {
3940
{"/array/0", 1, false},
4041
{"/array/3", nil, true},
4142
{"/nonexistent", nil, true},
43+
{"", document, false},
4244
}
4345

4446
for _, test := range tests {
@@ -51,7 +53,7 @@ func TestGetValue(t *testing.T) {
5153
t.Errorf("JSONPointer.GetValue() error = %v, wantErr %v", err, test.wantErr)
5254
continue
5355
}
54-
if got != test.want {
56+
if !reflect.DeepEqual(got, test.want) {
5557
t.Errorf("JSONPointer.GetValue() = %v, want %v", got, test.want)
5658
}
5759
}

0 commit comments

Comments
 (0)