| 1 |
package yaml |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/goccy/go-yaml/ast" |
| 11 |
"github.com/goccy/go-yaml/parser" |
| 12 |
"github.com/goccy/go-yaml/printer" |
| 13 |
) |
| 14 |
|
| 15 |
// PathString create Path from string |
| 16 |
// |
| 17 |
// YAMLPath rule |
| 18 |
// $ : the root object/element |
| 19 |
// . : child operator |
| 20 |
// .. : recursive descent |
| 21 |
// [num] : object/element of array by number |
| 22 |
// [*] : all objects/elements for array. |
| 23 |
// |
| 24 |
// If you want to use reserved characters such as `.` and `*` as a key name, |
| 25 |
// enclose them in single quotation as follows ( $.foo.'bar.baz-*'.hoge ). |
| 26 |
// If you want to use a single quote with reserved characters, escape it with `\` ( $.foo.'bar.baz\'s value'.hoge ). |
| 27 |
func PathString(s string) (*Path, error) { |
| 28 |
buf := []rune(s) |
| 29 |
length := len(buf) |
| 30 |
cursor := 0 |
| 31 |
builder := &PathBuilder{} |
| 32 |
for cursor < length { |
| 33 |
c := buf[cursor] |
| 34 |
switch c { |
| 35 |
case '$': |
| 36 |
builder = builder.Root() |
| 37 |
cursor++ |
| 38 |
case '.': |
| 39 |
b, buf, c, err := parsePathDot(builder, buf, cursor) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
length = len(buf) |
| 44 |
builder = b |
| 45 |
cursor = c |
| 46 |
case '[': |
| 47 |
b, buf, c, err := parsePathIndex(builder, buf, cursor) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
length = len(buf) |
| 52 |
builder = b |
| 53 |
cursor = c |
| 54 |
default: |
| 55 |
return nil, fmt.Errorf("invalid path at %d: %w", cursor, ErrInvalidPathString) |
| 56 |
} |
| 57 |
} |
| 58 |
return builder.Build(), nil |
| 59 |
} |
| 60 |
|
| 61 |
func parsePathRecursive(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) { |
| 62 |
length := len(buf) |
| 63 |
cursor += 2 // skip .. characters |
| 64 |
start := cursor |
| 65 |
for ; cursor < length; cursor++ { |
| 66 |
c := buf[cursor] |
| 67 |
switch c { |
| 68 |
case '$': |
| 69 |
return nil, nil, 0, fmt.Errorf("specified '$' after '..' character: %w", ErrInvalidPathString) |
| 70 |
case '*': |
| 71 |
return nil, nil, 0, fmt.Errorf("specified '*' after '..' character: %w", ErrInvalidPathString) |
| 72 |
case '.', '[': |
| 73 |
goto end |
| 74 |
case ']': |
| 75 |
return nil, nil, 0, fmt.Errorf("specified ']' after '..' character: %w", ErrInvalidPathString) |
| 76 |
} |
| 77 |
} |
| 78 |
end: |
| 79 |
if start == cursor { |
| 80 |
return nil, nil, 0, fmt.Errorf("not found recursive selector: %w", ErrInvalidPathString) |
| 81 |
} |
| 82 |
return b.Recursive(string(buf[start:cursor])), buf, cursor, nil |
| 83 |
} |
| 84 |
|
| 85 |
func parsePathDot(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) { |
| 86 |
length := len(buf) |
| 87 |
if cursor+1 < length && buf[cursor+1] == '.' { |
| 88 |
b, buf, c, err := parsePathRecursive(b, buf, cursor) |
| 89 |
if err != nil { |
| 90 |
return nil, nil, 0, err |
| 91 |
} |
| 92 |
return b, buf, c, nil |
| 93 |
} |
| 94 |
cursor++ // skip . character |
| 95 |
start := cursor |
| 96 |
|
| 97 |
// if started single quote, looking for end single quote char |
| 98 |
if cursor < length && buf[cursor] == '\'' { |
| 99 |
return parseQuotedKey(b, buf, cursor) |
| 100 |
} |
| 101 |
for ; cursor < length; cursor++ { |
| 102 |
c := buf[cursor] |
| 103 |
switch c { |
| 104 |
case '$': |
| 105 |
return nil, nil, 0, fmt.Errorf("specified '$' after '.' character: %w", ErrInvalidPathString) |
| 106 |
case '*': |
| 107 |
return nil, nil, 0, fmt.Errorf("specified '*' after '.' character: %w", ErrInvalidPathString) |
| 108 |
case '.', '[': |
| 109 |
goto end |
| 110 |
case ']': |
| 111 |
return nil, nil, 0, fmt.Errorf("specified ']' after '.' character: %w", ErrInvalidPathString) |
| 112 |
} |
| 113 |
} |
| 114 |
end: |
| 115 |
if start == cursor { |
| 116 |
return nil, nil, 0, fmt.Errorf("cloud not find by empty key: %w", ErrInvalidPathString) |
| 117 |
} |
| 118 |
return b.child(string(buf[start:cursor])), buf, cursor, nil |
| 119 |
} |
| 120 |
|
| 121 |
func parseQuotedKey(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) { |
| 122 |
cursor++ // skip single quote |
| 123 |
start := cursor |
| 124 |
length := len(buf) |
| 125 |
var foundEndDelim bool |
| 126 |
for ; cursor < length; cursor++ { |
| 127 |
switch buf[cursor] { |
| 128 |
case '\\': |
| 129 |
buf = append(append([]rune{}, buf[:cursor]...), buf[cursor+1:]...) |
| 130 |
length = len(buf) |
| 131 |
case '\'': |
| 132 |
foundEndDelim = true |
| 133 |
goto end |
| 134 |
} |
| 135 |
} |
| 136 |
end: |
| 137 |
if !foundEndDelim { |
| 138 |
return nil, nil, 0, fmt.Errorf("could not find end delimiter for key: %w", ErrInvalidPathString) |
| 139 |
} |
| 140 |
if start == cursor { |
| 141 |
return nil, nil, 0, fmt.Errorf("could not find by empty key: %w", ErrInvalidPathString) |
| 142 |
} |
| 143 |
selector := buf[start:cursor] |
| 144 |
cursor++ |
| 145 |
if cursor < length { |
| 146 |
switch buf[cursor] { |
| 147 |
case '$': |
| 148 |
return nil, nil, 0, fmt.Errorf("specified '$' after '.' character: %w", ErrInvalidPathString) |
| 149 |
case '*': |
| 150 |
return nil, nil, 0, fmt.Errorf("specified '*' after '.' character: %w", ErrInvalidPathString) |
| 151 |
case ']': |
| 152 |
return nil, nil, 0, fmt.Errorf("specified ']' after '.' character: %w", ErrInvalidPathString) |
| 153 |
} |
| 154 |
} |
| 155 |
return b.child(string(selector)), buf, cursor, nil |
| 156 |
} |
| 157 |
|
| 158 |
func parsePathIndex(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) { |
| 159 |
length := len(buf) |
| 160 |
cursor++ // skip '[' character |
| 161 |
if length <= cursor { |
| 162 |
return nil, nil, 0, fmt.Errorf("unexpected end of YAML Path: %w", ErrInvalidPathString) |
| 163 |
} |
| 164 |
c := buf[cursor] |
| 165 |
switch c { |
| 166 |
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*': |
| 167 |
start := cursor |
| 168 |
cursor++ |
| 169 |
for ; cursor < length; cursor++ { |
| 170 |
c := buf[cursor] |
| 171 |
switch c { |
| 172 |
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 173 |
continue |
| 174 |
} |
| 175 |
break |
| 176 |
} |
| 177 |
if buf[cursor] != ']' { |
| 178 |
return nil, nil, 0, fmt.Errorf("invalid character %s at %d: %w", string(buf[cursor]), cursor, ErrInvalidPathString) |
| 179 |
} |
| 180 |
numOrAll := string(buf[start:cursor]) |
| 181 |
if numOrAll == "*" { |
| 182 |
return b.IndexAll(), buf, cursor + 1, nil |
| 183 |
} |
| 184 |
num, err := strconv.ParseInt(numOrAll, 10, 64) |
| 185 |
if err != nil { |
| 186 |
return nil, nil, 0, err |
| 187 |
} |
| 188 |
return b.Index(uint(num)), buf, cursor + 1, nil |
| 189 |
} |
| 190 |
return nil, nil, 0, fmt.Errorf("invalid character %q at %d: %w", c, cursor, ErrInvalidPathString) |
| 191 |
} |
| 192 |
|
| 193 |
// Path represent YAMLPath ( like a JSONPath ). |
| 194 |
type Path struct { |
| 195 |
node pathNode |
| 196 |
} |
| 197 |
|
| 198 |
// String path to text. |
| 199 |
func (p *Path) String() string { |
| 200 |
return p.node.String() |
| 201 |
} |
| 202 |
|
| 203 |
// Read decode from r and set extracted value by YAMLPath to v. |
| 204 |
func (p *Path) Read(r io.Reader, v interface{}) error { |
| 205 |
node, err := p.ReadNode(r) |
| 206 |
if err != nil { |
| 207 |
return err |
| 208 |
} |
| 209 |
if err := Unmarshal([]byte(node.String()), v); err != nil { |
| 210 |
return err |
| 211 |
} |
| 212 |
return nil |
| 213 |
} |
| 214 |
|
| 215 |
// ReadNode create AST from r and extract node by YAMLPath. |
| 216 |
func (p *Path) ReadNode(r io.Reader) (ast.Node, error) { |
| 217 |
if p.node == nil { |
| 218 |
return nil, ErrInvalidPath |
| 219 |
} |
| 220 |
var buf bytes.Buffer |
| 221 |
if _, err := io.Copy(&buf, r); err != nil { |
| 222 |
return nil, err |
| 223 |
} |
| 224 |
f, err := parser.ParseBytes(buf.Bytes(), 0) |
| 225 |
if err != nil { |
| 226 |
return nil, err |
| 227 |
} |
| 228 |
node, err := p.FilterFile(f) |
| 229 |
if err != nil { |
| 230 |
return nil, err |
| 231 |
} |
| 232 |
return node, nil |
| 233 |
} |
| 234 |
|
| 235 |
// Filter filter from target by YAMLPath and set it to v. |
| 236 |
func (p *Path) Filter(target, v interface{}) error { |
| 237 |
b, err := Marshal(target) |
| 238 |
if err != nil { |
| 239 |
return err |
| 240 |
} |
| 241 |
if err := p.Read(bytes.NewBuffer(b), v); err != nil { |
| 242 |
return err |
| 243 |
} |
| 244 |
return nil |
| 245 |
} |
| 246 |
|
| 247 |
// FilterFile filter from ast.File by YAMLPath. |
| 248 |
func (p *Path) FilterFile(f *ast.File) (ast.Node, error) { |
| 249 |
for _, doc := range f.Docs { |
| 250 |
node, err := p.FilterNode(doc.Body) |
| 251 |
if err != nil { |
| 252 |
return nil, err |
| 253 |
} |
| 254 |
if node != nil { |
| 255 |
return node, nil |
| 256 |
} |
| 257 |
} |
| 258 |
return nil, fmt.Errorf("failed to find path ( %s ): %w", p.node, ErrNotFoundNode) |
| 259 |
} |
| 260 |
|
| 261 |
// FilterNode filter from node by YAMLPath. |
| 262 |
func (p *Path) FilterNode(node ast.Node) (ast.Node, error) { |
| 263 |
n, err := p.node.filter(node) |
| 264 |
if err != nil { |
| 265 |
return nil, err |
| 266 |
} |
| 267 |
return n, nil |
| 268 |
} |
| 269 |
|
| 270 |
// MergeFromReader merge YAML text into ast.File. |
| 271 |
func (p *Path) MergeFromReader(dst *ast.File, src io.Reader) error { |
| 272 |
var buf bytes.Buffer |
| 273 |
if _, err := io.Copy(&buf, src); err != nil { |
| 274 |
return err |
| 275 |
} |
| 276 |
file, err := parser.ParseBytes(buf.Bytes(), 0) |
| 277 |
if err != nil { |
| 278 |
return err |
| 279 |
} |
| 280 |
if err := p.MergeFromFile(dst, file); err != nil { |
| 281 |
return err |
| 282 |
} |
| 283 |
return nil |
| 284 |
} |
| 285 |
|
| 286 |
// MergeFromFile merge ast.File into ast.File. |
| 287 |
func (p *Path) MergeFromFile(dst *ast.File, src *ast.File) error { |
| 288 |
base, err := p.FilterFile(dst) |
| 289 |
if err != nil { |
| 290 |
return err |
| 291 |
} |
| 292 |
for _, doc := range src.Docs { |
| 293 |
if err := ast.Merge(base, doc); err != nil { |
| 294 |
return err |
| 295 |
} |
| 296 |
} |
| 297 |
return nil |
| 298 |
} |
| 299 |
|
| 300 |
// MergeFromNode merge ast.Node into ast.File. |
| 301 |
func (p *Path) MergeFromNode(dst *ast.File, src ast.Node) error { |
| 302 |
base, err := p.FilterFile(dst) |
| 303 |
if err != nil { |
| 304 |
return err |
| 305 |
} |
| 306 |
if err := ast.Merge(base, src); err != nil { |
| 307 |
return err |
| 308 |
} |
| 309 |
return nil |
| 310 |
} |
| 311 |
|
| 312 |
// ReplaceWithReader replace ast.File with io.Reader. |
| 313 |
func (p *Path) ReplaceWithReader(dst *ast.File, src io.Reader) error { |
| 314 |
var buf bytes.Buffer |
| 315 |
if _, err := io.Copy(&buf, src); err != nil { |
| 316 |
return err |
| 317 |
} |
| 318 |
file, err := parser.ParseBytes(buf.Bytes(), 0) |
| 319 |
if err != nil { |
| 320 |
return err |
| 321 |
} |
| 322 |
if err := p.ReplaceWithFile(dst, file); err != nil { |
| 323 |
return err |
| 324 |
} |
| 325 |
return nil |
| 326 |
} |
| 327 |
|
| 328 |
// ReplaceWithFile replace ast.File with ast.File. |
| 329 |
func (p *Path) ReplaceWithFile(dst *ast.File, src *ast.File) error { |
| 330 |
for _, doc := range src.Docs { |
| 331 |
if err := p.ReplaceWithNode(dst, doc); err != nil { |
| 332 |
return err |
| 333 |
} |
| 334 |
} |
| 335 |
return nil |
| 336 |
} |
| 337 |
|
| 338 |
// ReplaceNode replace ast.File with ast.Node. |
| 339 |
func (p *Path) ReplaceWithNode(dst *ast.File, node ast.Node) error { |
| 340 |
for _, doc := range dst.Docs { |
| 341 |
if node.Type() == ast.DocumentType { |
| 342 |
node = node.(*ast.DocumentNode).Body |
| 343 |
} |
| 344 |
if err := p.node.replace(doc.Body, node); err != nil { |
| 345 |
return err |
| 346 |
} |
| 347 |
} |
| 348 |
return nil |
| 349 |
} |
| 350 |
|
| 351 |
// AnnotateSource add annotation to passed source ( see section 5.1 in README.md ). |
| 352 |
func (p *Path) AnnotateSource(source []byte, colored bool) ([]byte, error) { |
| 353 |
file, err := parser.ParseBytes([]byte(source), 0) |
| 354 |
if err != nil { |
| 355 |
return nil, err |
| 356 |
} |
| 357 |
node, err := p.FilterFile(file) |
| 358 |
if err != nil { |
| 359 |
return nil, err |
| 360 |
} |
| 361 |
var pp printer.Printer |
| 362 |
return []byte(pp.PrintErrorToken(node.GetToken(), colored)), nil |
| 363 |
} |
| 364 |
|
| 365 |
// PathBuilder represent builder for YAMLPath. |
| 366 |
type PathBuilder struct { |
| 367 |
root *rootNode |
| 368 |
node pathNode |
| 369 |
} |
| 370 |
|
| 371 |
// Root add '$' to current path. |
| 372 |
func (b *PathBuilder) Root() *PathBuilder { |
| 373 |
root := newRootNode() |
| 374 |
return &PathBuilder{root: root, node: root} |
| 375 |
} |
| 376 |
|
| 377 |
// IndexAll add '[*]' to current path. |
| 378 |
func (b *PathBuilder) IndexAll() *PathBuilder { |
| 379 |
b.node = b.node.chain(newIndexAllNode()) |
| 380 |
return b |
| 381 |
} |
| 382 |
|
| 383 |
// Recursive add '..selector' to current path. |
| 384 |
func (b *PathBuilder) Recursive(selector string) *PathBuilder { |
| 385 |
b.node = b.node.chain(newRecursiveNode(selector)) |
| 386 |
return b |
| 387 |
} |
| 388 |
|
| 389 |
func (b *PathBuilder) containsReservedPathCharacters(path string) bool { |
| 390 |
if strings.Contains(path, ".") { |
| 391 |
return true |
| 392 |
} |
| 393 |
if strings.Contains(path, "*") { |
| 394 |
return true |
| 395 |
} |
| 396 |
return false |
| 397 |
} |
| 398 |
|
| 399 |
func (b *PathBuilder) enclosedSingleQuote(name string) bool { |
| 400 |
return strings.HasPrefix(name, "'") && strings.HasSuffix(name, "'") |
| 401 |
} |
| 402 |
|
| 403 |
func (b *PathBuilder) normalizeSelectorName(name string) string { |
| 404 |
if b.enclosedSingleQuote(name) { |
| 405 |
// already escaped name |
| 406 |
return name |
| 407 |
} |
| 408 |
if b.containsReservedPathCharacters(name) { |
| 409 |
escapedName := strings.ReplaceAll(name, `'`, `\'`) |
| 410 |
return "'" + escapedName + "'" |
| 411 |
} |
| 412 |
return name |
| 413 |
} |
| 414 |
|
| 415 |
func (b *PathBuilder) child(name string) *PathBuilder { |
| 416 |
b.node = b.node.chain(newSelectorNode(name)) |
| 417 |
return b |
| 418 |
} |
| 419 |
|
| 420 |
// Child add '.name' to current path. |
| 421 |
func (b *PathBuilder) Child(name string) *PathBuilder { |
| 422 |
return b.child(b.normalizeSelectorName(name)) |
| 423 |
} |
| 424 |
|
| 425 |
// Index add '[idx]' to current path. |
| 426 |
func (b *PathBuilder) Index(idx uint) *PathBuilder { |
| 427 |
b.node = b.node.chain(newIndexNode(idx)) |
| 428 |
return b |
| 429 |
} |
| 430 |
|
| 431 |
// Build build YAMLPath. |
| 432 |
func (b *PathBuilder) Build() *Path { |
| 433 |
return &Path{node: b.root} |
| 434 |
} |
| 435 |
|
| 436 |
type pathNode interface { |
| 437 |
fmt.Stringer |
| 438 |
chain(pathNode) pathNode |
| 439 |
filter(ast.Node) (ast.Node, error) |
| 440 |
replace(ast.Node, ast.Node) error |
| 441 |
} |
| 442 |
|
| 443 |
type basePathNode struct { |
| 444 |
child pathNode |
| 445 |
} |
| 446 |
|
| 447 |
func (n *basePathNode) chain(node pathNode) pathNode { |
| 448 |
n.child = node |
| 449 |
return node |
| 450 |
} |
| 451 |
|
| 452 |
type rootNode struct { |
| 453 |
*basePathNode |
| 454 |
} |
| 455 |
|
| 456 |
func newRootNode() *rootNode { |
| 457 |
return &rootNode{basePathNode: &basePathNode{}} |
| 458 |
} |
| 459 |
|
| 460 |
func (n *rootNode) String() string { |
| 461 |
s := "$" |
| 462 |
if n.child != nil { |
| 463 |
s += n.child.String() |
| 464 |
} |
| 465 |
return s |
| 466 |
} |
| 467 |
|
| 468 |
func (n *rootNode) filter(node ast.Node) (ast.Node, error) { |
| 469 |
if n.child == nil { |
| 470 |
return node, nil |
| 471 |
} |
| 472 |
filtered, err := n.child.filter(node) |
| 473 |
if err != nil { |
| 474 |
return nil, err |
| 475 |
} |
| 476 |
return filtered, nil |
| 477 |
} |
| 478 |
|
| 479 |
func (n *rootNode) replace(node ast.Node, target ast.Node) error { |
| 480 |
if n.child == nil { |
| 481 |
return nil |
| 482 |
} |
| 483 |
if err := n.child.replace(node, target); err != nil { |
| 484 |
return err |
| 485 |
} |
| 486 |
return nil |
| 487 |
} |
| 488 |
|
| 489 |
type selectorNode struct { |
| 490 |
*basePathNode |
| 491 |
selector string |
| 492 |
} |
| 493 |
|
| 494 |
func newSelectorNode(selector string) *selectorNode { |
| 495 |
return &selectorNode{ |
| 496 |
basePathNode: &basePathNode{}, |
| 497 |
selector: selector, |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
func (n *selectorNode) filter(node ast.Node) (ast.Node, error) { |
| 502 |
selector := n.selector |
| 503 |
if len(selector) > 1 && selector[0] == '\'' && selector[len(selector)-1] == '\'' { |
| 504 |
selector = selector[1 : len(selector)-1] |
| 505 |
} |
| 506 |
switch node.Type() { |
| 507 |
case ast.MappingType: |
| 508 |
for _, value := range node.(*ast.MappingNode).Values { |
| 509 |
key := value.Key.GetToken().Value |
| 510 |
if len(key) > 0 { |
| 511 |
switch key[0] { |
| 512 |
case '"': |
| 513 |
var err error |
| 514 |
key, err = strconv.Unquote(key) |
| 515 |
if err != nil { |
| 516 |
return nil, err |
| 517 |
} |
| 518 |
case '\'': |
| 519 |
if len(key) > 1 && key[len(key)-1] == '\'' { |
| 520 |
key = key[1 : len(key)-1] |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
if key == selector { |
| 525 |
if n.child == nil { |
| 526 |
return value.Value, nil |
| 527 |
} |
| 528 |
filtered, err := n.child.filter(value.Value) |
| 529 |
if err != nil { |
| 530 |
return nil, err |
| 531 |
} |
| 532 |
return filtered, nil |
| 533 |
} |
| 534 |
} |
| 535 |
case ast.MappingValueType: |
| 536 |
value, _ := node.(*ast.MappingValueNode) |
| 537 |
key := value.Key.GetToken().Value |
| 538 |
if key == selector { |
| 539 |
if n.child == nil { |
| 540 |
return value.Value, nil |
| 541 |
} |
| 542 |
filtered, err := n.child.filter(value.Value) |
| 543 |
if err != nil { |
| 544 |
return nil, err |
| 545 |
} |
| 546 |
return filtered, nil |
| 547 |
} |
| 548 |
default: |
| 549 |
return nil, fmt.Errorf("expected node type is map or map value. but got %s: %w", node.Type(), ErrInvalidQuery) |
| 550 |
} |
| 551 |
return nil, nil |
| 552 |
} |
| 553 |
|
| 554 |
func (n *selectorNode) replaceMapValue(value *ast.MappingValueNode, target ast.Node) error { |
| 555 |
key := value.Key.GetToken().Value |
| 556 |
if key != n.selector { |
| 557 |
return nil |
| 558 |
} |
| 559 |
if n.child == nil { |
| 560 |
if err := value.Replace(target); err != nil { |
| 561 |
return err |
| 562 |
} |
| 563 |
} else { |
| 564 |
if err := n.child.replace(value.Value, target); err != nil { |
| 565 |
return err |
| 566 |
} |
| 567 |
} |
| 568 |
return nil |
| 569 |
} |
| 570 |
|
| 571 |
func (n *selectorNode) replace(node ast.Node, target ast.Node) error { |
| 572 |
switch node.Type() { |
| 573 |
case ast.MappingType: |
| 574 |
for _, value := range node.(*ast.MappingNode).Values { |
| 575 |
if err := n.replaceMapValue(value, target); err != nil { |
| 576 |
return err |
| 577 |
} |
| 578 |
} |
| 579 |
case ast.MappingValueType: |
| 580 |
value, _ := node.(*ast.MappingValueNode) |
| 581 |
if err := n.replaceMapValue(value, target); err != nil { |
| 582 |
return err |
| 583 |
} |
| 584 |
default: |
| 585 |
return fmt.Errorf("expected node type is map or map value. but got %s: %w", node.Type(), ErrInvalidQuery) |
| 586 |
} |
| 587 |
return nil |
| 588 |
} |
| 589 |
|
| 590 |
func (n *selectorNode) String() string { |
| 591 |
var builder PathBuilder |
| 592 |
selector := builder.normalizeSelectorName(n.selector) |
| 593 |
s := fmt.Sprintf(".%s", selector) |
| 594 |
if n.child != nil { |
| 595 |
s += n.child.String() |
| 596 |
} |
| 597 |
return s |
| 598 |
} |
| 599 |
|
| 600 |
type indexNode struct { |
| 601 |
*basePathNode |
| 602 |
selector uint |
| 603 |
} |
| 604 |
|
| 605 |
func newIndexNode(selector uint) *indexNode { |
| 606 |
return &indexNode{ |
| 607 |
basePathNode: &basePathNode{}, |
| 608 |
selector: selector, |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
func (n *indexNode) filter(node ast.Node) (ast.Node, error) { |
| 613 |
if node.Type() != ast.SequenceType { |
| 614 |
return nil, fmt.Errorf("expected sequence type node. but got %s: %w", node.Type(), ErrInvalidQuery) |
| 615 |
} |
| 616 |
sequence, _ := node.(*ast.SequenceNode) |
| 617 |
if n.selector >= uint(len(sequence.Values)) { |
| 618 |
return nil, fmt.Errorf("expected index is %d. but got sequences has %d items: %w", n.selector, sequence.Values, ErrInvalidQuery) |
| 619 |
} |
| 620 |
value := sequence.Values[n.selector] |
| 621 |
if n.child == nil { |
| 622 |
return value, nil |
| 623 |
} |
| 624 |
filtered, err := n.child.filter(value) |
| 625 |
if err != nil { |
| 626 |
return nil, err |
| 627 |
} |
| 628 |
return filtered, nil |
| 629 |
} |
| 630 |
|
| 631 |
func (n *indexNode) replace(node ast.Node, target ast.Node) error { |
| 632 |
if node.Type() != ast.SequenceType { |
| 633 |
return fmt.Errorf("expected sequence type node. but got %s: %w", node.Type(), ErrInvalidQuery) |
| 634 |
} |
| 635 |
sequence, _ := node.(*ast.SequenceNode) |
| 636 |
if n.selector >= uint(len(sequence.Values)) { |
| 637 |
return fmt.Errorf("expected index is %d. but got sequences has %d items: %w", n.selector, sequence.Values, ErrInvalidQuery) |
| 638 |
} |
| 639 |
if n.child == nil { |
| 640 |
if err := sequence.Replace(int(n.selector), target); err != nil { |
| 641 |
return err |
| 642 |
} |
| 643 |
return nil |
| 644 |
} |
| 645 |
if err := n.child.replace(sequence.Values[n.selector], target); err != nil { |
| 646 |
return err |
| 647 |
} |
| 648 |
return nil |
| 649 |
} |
| 650 |
|
| 651 |
func (n *indexNode) String() string { |
| 652 |
s := fmt.Sprintf("[%d]", n.selector) |
| 653 |
if n.child != nil { |
| 654 |
s += n.child.String() |
| 655 |
} |
| 656 |
return s |
| 657 |
} |
| 658 |
|
| 659 |
type indexAllNode struct { |
| 660 |
*basePathNode |
| 661 |
} |
| 662 |
|
| 663 |
func newIndexAllNode() *indexAllNode { |
| 664 |
return &indexAllNode{ |
| 665 |
basePathNode: &basePathNode{}, |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
func (n *indexAllNode) String() string { |
| 670 |
s := "[*]" |
| 671 |
if n.child != nil { |
| 672 |
s += n.child.String() |
| 673 |
} |
| 674 |
return s |
| 675 |
} |
| 676 |
|
| 677 |
func (n *indexAllNode) filter(node ast.Node) (ast.Node, error) { |
| 678 |
if node.Type() != ast.SequenceType { |
| 679 |
return nil, fmt.Errorf("expected sequence type node. but got %s: %w", node.Type(), ErrInvalidQuery) |
| 680 |
} |
| 681 |
sequence, _ := node.(*ast.SequenceNode) |
| 682 |
if n.child == nil { |
| 683 |
return sequence, nil |
| 684 |
} |
| 685 |
out := *sequence |
| 686 |
out.Values = []ast.Node{} |
| 687 |
for _, value := range sequence.Values { |
| 688 |
filtered, err := n.child.filter(value) |
| 689 |
if err != nil { |
| 690 |
return nil, err |
| 691 |
} |
| 692 |
out.Values = append(out.Values, filtered) |
| 693 |
} |
| 694 |
return &out, nil |
| 695 |
} |
| 696 |
|
| 697 |
func (n *indexAllNode) replace(node ast.Node, target ast.Node) error { |
| 698 |
if node.Type() != ast.SequenceType { |
| 699 |
return fmt.Errorf("expected sequence type node. but got %s: %w", node.Type(), ErrInvalidQuery) |
| 700 |
} |
| 701 |
sequence, _ := node.(*ast.SequenceNode) |
| 702 |
if n.child == nil { |
| 703 |
for idx := range sequence.Values { |
| 704 |
if err := sequence.Replace(idx, target); err != nil { |
| 705 |
return err |
| 706 |
} |
| 707 |
} |
| 708 |
return nil |
| 709 |
} |
| 710 |
for _, value := range sequence.Values { |
| 711 |
if err := n.child.replace(value, target); err != nil { |
| 712 |
return err |
| 713 |
} |
| 714 |
} |
| 715 |
return nil |
| 716 |
} |
| 717 |
|
| 718 |
type recursiveNode struct { |
| 719 |
*basePathNode |
| 720 |
selector string |
| 721 |
} |
| 722 |
|
| 723 |
func newRecursiveNode(selector string) *recursiveNode { |
| 724 |
return &recursiveNode{ |
| 725 |
basePathNode: &basePathNode{}, |
| 726 |
selector: selector, |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
func (n *recursiveNode) String() string { |
| 731 |
s := fmt.Sprintf("..%s", n.selector) |
| 732 |
if n.child != nil { |
| 733 |
s += n.child.String() |
| 734 |
} |
| 735 |
return s |
| 736 |
} |
| 737 |
|
| 738 |
func (n *recursiveNode) filterNode(node ast.Node) (*ast.SequenceNode, error) { |
| 739 |
sequence := &ast.SequenceNode{BaseNode: &ast.BaseNode{}} |
| 740 |
switch typedNode := node.(type) { |
| 741 |
case *ast.MappingNode: |
| 742 |
for _, value := range typedNode.Values { |
| 743 |
seq, err := n.filterNode(value) |
| 744 |
if err != nil { |
| 745 |
return nil, err |
| 746 |
} |
| 747 |
sequence.Values = append(sequence.Values, seq.Values...) |
| 748 |
} |
| 749 |
case *ast.MappingValueNode: |
| 750 |
key := typedNode.Key.GetToken().Value |
| 751 |
if n.selector == key { |
| 752 |
sequence.Values = append(sequence.Values, typedNode.Value) |
| 753 |
} |
| 754 |
seq, err := n.filterNode(typedNode.Value) |
| 755 |
if err != nil { |
| 756 |
return nil, err |
| 757 |
} |
| 758 |
sequence.Values = append(sequence.Values, seq.Values...) |
| 759 |
case *ast.SequenceNode: |
| 760 |
for _, value := range typedNode.Values { |
| 761 |
seq, err := n.filterNode(value) |
| 762 |
if err != nil { |
| 763 |
return nil, err |
| 764 |
} |
| 765 |
sequence.Values = append(sequence.Values, seq.Values...) |
| 766 |
} |
| 767 |
} |
| 768 |
return sequence, nil |
| 769 |
} |
| 770 |
|
| 771 |
func (n *recursiveNode) filter(node ast.Node) (ast.Node, error) { |
| 772 |
sequence, err := n.filterNode(node) |
| 773 |
if err != nil { |
| 774 |
return nil, err |
| 775 |
} |
| 776 |
sequence.Start = node.GetToken() |
| 777 |
return sequence, nil |
| 778 |
} |
| 779 |
|
| 780 |
func (n *recursiveNode) replaceNode(node ast.Node, target ast.Node) error { |
| 781 |
switch typedNode := node.(type) { |
| 782 |
case *ast.MappingNode: |
| 783 |
for _, value := range typedNode.Values { |
| 784 |
if err := n.replaceNode(value, target); err != nil { |
| 785 |
return err |
| 786 |
} |
| 787 |
} |
| 788 |
case *ast.MappingValueNode: |
| 789 |
key := typedNode.Key.GetToken().Value |
| 790 |
if n.selector == key { |
| 791 |
if err := typedNode.Replace(target); err != nil { |
| 792 |
return err |
| 793 |
} |
| 794 |
} |
| 795 |
if err := n.replaceNode(typedNode.Value, target); err != nil { |
| 796 |
return err |
| 797 |
} |
| 798 |
case *ast.SequenceNode: |
| 799 |
for _, value := range typedNode.Values { |
| 800 |
if err := n.replaceNode(value, target); err != nil { |
| 801 |
return err |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
return nil |
| 806 |
} |
| 807 |
|
| 808 |
func (n *recursiveNode) replace(node ast.Node, target ast.Node) error { |
| 809 |
if err := n.replaceNode(node, target); err != nil { |
| 810 |
return err |
| 811 |
} |
| 812 |
return nil |
| 813 |
} |