Skip to content

Commit 0f325e4

Browse files
committed
comments
1 parent f59a16a commit 0f325e4

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ package jsonpointer
1515

1616
import "reflect"
1717

18+
// Deleter is an interface that is implemented by any type which can delete a
19+
// value by JSON pointer.
1820
type Deleter interface {
1921
DeleteByJSONPointer(ptr *JSONPointer) error
2022
}

errors.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ func IsKeyError(err error) bool {
165165
return ok
166166
}
167167

168+
// AsKeyError returns err as a ValueError, if possible. It does so by calling
169+
// errors.As, returning a KeyError and true if successful. If unsuccessful, nil
170+
// and false is returned.
168171
func AsKeyError(err error) (KeyError, bool) {
169172
var e KeyError
170173
return e, errors.As(err, &e)
@@ -243,6 +246,7 @@ func IsValueError(err error) bool {
243246
return ok
244247
}
245248

249+
// AsValueError returns err as a ValueError, if possible.
246250
func AsValueError(err error) (ValueError, bool) {
247251
var e ValueError
248252
return e, errors.As(err, &e)
@@ -317,9 +321,9 @@ func (e *indexError) Error() string {
317321
return fmt.Sprintf("%v for index %d of %d", e.err.Error(), e.index, e.maxIndex)
318322
}
319323

320-
// AsIndexError is a convenience function which calls calls errors.As, returning
321-
// err as an IndexError and true or nil and false if err can not be assigned to
322-
// an IndexError
324+
// AsIndexError returns err as a IndexError, if possible. It does so by calling
325+
// errors.As, returning a IndexError and true if successful. If unsuccessful, nil
326+
// and false is returned.
323327
func AsIndexError(err error) (IndexError, bool) {
324328
var ie IndexError
325329
return ie, errors.As(err, &ie)

resolve.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ import (
1818
"reflect"
1919
)
2020

21+
// Resolver is the interface that is implemented by types which can resolve json
22+
// pointers. The method is expected not to have side effects to the source.
2123
type Resolver interface {
2224
ResolveJSONPointer(ptr *JSONPointer, op Operation) (interface{}, error)
2325
}
2426

27+
// Resolve performs resolution on src by traversing the path of the JSON Pointer
28+
// and assigning the value to dst. If the path can not be reached, an error is
29+
// returned.
2530
func Resolve(src interface{}, ptr JSONPointer, dst interface{}) error {
2631
dv := reflect.ValueOf(dst)
2732
s := newState(ptr, Resolving)

token.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,48 @@ import (
1818
"strconv"
1919
)
2020

21+
// Decode decodes a JSON Pointer token by replacing each encoded slash ("~1")
22+
// with '/' (%x2F) and each encoded tilde ("~0") with '~' (%x7E).
2123
func Decode(token string) string {
2224
return decoder.Replace(token)
2325
}
2426

27+
// Encode encodes a string to a token of a JSON Pointer by replacing each '~'
28+
// (%x7E) with "~0" and '/' (%x2F) with "~1".
2529
func Encode(token string) string {
2630
return encoder.Replace(token)
2731
}
2832

33+
// Token is a segment of a JSON Pointer, divided by '/' (%x2F).
2934
type Token string
3035

36+
// Bytes returns the decoded Bytes of t
3137
func (t Token) Bytes() []byte {
32-
return []byte(t)
38+
return []byte(t.String())
3339
}
3440

41+
// String returns the decoded value of t
3542
func (t Token) String() string {
3643
return decoder.Replace(string(t))
3744
}
3845

46+
// Int64 attempts to parse t as an int64. If t can be parsed as an int64 then
47+
// the value is returned. If t can not be parsed as an int64 then an error is
48+
// returned.
3949
func (t Token) Int64() (int64, error) {
4050
return strconv.ParseInt(t.String(), 10, 64)
4151
}
4252

53+
// Uint64 attempts to parse t as an uint64. If t can be parsed as an uint64 then
54+
// the value is returned. If t can not be parsed as an uint64 then an error is
55+
// returned.
4356
func (t Token) Uint64() (uint64, error) {
4457
return strconv.ParseUint(t.String(), 10, 64)
4558
}
4659

60+
// Int attempts to parse t as an int. If t can be parsed as an int then
61+
// the value is returned. If t can not be parsed as an int then an error is
62+
// returned.
4763
func (t Token) Int() (int, error) {
4864
return strconv.Atoi(t.String())
4965
}
@@ -52,27 +68,6 @@ func (t Token) ptr() JSONPointer {
5268
return JSONPointer(t)
5369
}
5470

55-
// Tokens is a slice of Tokens.
56-
type Tokens []Token
57-
58-
// Strings returns ts as a slice of strings
59-
func (ts Tokens) Strings() []string {
60-
s := make([]string, len(ts))
61-
for i, t := range ts {
62-
s[i] = t.String()
63-
}
64-
return s
65-
}
66-
67-
// Stringers returns ts as a slice of fmt.Stringers
68-
func (ts Tokens) Stringers() []fmt.Stringer {
69-
s := make([]fmt.Stringer, len(ts))
70-
for i, t := range ts {
71-
s[i] = t
72-
}
73-
return s
74-
}
75-
7671
// Index parses t for an index value. If t can be parsed as an int, is equal to
7772
// or greater than 0 and less than or equal to next then the value is returned.
7873
// If t is equal to "-" then next is returned. If neither condition is true, -1
@@ -103,3 +98,24 @@ func (t Token) Index(next int) (int, error) {
10398
}
10499
return i, nil
105100
}
101+
102+
// Tokens is a slice of Tokens.
103+
type Tokens []Token
104+
105+
// Strings returns ts as a slice of strings
106+
func (ts Tokens) Strings() []string {
107+
s := make([]string, len(ts))
108+
for i, t := range ts {
109+
s[i] = t.String()
110+
}
111+
return s
112+
}
113+
114+
// Stringers returns ts as a slice of fmt.Stringers
115+
func (ts Tokens) Stringers() []fmt.Stringer {
116+
s := make([]fmt.Stringer, len(ts))
117+
for i, t := range ts {
118+
s[i] = t
119+
}
120+
return s
121+
}

0 commit comments

Comments
 (0)