Skip to content

Commit f307a21

Browse files
committed
docs: add comments to describe the package
1 parent 4fb7105 commit f307a21

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Package jsonpointer-go provides functions to use jsonpointer
2+
// defined by the [RFC 6902] in Golang.
3+
//
4+
// [RFC 6902]: https://www.rfc-editor.org/rfc/rfc6902
5+
package jsonpointergo

pointer.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ const (
1414
JSONPointerTildaEncoded = "~0"
1515
)
1616

17+
// JSONObject is a type alias for a map with string keys and values of
18+
// any type
1719
type JSONObject = map[string]any
1820

21+
// JSONPointer struct holds the parsed reference tokens of a JSON
22+
// Pointer
1923
type JSONPointer struct {
24+
// Slice of reference tokens derived from the JSON Pointer
2025
referenceTokens []string
2126
}
2227

28+
// NewJSONPointer creates a new JSONPointer instance from a JSON
29+
// Pointer string
2330
func NewJSONPointer(jsonPointer string) (*JSONPointer, error) {
2431
tokens, err := parseJSONPointerString(jsonPointer)
2532
if err != nil {
@@ -30,6 +37,8 @@ func NewJSONPointer(jsonPointer string) (*JSONPointer, error) {
3037
}, nil
3138
}
3239

40+
// parseJSONPointerString parses a JSON Pointer string into its
41+
// reference tokens
3342
func parseJSONPointerString(jsonPointer string) ([]string, error) {
3443
if jsonPointer == JSONPointerEmptyPointer {
3544
return []string{}, nil
@@ -40,32 +49,39 @@ func parseJSONPointerString(jsonPointer string) ([]string, error) {
4049
JSONPointerSeparatorToken,
4150
)
4251
}
52+
// Split the JSON Pointer into tokens
4353
tokens := strings.Split(jsonPointer, JSONPointerSeparatorToken)
4454
return tokens[1:], nil
4555
}
4656

57+
// GetValue retrieves the value from the JSON document based on the
58+
// JSON Pointer
4759
func (jp *JSONPointer) GetValue(document JSONObject) (any, error) {
4860
if document == nil {
4961
return nil, fmt.Errorf(
5062
"jsonpointer: the JSON document provided is nil",
5163
)
5264
}
5365
var subDocument any
66+
// Start with the root of the JSON document
5467
subDocument = document
5568
for i, tokenRefEncoded := range jp.referenceTokens {
5669
tokenRef := decodeJSONPointerReference(tokenRefEncoded)
70+
// Handle the case where the value is a JSON object
5771
jsonDoc, ok := subDocument.(JSONObject)
5872
if ok {
5973
value, ok := jsonDoc[tokenRef]
6074
if !ok {
6175
return nil, fmt.Errorf(
6276
"jsonpointer: the document provided does not have the following reference: %v, %v",
63-
tokenRef, i,
77+
tokenRef,
78+
i,
6479
)
6580
}
6681
subDocument = value
6782
continue
6883
}
84+
// Handle the case where the value is an Array
6985
jsonArray, ok := subDocument.([]any)
7086
if ok {
7187
index, err := strconv.Atoi(tokenRef)
@@ -85,19 +101,28 @@ func (jp *JSONPointer) GetValue(document JSONObject) (any, error) {
85101
subDocument = jsonArray[index]
86102
continue
87103
}
88-
return nil, fmt.Errorf("jsonpointer: the reference is trying to access a single value: %v. Type of subdocument: %T", tokenRef, subDocument)
104+
// Handle the case where the value is a single value
105+
return nil, fmt.Errorf(
106+
"jsonpointer: the reference is trying to access a single value: %v. Type of subdocument: %T",
107+
tokenRef,
108+
subDocument,
109+
)
89110
}
90111
return subDocument, nil
91112
}
92113

114+
// decodeJSONPointerReference decodes a reference token by replacing
115+
// escape sequences
93116
func decodeJSONPointerReference(ref string) string {
94-
refWithSlash := strings.ReplaceAll(
117+
// Replace "~1" with "/"
118+
ref = strings.ReplaceAll(
95119
ref,
96120
JSONPointerSlashEncoded,
97121
JSONPointerSeparatorToken,
98122
)
123+
// Replace "~0" with "~"
99124
return strings.ReplaceAll(
100-
refWithSlash,
125+
ref,
101126
JSONPointerTildaEncoded,
102127
JSONPointerEscapeToken,
103128
)

pointer_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ func TestNewJSONPointer(t *testing.T) {
1818
for _, test := range tests {
1919
_, err := NewJSONPointer(test.jsonPointer)
2020
if (err != nil) != test.wantErr {
21-
t.Errorf("NewJSONPointer(%v) error = %v, wantErr %v", test.jsonPointer, err, test.wantErr)
21+
t.Errorf(
22+
"NewJSONPointer(%v) error = %v, wantErr %v",
23+
test.jsonPointer,
24+
err,
25+
test.wantErr,
26+
)
2227
}
2328
}
2429
}
@@ -46,15 +51,27 @@ func TestGetValue(t *testing.T) {
4651
for _, test := range tests {
4752
jp, err := NewJSONPointer(test.jsonPointer)
4853
if err != nil {
49-
t.Fatalf("NewJSONPointer(%v) error = %v", test.jsonPointer, err)
54+
t.Fatalf(
55+
"NewJSONPointer(%v) error = %v",
56+
test.jsonPointer,
57+
err,
58+
)
5059
}
5160
got, err := jp.GetValue(document)
5261
if (err != nil) != test.wantErr {
53-
t.Errorf("JSONPointer.GetValue() error = %v, wantErr %v", err, test.wantErr)
62+
t.Errorf(
63+
"JSONPointer.GetValue() error = %v, wantErr %v",
64+
err,
65+
test.wantErr,
66+
)
5467
continue
5568
}
5669
if !reflect.DeepEqual(got, test.want) {
57-
t.Errorf("JSONPointer.GetValue() = %v, want %v", got, test.want)
70+
t.Errorf(
71+
"JSONPointer.GetValue() = %v, want %v",
72+
got,
73+
test.want,
74+
)
5875
}
5976
}
6077
}
@@ -71,7 +88,12 @@ func TestDecodeJSONPointerReference(t *testing.T) {
7188

7289
for _, test := range tests {
7390
if got := decodeJSONPointerReference(test.ref); got != test.want {
74-
t.Errorf("decodeJSONPointerReference(%v) = %v, want %v", test.ref, got, test.want)
91+
t.Errorf(
92+
"decodeJSONPointerReference(%v) = %v, want %v",
93+
test.ref,
94+
got,
95+
test.want,
96+
)
7597
}
7698
}
7799
}

0 commit comments

Comments
 (0)