@@ -18,32 +18,48 @@ import (
18
18
"strconv"
19
19
)
20
20
21
+ // Decode decodes a JSON Pointer token by replacing each encoded slash ("~1")
22
+ // with '/' (%x2F) and each encoded tilde ("~0") with '~' (%x7E).
21
23
func Decode (token string ) string {
22
24
return decoder .Replace (token )
23
25
}
24
26
27
+ // Encode encodes a string to a token of a JSON Pointer by replacing each '~'
28
+ // (%x7E) with "~0" and '/' (%x2F) with "~1".
25
29
func Encode (token string ) string {
26
30
return encoder .Replace (token )
27
31
}
28
32
33
+ // Token is a segment of a JSON Pointer, divided by '/' (%x2F).
29
34
type Token string
30
35
36
+ // Bytes returns the decoded Bytes of t
31
37
func (t Token ) Bytes () []byte {
32
- return []byte (t )
38
+ return []byte (t . String () )
33
39
}
34
40
41
+ // String returns the decoded value of t
35
42
func (t Token ) String () string {
36
43
return decoder .Replace (string (t ))
37
44
}
38
45
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.
39
49
func (t Token ) Int64 () (int64 , error ) {
40
50
return strconv .ParseInt (t .String (), 10 , 64 )
41
51
}
42
52
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.
43
56
func (t Token ) Uint64 () (uint64 , error ) {
44
57
return strconv .ParseUint (t .String (), 10 , 64 )
45
58
}
46
59
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.
47
63
func (t Token ) Int () (int , error ) {
48
64
return strconv .Atoi (t .String ())
49
65
}
@@ -52,27 +68,6 @@ func (t Token) ptr() JSONPointer {
52
68
return JSONPointer (t )
53
69
}
54
70
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
-
76
71
// Index parses t for an index value. If t can be parsed as an int, is equal to
77
72
// or greater than 0 and less than or equal to next then the value is returned.
78
73
// 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) {
103
98
}
104
99
return i , nil
105
100
}
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