-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.go
More file actions
201 lines (185 loc) · 6.27 KB
/
path.go
File metadata and controls
201 lines (185 loc) · 6.27 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright 2025 The Go-Spring Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package flatten
import (
"fmt"
"strconv"
"strings"
"github.com/go-spring/stdlib/errutil"
)
// PathType represents the type of a path element in a hierarchical key.
// A path element can either be a key (map field) or an index (array/slice element).
type PathType int8
const (
PathTypeKey PathType = iota // A named key in a map.
PathTypeIndex // A numeric index in a list.
)
// String returns the string representation of PathType.
func (t PathType) String() string {
switch t {
case PathTypeKey:
return "key"
case PathTypeIndex:
return "index"
default:
return fmt.Sprintf("PathType(%d)", int8(t))
}
}
// Path represents a single segment in a parsed key path.
// A path is composed of multiple Path elements that can be joined or split.
// For example, "foo.bar[0]" parses into:
//
// [{Type: PathTypeKey, Elem: "foo"},
// {Type: PathTypeKey, Elem: "bar"},
// {Type: PathTypeIndex, Elem: "0"}].
type Path struct {
// Whether the element is a key or an index.
Type PathType
// Actual key or index value as a string.
// For PathTypeKey, it's the key string;
// for PathTypeIndex, it's the index number as a string.
Elem string
}
// JoinPath converts a slice of Path objects into a string representation.
// Keys are joined with dots, and array indices are wrapped in square brackets.
// Example: [key, index(0), key] => "key[0].key".
func JoinPath(path []Path) string {
var sb strings.Builder
for i, p := range path {
switch p.Type {
case PathTypeKey:
if i > 0 {
sb.WriteString(".")
}
sb.WriteString(p.Elem)
case PathTypeIndex:
sb.WriteString("[")
sb.WriteString(p.Elem)
sb.WriteString("]")
}
}
return sb.String()
}
// SplitPath parses a hierarchical key string into a slice of Path objects.
// It supports dot-notation for maps and bracket-notation for arrays.
// Examples:
//
// "foo.bar[0]" -> [{Key:"foo"}, {Key:"bar"}, {Index:"0"}]
// "a[1][2]" -> [{Key:"a"}, {Index:"1"}, {Index:"2"}]
//
// Rules:
// - Keys must be non-empty strings without spaces.
// - Indices must be unsigned integers (no sign, no decimal).
// - Empty maps/slices are not special-cased here.
// - Returns an error if the key is malformed (e.g. unbalanced brackets,
// unexpected characters, or empty keys if disallowed).
func SplitPath(key string) (_ []Path, err error) {
if key == "" {
return nil, errutil.Explain(nil, "SplitPath: invalid key: empty string")
}
var (
path []Path
lastPos int // start index of current segment
lastChar rune // previous rune seen (0 initial)
openBracket bool // whether we're inside '[' ... ']'
)
for i, c := range key {
switch c {
case ' ':
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: contains space", key, i)
case '.':
if openBracket {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: '.' not allowed inside brackets", key, i)
}
if lastChar == '.' {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: empty key between dots", key, i)
}
if lastChar != ']' {
if path, err = appendKey(path, key[lastPos:i]); err != nil {
return nil, errutil.Explain(err, "SplitPath: invalid key %q at pos %d", key, lastPos)
}
}
lastPos = i + 1
lastChar = '.'
case '[':
if openBracket {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: nested '['", key, i)
}
if lastChar == '.' {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: '[' cannot directly follow '.'", key, i)
}
if i > 0 && lastChar != ']' {
if path, err = appendKey(path, key[lastPos:i]); err != nil {
return nil, errutil.Explain(err, "SplitPath: invalid key %q at pos %d", key, lastPos)
}
}
openBracket = true
lastPos = i + 1
lastChar = '['
case ']':
if !openBracket {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: ']' without matching '['", key, i)
}
if lastPos == i {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: empty index", key, lastPos)
}
if path, err = appendIndex(path, key[lastPos:i]); err != nil {
return nil, errutil.Explain(err, "SplitPath: invalid key %q at pos %d", key, lastPos)
}
openBracket = false
lastPos = i + 1
lastChar = ']'
default:
// if previous char was ']' and now we see other char that's not '.' or '[' it's invalid:
if lastChar == ']' {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: unexpected character %q after ']'", key, i, c)
}
lastChar = c
}
}
if openBracket {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: unclosed '['", key, lastPos-1)
}
if lastChar == '.' {
return nil, errutil.Explain(nil, "SplitPath: invalid key %q at pos %d: ends with '.'", key, len(key)-1)
}
if lastChar != ']' {
if path, err = appendKey(path, key[lastPos:]); err != nil {
return nil, errutil.Explain(err, "SplitPath: invalid key %q at pos %d", key, lastPos)
}
}
return path, nil
}
// appendKey validates and appends a key segment.
func appendKey(path []Path, s string) ([]Path, error) {
if s == "" {
return nil, errutil.Explain(nil, "empty key segment")
}
if strings.ContainsRune(s, ' ') {
return nil, errutil.Explain(nil, "key segment %q contains space", s)
}
return append(path, Path{Type: PathTypeKey, Elem: s}), nil
}
// appendIndex validates and appends an index segment.
func appendIndex(path []Path, s string) ([]Path, error) {
if s == "" {
return nil, errutil.Explain(nil, "empty index")
}
if _, err := strconv.ParseUint(s, 10, 64); err != nil {
return nil, errutil.Explain(nil, "index must be an unsigned integer (got %q)", s)
}
return append(path, Path{Type: PathTypeIndex, Elem: s}), nil
}