Skip to content

Commit dea88d2

Browse files
committed
Refactor string representation of tokens
1 parent d86cbba commit dea88d2

8 files changed

+54
-40
lines changed

patch/pointer.go

+8-38
Original file line numberDiff line numberDiff line change
@@ -114,47 +114,17 @@ func (p Pointer) IsSet() bool { return len(p.tokens) > 0 }
114114
func (p Pointer) String() string {
115115
var strs []string
116116

117-
optional := false
118-
117+
seenOptional := false
119118
for _, token := range p.tokens {
120-
switch typedToken := token.(type) {
121-
case RootToken:
122-
strs = append(strs, "")
123-
124-
case IndexToken:
125-
strs = append(strs, fmt.Sprintf("%d", typedToken.Index))
126-
127-
case AfterLastIndexToken:
128-
strs = append(strs, "-")
129-
130-
case MatchingIndexToken:
131-
key := rfc6901Encoder.Replace(typedToken.Key)
132-
val := rfc6901Encoder.Replace(typedToken.Value)
133-
134-
if typedToken.Optional {
135-
if !optional {
136-
val += "?"
137-
optional = true
138-
}
119+
s := token.String()
120+
if strings.HasSuffix(s, "?") {
121+
if seenOptional {
122+
s = s[:len(s)-1]
123+
} else {
124+
seenOptional = true
139125
}
140-
141-
strs = append(strs, fmt.Sprintf("%s=%s", key, val))
142-
143-
case KeyToken:
144-
str := rfc6901Encoder.Replace(typedToken.Key)
145-
146-
if typedToken.Optional { // /key?/key2/key3
147-
if !optional {
148-
str += "?"
149-
optional = true
150-
}
151-
}
152-
153-
strs = append(strs, str)
154-
155-
default:
156-
panic(fmt.Sprintf("Unknown token type '%T'", typedToken))
157126
}
127+
strs = append(strs, s)
158128
}
159129

160130
return strings.Join(strs, "/")

patch/token_afterlastindex.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77

88
type AfterLastIndexToken struct{}
99

10+
func (t AfterLastIndexToken) String() string {
11+
return "-"
12+
}
13+
1014
func (t AfterLastIndexToken) processDescent(ctx *tokenContext) (interface{}, error) {
1115
switch ctx.Method {
1216
case methodFind:

patch/token_index.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package patch
22

3-
import "errors"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
type IndexToken struct {
69
Index int
710
}
811

12+
func (t IndexToken) String() string {
13+
return fmt.Sprintf("%d", t.Index)
14+
}
15+
916
func (t IndexToken) processDescent(ctx *tokenContext) (interface{}, error) {
1017
typedObj, ok := ctx.Node.([]interface{})
1118
if !ok {

patch/token_key.go

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ type KeyToken struct {
1111
Optional bool
1212
}
1313

14+
func (t KeyToken) String() string {
15+
str := rfc6901Encoder.Replace(t.Key)
16+
17+
if t.Optional { // /key?/key2/key3
18+
str += "?"
19+
}
20+
21+
return str
22+
}
23+
1424
func (t KeyToken) processDescent(ctx *tokenContext) (interface{}, error) {
1525
typedObj, ok := ctx.Node.(map[interface{}]interface{})
1626
if !ok {

patch/token_matchingindex.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package patch
22

3-
import "errors"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
type MatchingIndexToken struct {
69
Key string
@@ -9,6 +12,17 @@ type MatchingIndexToken struct {
912
Optional bool
1013
}
1114

15+
func (t MatchingIndexToken) String() string {
16+
key := rfc6901Encoder.Replace(t.Key)
17+
val := rfc6901Encoder.Replace(t.Value)
18+
19+
if t.Optional {
20+
val += "?"
21+
}
22+
23+
return fmt.Sprintf("%s=%s", key, val)
24+
}
25+
1226
func (t MatchingIndexToken) processDescent(ctx *tokenContext) (interface{}, error) {
1327
typedObj, ok := ctx.Node.([]interface{})
1428
if !ok {

patch/token_root.go

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import "errors"
44

55
type RootToken struct{}
66

7+
func (t RootToken) String() string {
8+
return ""
9+
}
10+
711
func (t RootToken) processDescent(ctx *tokenContext) (interface{}, error) {
812
return nil, errors.New("not supported")
913
}

patch/token_wildcard.go

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import "errors"
44

55
type WildcardToken struct{}
66

7+
func (t WildcardToken) String() string {
8+
return "*"
9+
}
10+
711
func (t WildcardToken) processDescent(ctx *tokenContext) (interface{}, error) {
812
if ctx.IsLast() {
913
return nil, errors.New("wildcard can't be used for last element. Use - instead")

patch/tokens.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ func (rc *tokenContext) Descend() (interface{}, error) {
3333
type Token interface {
3434
processDescent(ctx *tokenContext) (interface{}, error)
3535
createEmptyValueForNext() (interface{}, error)
36+
String() string
3637
}

0 commit comments

Comments
 (0)