Skip to content

Commit dd629d4

Browse files
committed
renames JSONPointer to Pointer. Creates type Alias for JSONPointer
1 parent 7326157 commit dd629d4

File tree

12 files changed

+72
-68
lines changed

12 files changed

+72
-68
lines changed

assign.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// resolution is needed, the type should also implement Resolver.
2626
//
2727
type Assigner interface {
28-
AssignByJSONPointer(ptr *JSONPointer, value interface{}) error
28+
AssignByJSONPointer(ptr *Pointer, value interface{}) error
2929
}
3030

3131
// Assign performs an assignment of value to the target dst specified by the
@@ -38,7 +38,7 @@ type Assigner interface {
3838
// If a type in the path implements Assigner, AssignByJSONPointer will be called
3939
// with the updated value pertinent to that path.
4040
//
41-
func Assign(dst interface{}, ptr JSONPointer, value interface{}) error {
41+
func Assign(dst interface{}, ptr Pointer, value interface{}) error {
4242
if value == nil {
4343
return Delete(dst, ptr)
4444
}

assign_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestAssign(t *testing.T) {
2929
var r *Root
3030

3131
tests := []struct {
32-
ptr jsonpointer.JSONPointer
32+
ptr jsonpointer.Pointer
3333
value interface{}
3434
run func(v interface{}, err error)
3535
}{
@@ -121,7 +121,7 @@ func TestAssignAny(t *testing.T) {
121121

122122
m := map[string]interface{}{}
123123
tests := []struct {
124-
ptr jsonpointer.JSONPointer
124+
ptr jsonpointer.Pointer
125125
value interface{}
126126
err error
127127
run func(v interface{})
@@ -183,7 +183,7 @@ func TestAssignJSON(t *testing.T) {
183183
_ = assert
184184

185185
tests := []struct {
186-
ptr jsonpointer.JSONPointer
186+
ptr jsonpointer.Pointer
187187
json string
188188
value interface{}
189189
run func(v Root, err error)

delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import "reflect"
1818
// Deleter is an interface that is implemented by any type which can delete a
1919
// value by JSON pointer.
2020
type Deleter interface {
21-
DeleteByJSONPointer(ptr *JSONPointer) error
21+
DeleteByJSONPointer(ptr *Pointer) error
2222
}
2323

2424
// Delete deletes the value at the given JSON pointer from src.
2525
//
2626
// If any part of the path is unreachable, the Delete function is
2727
// considered a success as the value is not present to delete.
28-
func Delete(src interface{}, ptr JSONPointer) error {
28+
func Delete(src interface{}, ptr Pointer) error {
2929
dv := reflect.ValueOf(src)
3030
s := newState(ptr, Deleting)
3131
defer s.Release()

delete_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestDelete(t *testing.T) {
2626
assert := require.New(t)
2727

2828
tests := []struct {
29-
ptr jsonpointer.JSONPointer
29+
ptr jsonpointer.Pointer
3030
root Root
3131
run func(r Root, err error)
3232
}{
@@ -58,7 +58,7 @@ func TestDeleteJSON(t *testing.T) {
5858
assert := require.New(t)
5959

6060
tests := []struct {
61-
ptr jsonpointer.JSONPointer
61+
ptr jsonpointer.Pointer
6262
root Root
6363
run func(r Root, err error)
6464
}{

errors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ var (
8585
// Error is a base error type returned from Resolve, Assign, and Delete.
8686
type Error interface {
8787
error
88-
JSONPointer() JSONPointer
89-
CurrentJSONPointer() JSONPointer
88+
JSONPointer() Pointer
89+
CurrentJSONPointer() Pointer
9090
Token() (Token, bool)
9191
Operation() Operation
9292
Unwrap() error
@@ -135,7 +135,7 @@ func (e *ptrError) updateState(s state) {
135135
}
136136

137137
// JSONPointer returns the initial JSONPointer.
138-
func (e *ptrError) JSONPointer() JSONPointer {
138+
func (e *ptrError) JSONPointer() Pointer {
139139
return e.ptr
140140
}
141141

jsonpointer.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636

3737
const (
3838
// Root is a top-level JSONPointer, indicated by an empty string.
39-
Root JSONPointer = ""
39+
Root Pointer = ""
4040
)
4141

4242
// New encodes and returns the token + tokens into a JSONPointer.
@@ -49,15 +49,15 @@ const (
4949
// jsonpointer.New("/") => "/~1"
5050
// jsonpointer.New("~") => "/~0"
5151
//
52-
func New(tokens ...string) JSONPointer {
52+
func New(tokens ...string) Pointer {
5353
return NewFromStrings(tokens)
5454
}
5555

5656
// NewFromStrings encodes and returns the tokens into a JSONPointer.
5757
//
5858
// Examples:
5959
// jsonpointer.NewFromStrings([]string{"foo", "bar", "baz"}) => "/foo/bar/baz"
60-
func NewFromStrings(tokens []string) JSONPointer {
60+
func NewFromStrings(tokens []string) Pointer {
6161
b := &strings.Builder{}
6262
b.Grow(len(tokens))
6363
if len(tokens) == 0 {
@@ -70,31 +70,35 @@ func NewFromStrings(tokens []string) JSONPointer {
7070
panic(err)
7171
}
7272
}
73-
return JSONPointer(b.String())
73+
return Pointer(b.String())
7474
}
7575

76-
// A JSONPointer is a Unicode string containing a sequence of zero or more
76+
// Alias for Pointer
77+
// Deprecated in favor of Pointer
78+
type JSONPointer = Pointer
79+
80+
// A Pointer is a Unicode string containing a sequence of zero or more
7781
// reference tokens, each prefixed by a '/' character.
7882
//
7983
// See [rfc 6901 for more information](https://datatracker.ietf.org/doc/html/rfc6901).
8084
//
81-
type JSONPointer string
85+
type Pointer string
8286

83-
func (p JSONPointer) String() string {
87+
func (p Pointer) String() string {
8488
return string(p)
8589
}
8690

8791
// Append appends token to the end of reference p and returns the new JSONPointer.
8892
//
8993
// Note: token is not encoded. Use p.AppendString to encode and append the
9094
// token.
91-
func (p JSONPointer) Append(token Token) JSONPointer {
95+
func (p Pointer) Append(token Token) Pointer {
9296
return p + "/" + token.ptr()
9397
}
9498

9599
// AppendString encodes and appends token to the value of p and returns the new
96100
// JSONPointer.
97-
func (p JSONPointer) AppendString(token string) JSONPointer {
101+
func (p Pointer) AppendString(token string) Pointer {
98102
return p.Append(Token(encoder.Replace(token)))
99103
}
100104

@@ -103,13 +107,13 @@ func (p JSONPointer) AppendString(token string) JSONPointer {
103107
//
104108
// Note: token is not encoded. Use p.PrependString to encode and prepend the
105109
// token.
106-
func (p JSONPointer) Prepend(token Token) JSONPointer {
110+
func (p Pointer) Prepend(token Token) Pointer {
107111
return "/" + token.ptr() + p
108112
}
109113

110114
// PrependString encodes and prepends token to the value of p and returns the new
111115
// JSONPointer.
112-
func (p JSONPointer) PrependString(token string) JSONPointer {
116+
func (p Pointer) PrependString(token string) Pointer {
113117
return p.Prepend(Token(encoder.Replace(token)))
114118
}
115119

@@ -121,14 +125,14 @@ func (p JSONPointer) PrependString(token string) JSONPointer {
121125
// by a '0' or '1'.
122126
//
123127
//
124-
func (p JSONPointer) Validate() (err error) {
128+
func (p Pointer) Validate() (err error) {
125129
if err = p.validateStart(); err != nil {
126130
return err
127131
}
128132
return p.validateeEncoding()
129133
}
130134

131-
func (p JSONPointer) validateeEncoding() error {
135+
func (p Pointer) validateeEncoding() error {
132136
if len(p) == 0 {
133137
return nil
134138
}
@@ -143,29 +147,29 @@ func (p JSONPointer) validateeEncoding() error {
143147
return nil
144148
}
145149

146-
func (p JSONPointer) validateStart() error {
150+
func (p Pointer) validateStart() error {
147151
if len(p) > 0 && !startsWithSlash(p) {
148152
return ErrMalformedStart
149153
}
150154
return nil
151155
}
152156

153-
func (p JSONPointer) Pop() (JSONPointer, Token, bool) {
157+
func (p Pointer) Pop() (Pointer, Token, bool) {
154158
i := lastSlash(p)
155159
if i == -1 {
156160
return "", "", false
157161
}
158-
return JSONPointer(p[:i]), Token(p[i+1:]), true
162+
return Pointer(p[:i]), Token(p[i+1:]), true
159163
}
160164

161-
func (p JSONPointer) LastToken() (Token, bool) {
165+
func (p Pointer) LastToken() (Token, bool) {
162166
_, t, e := p.Pop()
163167
return t, e
164168
}
165169

166170
// Next splits the JSONPointer at the first slash and returns the token and the
167171
// remaining JSONPointer.
168-
func (p JSONPointer) Next() (JSONPointer, Token, bool) {
172+
func (p Pointer) Next() (Pointer, Token, bool) {
169173
if p == "" {
170174
return "", "", false
171175
}
@@ -176,27 +180,27 @@ func (p JSONPointer) Next() (JSONPointer, Token, bool) {
176180
case 0:
177181
return "", Token(p[1:]), true
178182
default:
179-
return JSONPointer(p[i:]), Token(p[1:i]), true
183+
return Pointer(p[i:]), Token(p[1:i]), true
180184
}
181185
}
182186

183187
// NextToken splits the JSONPointer at the first slash and returns the token.
184-
func (p JSONPointer) NextToken() (Token, bool) {
188+
func (p Pointer) NextToken() (Token, bool) {
185189
_, t, ok := p.Next()
186190
return t, ok
187191
}
188192

189-
func (p JSONPointer) NextPointer() (JSONPointer, bool) {
193+
func (p Pointer) NextPointer() (Pointer, bool) {
190194
v, _, ok := p.Next()
191195
return v, ok
192196
}
193197

194-
func (p JSONPointer) IsRoot() bool {
198+
func (p Pointer) IsRoot() bool {
195199
return p == Root
196200
}
197201

198202
// Tokens returns the decoded tokens of the JSONPointer.
199-
func (p JSONPointer) Tokens() []string {
203+
func (p Pointer) Tokens() []string {
200204
if p == "" {
201205
return []string{}
202206
}
@@ -212,19 +216,19 @@ func (p JSONPointer) Tokens() []string {
212216

213217
// lastSlash(ptr) returns the index of the last slash in the JSONPointer or -1
214218
// if not present
215-
func lastSlash(ptr JSONPointer) int {
219+
func lastSlash(ptr Pointer) int {
216220
return strings.LastIndexByte(string(ptr), '/')
217221
}
218222

219-
func startsWithSlash(ptr JSONPointer) bool {
223+
func startsWithSlash(ptr Pointer) bool {
220224
if len(ptr) == 0 {
221225
return false
222226
}
223227
return ptr[0] == '/'
224228
}
225229

226230
// nextSlash(ptr) returns the index of the next slash in the JSONPointer.
227-
func nextSlash(ptr JSONPointer) int {
231+
func nextSlash(ptr Pointer) int {
228232
if !startsWithSlash(ptr) {
229233
return -1
230234
}

jsonpointer_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (str strval) String() string { return string(str) }
3434
func TestNew(t *testing.T) {
3535
assert := require.New(t)
3636
tests := []struct {
37-
pointer jsonpointer.JSONPointer
37+
pointer jsonpointer.Pointer
3838
expectedstring string
3939
}{
4040
{jsonpointer.New(""), "/"},
@@ -52,7 +52,7 @@ func TestNew(t *testing.T) {
5252
func TestJSONPointerValidate(t *testing.T) {
5353
assert := require.New(t)
5454
tests := []struct {
55-
ptr jsonpointer.JSONPointer
55+
ptr jsonpointer.Pointer
5656
err error
5757
}{
5858
{"", nil},
@@ -86,8 +86,8 @@ func TestNewFromStrings(t *testing.T) {
8686
func TestJSONPointerNext(t *testing.T) {
8787
assert := require.New(t)
8888
tests := []struct {
89-
pointer jsonpointer.JSONPointer
90-
expectedpointer jsonpointer.JSONPointer
89+
pointer jsonpointer.Pointer
90+
expectedpointer jsonpointer.Pointer
9191
expectedtoken jsonpointer.Token
9292
expectedok bool
9393
}{
@@ -110,8 +110,8 @@ func TestJSONPointerNext(t *testing.T) {
110110
func TestJSONPointerPop(t *testing.T) {
111111
assert := require.New(t)
112112
pt := []struct {
113-
pointer jsonpointer.JSONPointer
114-
expectedpointer jsonpointer.JSONPointer
113+
pointer jsonpointer.Pointer
114+
expectedpointer jsonpointer.Pointer
115115
expectedtoken jsonpointer.Token
116116
expectedok bool
117117
}{

resolve.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
// Resolver is the interface that is implemented by types which can resolve json
2222
// pointers. The method is expected not to have side effects to the source.
2323
type Resolver interface {
24-
ResolveJSONPointer(ptr *JSONPointer, op Operation) (interface{}, error)
24+
ResolveJSONPointer(ptr *Pointer, op Operation) (interface{}, error)
2525
}
2626

2727
// Resolve performs resolution on src by traversing the path of the JSON Pointer
2828
// and assigning the value to dst. If the path can not be reached, an error is
2929
// returned.
30-
func Resolve(src interface{}, ptr JSONPointer, dst interface{}) error {
30+
func Resolve(src interface{}, ptr Pointer, dst interface{}) error {
3131
dv := reflect.ValueOf(dst)
3232
s := newState(ptr, Resolving)
3333
defer s.Release()

resolve_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestResolveField(t *testing.T) {
4949
}
5050

5151
tests := []struct {
52-
ptr jsonpointer.JSONPointer
52+
ptr jsonpointer.Pointer
5353
expectedval interface{}
5454
expectederr error
5555
}{
@@ -101,7 +101,7 @@ func TestResolveMapIndex(t *testing.T) {
101101
}
102102

103103
tests := []struct {
104-
ptr jsonpointer.JSONPointer
104+
ptr jsonpointer.Pointer
105105
expectedval interface{}
106106
expectederr error
107107
}{
@@ -151,7 +151,7 @@ func TestResolveArray(t *testing.T) {
151151
}
152152

153153
tests := []struct {
154-
ptr jsonpointer.JSONPointer
154+
ptr jsonpointer.Pointer
155155
expectedval interface{}
156156
expectederr error
157157
}{
@@ -206,7 +206,7 @@ func TestResolveSlice(t *testing.T) {
206206
}
207207

208208
tests := []struct {
209-
ptr jsonpointer.JSONPointer
209+
ptr jsonpointer.Pointer
210210
expectedval interface{}
211211
expectederr error
212212
}{
@@ -236,7 +236,7 @@ func TestResolveJSON(t *testing.T) {
236236
assert := require.New(t)
237237

238238
tests := []struct {
239-
ptr jsonpointer.JSONPointer
239+
ptr jsonpointer.Pointer
240240
json string
241241
val interface{}
242242
err error

0 commit comments

Comments
 (0)