-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegments.go
More file actions
163 lines (148 loc) · 3.94 KB
/
segments.go
File metadata and controls
163 lines (148 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package protopath
import "slices"
// Segments represents a parsed proto path value.
type Segments []Segment
// Parse convert the given string path to slice of segments and validates all of them.
func Parse(path string) (Segments, error) {
if len(path) == 0 {
return Segments{Segment{encoded: path, decoded: path, isValid: true}}, nil
}
ss := Segments{}
for len(path) > 0 {
s := decodeFirstPathSegment(path)
ss = append(ss, s)
if !s.IsValid() {
if len(ss) == 1 { // first segment
return ss, &ErrInvalidSegment{Encoded: s.Encoded()}
}
return ss, &ErrInPath{Path: ss.PrecedingSegments(-1).Encode(), Cause: &ErrInvalidSegment{Encoded: s.Encoded()}}
}
path = path[len(s.encoded):]
next, hasNext := nextByte(path)
if !hasNext {
continue
}
if next == '.' {
path = path[1:]
// case for paths `...foo.` that should result in segments: ..., `foo`, ``
// case for paths `...foo.[bar]...` that should result in segments: ..., `foo`, ``, `[bar]`, ...
if next, hasNext = nextByte(path); !hasNext || next == '[' {
ss = append(ss, Segment{encoded: path[0:0], decoded: "", isValid: true})
}
}
}
return ss, nil
}
// Encode encodes slice of segments into string path.
func (ss Segments) Encode() string {
if len(ss) == 0 {
return ""
}
l := 0
for _, s := range ss {
l += len(s.encoded)
}
e := append(make([]byte, 0, l), ss[0].encoded...)
for _, s := range ss[1:] {
e = joinPath(e, s.encoded)
}
return string(e)
}
// First returns the first segment in path.
func (ss Segments) First() Segment {
if len(ss) == 0 {
return Segment{}
}
return ss[0]
}
// Last returns the last segment in path.
func (ss Segments) Last() Segment {
if len(ss) == 0 {
return Segment{}
}
return ss[len(ss)-1]
}
// Index returns the n'th segment in path. Negative indexes returns segments starting from the back.
func (ss Segments) Index(n int) Segment {
if n < 0 {
n += len(ss)
}
if n < 0 || n >= len(ss) {
return Segment{}
}
return ss[n]
}
// PrecedingSegments returns a slice of segments composed of the segments before a segment with the provided index.
func (ss Segments) PrecedingSegments(n int) Segments {
if n < 0 {
n += len(ss)
}
if n < 0 || n >= len(ss) {
return nil
}
return slices.Clone(ss[:n])
}
// PrecedingSegmentsWithCurrent returns a slice of segments that includes a segment with the provided index and all segments before it.
func (ss Segments) PrecedingSegmentsWithCurrent(n int) Segments {
if n < 0 {
n += len(ss)
}
if n < 0 || n >= len(ss) {
return nil
}
return slices.Clone(ss[:n+1])
}
// FollowingSegments returns a slice of segments composed of the segments after a segment with the provided index.
func (ss Segments) FollowingSegments(n int) Segments {
if n < 0 {
n += len(ss)
}
if n < 0 || n >= len(ss) {
return nil
}
return slices.Clone(ss[n+1:])
}
// FollowingSegmentsWithCurrent returns a slice of segments that includes a segment with the provided index and all segments after it.
func (ss Segments) FollowingSegmentsWithCurrent(n int) Segments {
if n < 0 {
n += len(ss)
}
if n < 0 || n >= len(ss) {
return nil
}
return slices.Clone(ss[n:])
}
func (ss Segments) Equal(o Segments) bool {
return slices.EqualFunc(ss, o, func(i, j Segment) bool {
return i.Equal(j)
})
}
func (ss Segments) Compare(o Segments) int {
return slices.CompareFunc(ss, o, func(i, j Segment) int {
return i.Compare(j)
})
}
func joinPath(first []byte, second string) []byte {
if len(second) > 0 && second[0] == '[' { // second starts with '['
if len(first) == 0 {
return append([]byte{'[', ']'}, second...)
}
return append(first, second...)
}
return append(append(first, '.'), second...)
}
// JoinPaths create a new encoded path by concatenating all provided paths.
func JoinPaths(paths ...string) string {
if len(paths) == 0 {
return ""
}
l := 0
for _, p := range paths {
l += len(p)
}
p := append(make([]byte, 0, l), paths[0]...)
for _, o := range paths[1:] {
p = joinPath(p, o)
}
return string(p)
}