| 1 | package ast |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "math" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/goccy/go-yaml/token" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | ErrInvalidTokenType = errors.New("invalid token type") |
| 16 | ErrInvalidAnchorName = errors.New("invalid anchor name") |
| 17 | ErrInvalidAliasName = errors.New("invalid alias name") |
| 18 | ) |
| 19 | |
| 20 | // NodeType type identifier of node |
| 21 | type NodeType int |
| 22 | |
| 23 | const ( |
| 24 | // UnknownNodeType type identifier for default |
| 25 | UnknownNodeType NodeType = iota |
| 26 | // DocumentType type identifier for document node |
| 27 | DocumentType |
| 28 | // NullType type identifier for null node |
| 29 | NullType |
| 30 | // BoolType type identifier for boolean node |
| 31 | BoolType |
| 32 | // IntegerType type identifier for integer node |
| 33 | IntegerType |
| 34 | // FloatType type identifier for float node |
| 35 | FloatType |
| 36 | // InfinityType type identifier for infinity node |
| 37 | InfinityType |
| 38 | // NanType type identifier for nan node |
| 39 | NanType |
| 40 | // StringType type identifier for string node |
| 41 | StringType |
| 42 | // MergeKeyType type identifier for merge key node |
| 43 | MergeKeyType |
| 44 | // LiteralType type identifier for literal node |
| 45 | LiteralType |
| 46 | // MappingType type identifier for mapping node |
| 47 | MappingType |
| 48 | // MappingKeyType type identifier for mapping key node |
| 49 | MappingKeyType |
| 50 | // MappingValueType type identifier for mapping value node |
| 51 | MappingValueType |
| 52 | // SequenceType type identifier for sequence node |
| 53 | SequenceType |
| 54 | // SequenceEntryType type identifier for sequence entry node |
| 55 | SequenceEntryType |
| 56 | // AnchorType type identifier for anchor node |
| 57 | AnchorType |
| 58 | // AliasType type identifier for alias node |
| 59 | AliasType |
| 60 | // DirectiveType type identifier for directive node |
| 61 | DirectiveType |
| 62 | // TagType type identifier for tag node |
| 63 | TagType |
| 64 | // CommentType type identifier for comment node |
| 65 | CommentType |
| 66 | // CommentGroupType type identifier for comment group node |
| 67 | CommentGroupType |
| 68 | ) |
| 69 | |
| 70 | // String node type identifier to text |
| 71 | func (t NodeType) String() string { |
| 72 | switch t { |
| 73 | case UnknownNodeType: |
| 74 | return "UnknownNode" |
| 75 | case DocumentType: |
| 76 | return "Document" |
| 77 | case NullType: |
| 78 | return "Null" |
| 79 | case BoolType: |
| 80 | return "Bool" |
| 81 | case IntegerType: |
| 82 | return "Integer" |
| 83 | case FloatType: |
| 84 | return "Float" |
| 85 | case InfinityType: |
| 86 | return "Infinity" |
| 87 | case NanType: |
| 88 | return "Nan" |
| 89 | case StringType: |
| 90 | return "String" |
| 91 | case MergeKeyType: |
| 92 | return "MergeKey" |
| 93 | case LiteralType: |
| 94 | return "Literal" |
| 95 | case MappingType: |
| 96 | return "Mapping" |
| 97 | case MappingKeyType: |
| 98 | return "MappingKey" |
| 99 | case MappingValueType: |
| 100 | return "MappingValue" |
| 101 | case SequenceType: |
| 102 | return "Sequence" |
| 103 | case SequenceEntryType: |
| 104 | return "SequenceEntry" |
| 105 | case AnchorType: |
| 106 | return "Anchor" |
| 107 | case AliasType: |
| 108 | return "Alias" |
| 109 | case DirectiveType: |
| 110 | return "Directive" |
| 111 | case TagType: |
| 112 | return "Tag" |
| 113 | case CommentType: |
| 114 | return "Comment" |
| 115 | case CommentGroupType: |
| 116 | return "CommentGroup" |
| 117 | } |
| 118 | return "" |
| 119 | } |
| 120 | |
| 121 | // String node type identifier to YAML Structure name |
| 122 | // based on https://yaml.org/spec/1.2/spec.html |
| 123 | func (t NodeType) YAMLName() string { |
| 124 | switch t { |
| 125 | case UnknownNodeType: |
| 126 | return "unknown" |
| 127 | case DocumentType: |
| 128 | return "document" |
| 129 | case NullType: |
| 130 | return "null" |
| 131 | case BoolType: |
| 132 | return "boolean" |
| 133 | case IntegerType: |
| 134 | return "int" |
| 135 | case FloatType: |
| 136 | return "float" |
| 137 | case InfinityType: |
| 138 | return "inf" |
| 139 | case NanType: |
| 140 | return "nan" |
| 141 | case StringType: |
| 142 | return "string" |
| 143 | case MergeKeyType: |
| 144 | return "merge key" |
| 145 | case LiteralType: |
| 146 | return "scalar" |
| 147 | case MappingType: |
| 148 | return "mapping" |
| 149 | case MappingKeyType: |
| 150 | return "key" |
| 151 | case MappingValueType: |
| 152 | return "value" |
| 153 | case SequenceType: |
| 154 | return "sequence" |
| 155 | case SequenceEntryType: |
| 156 | return "value" |
| 157 | case AnchorType: |
| 158 | return "anchor" |
| 159 | case AliasType: |
| 160 | return "alias" |
| 161 | case DirectiveType: |
| 162 | return "directive" |
| 163 | case TagType: |
| 164 | return "tag" |
| 165 | case CommentType: |
| 166 | return "comment" |
| 167 | case CommentGroupType: |
| 168 | return "comment" |
| 169 | } |
| 170 | return "" |
| 171 | } |
| 172 | |
| 173 | // Node type of node |
| 174 | type Node interface { |
| 175 | io.Reader |
| 176 | // String node to text |
| 177 | String() string |
| 178 | // GetToken returns token instance |
| 179 | GetToken() *token.Token |
| 180 | // Type returns type of node |
| 181 | Type() NodeType |
| 182 | // AddColumn add column number to child nodes recursively |
| 183 | AddColumn(int) |
| 184 | // SetComment set comment token to node |
| 185 | SetComment(*CommentGroupNode) error |
| 186 | // Comment returns comment token instance |
| 187 | GetComment() *CommentGroupNode |
| 188 | // GetPath returns YAMLPath for the current node |
| 189 | GetPath() string |
| 190 | // SetPath set YAMLPath for the current node |
| 191 | SetPath(string) |
| 192 | // MarshalYAML |
| 193 | MarshalYAML() ([]byte, error) |
| 194 | // already read length |
| 195 | readLen() int |
| 196 | // append read length |
| 197 | addReadLen(int) |
| 198 | // clean read length |
| 199 | clearLen() |
| 200 | } |
| 201 | |
| 202 | // MapKeyNode type for map key node |
| 203 | type MapKeyNode interface { |
| 204 | Node |
| 205 | IsMergeKey() bool |
| 206 | // String node to text without comment |
| 207 | stringWithoutComment() string |
| 208 | } |
| 209 | |
| 210 | // ScalarNode type for scalar node |
| 211 | type ScalarNode interface { |
| 212 | MapKeyNode |
| 213 | GetValue() interface{} |
| 214 | } |
| 215 | |
| 216 | type BaseNode struct { |
| 217 | Path string |
| 218 | Comment *CommentGroupNode |
| 219 | read int |
| 220 | } |
| 221 | |
| 222 | func addCommentString(base string, node *CommentGroupNode) string { |
| 223 | return fmt.Sprintf("%s %s", base, node.String()) |
| 224 | } |
| 225 | |
| 226 | func (n *BaseNode) readLen() int { |
| 227 | return n.read |
| 228 | } |
| 229 | |
| 230 | func (n *BaseNode) clearLen() { |
| 231 | n.read = 0 |
| 232 | } |
| 233 | |
| 234 | func (n *BaseNode) addReadLen(len int) { |
| 235 | n.read += len |
| 236 | } |
| 237 | |
| 238 | // GetPath returns YAMLPath for the current node. |
| 239 | func (n *BaseNode) GetPath() string { |
| 240 | if n == nil { |
| 241 | return "" |
| 242 | } |
| 243 | return n.Path |
| 244 | } |
| 245 | |
| 246 | // SetPath set YAMLPath for the current node. |
| 247 | func (n *BaseNode) SetPath(path string) { |
| 248 | if n == nil { |
| 249 | return |
| 250 | } |
| 251 | n.Path = path |
| 252 | } |
| 253 | |
| 254 | // GetComment returns comment token instance |
| 255 | func (n *BaseNode) GetComment() *CommentGroupNode { |
| 256 | return n.Comment |
| 257 | } |
| 258 | |
| 259 | // SetComment set comment token |
| 260 | func (n *BaseNode) SetComment(node *CommentGroupNode) error { |
| 261 | n.Comment = node |
| 262 | return nil |
| 263 | } |
| 264 | |
| 265 | func min(a, b int) int { |
| 266 | if a < b { |
| 267 | return a |
| 268 | } |
| 269 | return b |
| 270 | } |
| 271 | |
| 272 | func readNode(p []byte, node Node) (int, error) { |
| 273 | s := node.String() |
| 274 | readLen := node.readLen() |
| 275 | remain := len(s) - readLen |
| 276 | if remain == 0 { |
| 277 | node.clearLen() |
| 278 | return 0, io.EOF |
| 279 | } |
| 280 | size := min(remain, len(p)) |
| 281 | for idx, b := range []byte(s[readLen : readLen+size]) { |
| 282 | p[idx] = byte(b) |
| 283 | } |
| 284 | node.addReadLen(size) |
| 285 | return size, nil |
| 286 | } |
| 287 | |
| 288 | func checkLineBreak(t *token.Token) bool { |
| 289 | if t.Prev != nil { |
| 290 | lbc := "\n" |
| 291 | prev := t.Prev |
| 292 | var adjustment int |
| 293 | // if the previous type is sequence entry use the previous type for that |
| 294 | if prev.Type == token.SequenceEntryType { |
| 295 | // as well as switching to previous type count any new lines in origin to account for: |
| 296 | // - |
| 297 | // b: c |
| 298 | adjustment = strings.Count(strings.TrimRight(t.Origin, lbc), lbc) |
| 299 | if prev.Prev != nil { |
| 300 | prev = prev.Prev |
| 301 | } |
| 302 | } |
| 303 | lineDiff := t.Position.Line - prev.Position.Line - 1 |
| 304 | if lineDiff > 0 { |
| 305 | if prev.Type == token.StringType { |
| 306 | // Remove any line breaks included in multiline string |
| 307 | adjustment += strings.Count(strings.TrimRight(strings.TrimSpace(prev.Origin), lbc), lbc) |
| 308 | } |
| 309 | // Due to the way that comment parsing works its assumed that when a null value does not have new line in origin |
| 310 | // it was squashed therefore difference is ignored. |
| 311 | // foo: |
| 312 | // bar: |
| 313 | // # comment |
| 314 | // baz: 1 |
| 315 | // becomes |
| 316 | // foo: |
| 317 | // bar: null # comment |
| 318 | // |
| 319 | // baz: 1 |
| 320 | if prev.Type == token.NullType || prev.Type == token.ImplicitNullType { |
| 321 | return strings.Count(prev.Origin, lbc) > 0 |
| 322 | } |
| 323 | if lineDiff-adjustment > 0 { |
| 324 | return true |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | return false |
| 329 | } |
| 330 | |
| 331 | // Null create node for null value |
| 332 | func Null(tk *token.Token) *NullNode { |
| 333 | return &NullNode{ |
| 334 | BaseNode: &BaseNode{}, |
| 335 | Token: tk, |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // Bool create node for boolean value |
| 340 | func Bool(tk *token.Token) *BoolNode { |
| 341 | b, _ := strconv.ParseBool(tk.Value) |
| 342 | return &BoolNode{ |
| 343 | BaseNode: &BaseNode{}, |
| 344 | Token: tk, |
| 345 | Value: b, |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Integer create node for integer value |
| 350 | func Integer(tk *token.Token) *IntegerNode { |
| 351 | var v any |
| 352 | if num := token.ToNumber(tk.Value); num != nil { |
| 353 | v = num.Value |
| 354 | } |
| 355 | return &IntegerNode{ |
| 356 | BaseNode: &BaseNode{}, |
| 357 | Token: tk, |
| 358 | Value: v, |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // Float create node for float value |
| 363 | func Float(tk *token.Token) *FloatNode { |
| 364 | var v float64 |
| 365 | if num := token.ToNumber(tk.Value); num != nil && num.Type == token.NumberTypeFloat { |
| 366 | value, ok := num.Value.(float64) |
| 367 | if ok { |
| 368 | v = value |
| 369 | } |
| 370 | } |
| 371 | return &FloatNode{ |
| 372 | BaseNode: &BaseNode{}, |
| 373 | Token: tk, |
| 374 | Value: v, |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Infinity create node for .inf or -.inf value |
| 379 | func Infinity(tk *token.Token) *InfinityNode { |
| 380 | node := &InfinityNode{ |
| 381 | BaseNode: &BaseNode{}, |
| 382 | Token: tk, |
| 383 | } |
| 384 | switch tk.Value { |
| 385 | case ".inf", ".Inf", ".INF": |
| 386 | node.Value = math.Inf(0) |
| 387 | case "-.inf", "-.Inf", "-.INF": |
| 388 | node.Value = math.Inf(-1) |
| 389 | } |
| 390 | return node |
| 391 | } |
| 392 | |
| 393 | // Nan create node for .nan value |
| 394 | func Nan(tk *token.Token) *NanNode { |
| 395 | return &NanNode{ |
| 396 | BaseNode: &BaseNode{}, |
| 397 | Token: tk, |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // String create node for string value |
| 402 | func String(tk *token.Token) *StringNode { |
| 403 | return &StringNode{ |
| 404 | BaseNode: &BaseNode{}, |
| 405 | Token: tk, |
| 406 | Value: tk.Value, |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Comment create node for comment |
| 411 | func Comment(tk *token.Token) *CommentNode { |
| 412 | return &CommentNode{ |
| 413 | BaseNode: &BaseNode{}, |
| 414 | Token: tk, |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | func CommentGroup(comments []*token.Token) *CommentGroupNode { |
| 419 | nodes := []*CommentNode{} |
| 420 | for _, comment := range comments { |
| 421 | nodes = append(nodes, Comment(comment)) |
| 422 | } |
| 423 | return &CommentGroupNode{ |
| 424 | BaseNode: &BaseNode{}, |
| 425 | Comments: nodes, |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // MergeKey create node for merge key ( << ) |
| 430 | func MergeKey(tk *token.Token) *MergeKeyNode { |
| 431 | return &MergeKeyNode{ |
| 432 | BaseNode: &BaseNode{}, |
| 433 | Token: tk, |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // Mapping create node for map |
| 438 | func Mapping(tk *token.Token, isFlowStyle bool, values ...*MappingValueNode) *MappingNode { |
| 439 | node := &MappingNode{ |
| 440 | BaseNode: &BaseNode{}, |
| 441 | Start: tk, |
| 442 | IsFlowStyle: isFlowStyle, |
| 443 | Values: []*MappingValueNode{}, |
| 444 | } |
| 445 | node.Values = append(node.Values, values...) |
| 446 | return node |
| 447 | } |
| 448 | |
| 449 | // MappingValue create node for mapping value |
| 450 | func MappingValue(tk *token.Token, key MapKeyNode, value Node) *MappingValueNode { |
| 451 | return &MappingValueNode{ |
| 452 | BaseNode: &BaseNode{}, |
| 453 | Start: tk, |
| 454 | Key: key, |
| 455 | Value: value, |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // MappingKey create node for map key ( '?' ). |
| 460 | func MappingKey(tk *token.Token) *MappingKeyNode { |
| 461 | return &MappingKeyNode{ |
| 462 | BaseNode: &BaseNode{}, |
| 463 | Start: tk, |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Sequence create node for sequence |
| 468 | func Sequence(tk *token.Token, isFlowStyle bool) *SequenceNode { |
| 469 | return &SequenceNode{ |
| 470 | BaseNode: &BaseNode{}, |
| 471 | Start: tk, |
| 472 | IsFlowStyle: isFlowStyle, |
| 473 | Values: []Node{}, |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func Anchor(tk *token.Token) *AnchorNode { |
| 478 | return &AnchorNode{ |
| 479 | BaseNode: &BaseNode{}, |
| 480 | Start: tk, |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | func Alias(tk *token.Token) *AliasNode { |
| 485 | return &AliasNode{ |
| 486 | BaseNode: &BaseNode{}, |
| 487 | Start: tk, |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | func Document(tk *token.Token, body Node) *DocumentNode { |
| 492 | return &DocumentNode{ |
| 493 | BaseNode: &BaseNode{}, |
| 494 | Start: tk, |
| 495 | Body: body, |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | func Directive(tk *token.Token) *DirectiveNode { |
| 500 | return &DirectiveNode{ |
| 501 | BaseNode: &BaseNode{}, |
| 502 | Start: tk, |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | func Literal(tk *token.Token) *LiteralNode { |
| 507 | return &LiteralNode{ |
| 508 | BaseNode: &BaseNode{}, |
| 509 | Start: tk, |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | func Tag(tk *token.Token) *TagNode { |
| 514 | return &TagNode{ |
| 515 | BaseNode: &BaseNode{}, |
| 516 | Start: tk, |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // File contains all documents in YAML file |
| 521 | type File struct { |
| 522 | Name string |
| 523 | Docs []*DocumentNode |
| 524 | } |
| 525 | |
| 526 | // Read implements (io.Reader).Read |
| 527 | func (f *File) Read(p []byte) (int, error) { |
| 528 | for _, doc := range f.Docs { |
| 529 | n, err := doc.Read(p) |
| 530 | if err == io.EOF { |
| 531 | continue |
| 532 | } |
| 533 | return n, nil |
| 534 | } |
| 535 | return 0, io.EOF |
| 536 | } |
| 537 | |
| 538 | // String all documents to text |
| 539 | func (f *File) String() string { |
| 540 | docs := []string{} |
| 541 | for _, doc := range f.Docs { |
| 542 | docs = append(docs, doc.String()) |
| 543 | } |
| 544 | if len(docs) > 0 { |
| 545 | return strings.Join(docs, "\n") + "\n" |
| 546 | } else { |
| 547 | return "" |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // DocumentNode type of Document |
| 552 | type DocumentNode struct { |
| 553 | *BaseNode |
| 554 | Start *token.Token // position of DocumentHeader ( `---` ) |
| 555 | End *token.Token // position of DocumentEnd ( `...` ) |
| 556 | Body Node |
| 557 | } |
| 558 | |
| 559 | // Read implements (io.Reader).Read |
| 560 | func (d *DocumentNode) Read(p []byte) (int, error) { |
| 561 | return readNode(p, d) |
| 562 | } |
| 563 | |
| 564 | // Type returns DocumentNodeType |
| 565 | func (d *DocumentNode) Type() NodeType { return DocumentType } |
| 566 | |
| 567 | // GetToken returns token instance |
| 568 | func (d *DocumentNode) GetToken() *token.Token { |
| 569 | return d.Body.GetToken() |
| 570 | } |
| 571 | |
| 572 | // AddColumn add column number to child nodes recursively |
| 573 | func (d *DocumentNode) AddColumn(col int) { |
| 574 | if d.Body != nil { |
| 575 | d.Body.AddColumn(col) |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // String document to text |
| 580 | func (d *DocumentNode) String() string { |
| 581 | doc := []string{} |
| 582 | if d.Start != nil { |
| 583 | doc = append(doc, d.Start.Value) |
| 584 | } |
| 585 | if d.Body != nil { |
| 586 | doc = append(doc, d.Body.String()) |
| 587 | } |
| 588 | if d.End != nil { |
| 589 | doc = append(doc, d.End.Value) |
| 590 | } |
| 591 | return strings.Join(doc, "\n") |
| 592 | } |
| 593 | |
| 594 | // MarshalYAML encodes to a YAML text |
| 595 | func (d *DocumentNode) MarshalYAML() ([]byte, error) { |
| 596 | return []byte(d.String()), nil |
| 597 | } |
| 598 | |
| 599 | // NullNode type of null node |
| 600 | type NullNode struct { |
| 601 | *BaseNode |
| 602 | Token *token.Token |
| 603 | } |
| 604 | |
| 605 | // Read implements (io.Reader).Read |
| 606 | func (n *NullNode) Read(p []byte) (int, error) { |
| 607 | return readNode(p, n) |
| 608 | } |
| 609 | |
| 610 | // Type returns NullType |
| 611 | func (n *NullNode) Type() NodeType { return NullType } |
| 612 | |
| 613 | // GetToken returns token instance |
| 614 | func (n *NullNode) GetToken() *token.Token { |
| 615 | return n.Token |
| 616 | } |
| 617 | |
| 618 | // AddColumn add column number to child nodes recursively |
| 619 | func (n *NullNode) AddColumn(col int) { |
| 620 | n.Token.AddColumn(col) |
| 621 | } |
| 622 | |
| 623 | // GetValue returns nil value |
| 624 | func (n *NullNode) GetValue() interface{} { |
| 625 | return nil |
| 626 | } |
| 627 | |
| 628 | // String returns `null` text |
| 629 | func (n *NullNode) String() string { |
| 630 | if n.Token.Type == token.ImplicitNullType { |
| 631 | if n.Comment != nil { |
| 632 | return n.Comment.String() |
| 633 | } |
| 634 | return "" |
| 635 | } |
| 636 | if n.Comment != nil { |
| 637 | return addCommentString("null", n.Comment) |
| 638 | } |
| 639 | return n.stringWithoutComment() |
| 640 | } |
| 641 | |
| 642 | func (n *NullNode) stringWithoutComment() string { |
| 643 | return "null" |
| 644 | } |
| 645 | |
| 646 | // MarshalYAML encodes to a YAML text |
| 647 | func (n *NullNode) MarshalYAML() ([]byte, error) { |
| 648 | return []byte(n.String()), nil |
| 649 | } |
| 650 | |
| 651 | // IsMergeKey returns whether it is a MergeKey node. |
| 652 | func (n *NullNode) IsMergeKey() bool { |
| 653 | return false |
| 654 | } |
| 655 | |
| 656 | // IntegerNode type of integer node |
| 657 | type IntegerNode struct { |
| 658 | *BaseNode |
| 659 | Token *token.Token |
| 660 | Value interface{} // int64 or uint64 value |
| 661 | } |
| 662 | |
| 663 | // Read implements (io.Reader).Read |
| 664 | func (n *IntegerNode) Read(p []byte) (int, error) { |
| 665 | return readNode(p, n) |
| 666 | } |
| 667 | |
| 668 | // Type returns IntegerType |
| 669 | func (n *IntegerNode) Type() NodeType { return IntegerType } |
| 670 | |
| 671 | // GetToken returns token instance |
| 672 | func (n *IntegerNode) GetToken() *token.Token { |
| 673 | return n.Token |
| 674 | } |
| 675 | |
| 676 | // AddColumn add column number to child nodes recursively |
| 677 | func (n *IntegerNode) AddColumn(col int) { |
| 678 | n.Token.AddColumn(col) |
| 679 | } |
| 680 | |
| 681 | // GetValue returns int64 value |
| 682 | func (n *IntegerNode) GetValue() interface{} { |
| 683 | return n.Value |
| 684 | } |
| 685 | |
| 686 | // String int64 to text |
| 687 | func (n *IntegerNode) String() string { |
| 688 | if n.Comment != nil { |
| 689 | return addCommentString(n.Token.Value, n.Comment) |
| 690 | } |
| 691 | return n.stringWithoutComment() |
| 692 | } |
| 693 | |
| 694 | func (n *IntegerNode) stringWithoutComment() string { |
| 695 | return n.Token.Value |
| 696 | } |
| 697 | |
| 698 | // MarshalYAML encodes to a YAML text |
| 699 | func (n *IntegerNode) MarshalYAML() ([]byte, error) { |
| 700 | return []byte(n.String()), nil |
| 701 | } |
| 702 | |
| 703 | // IsMergeKey returns whether it is a MergeKey node. |
| 704 | func (n *IntegerNode) IsMergeKey() bool { |
| 705 | return false |
| 706 | } |
| 707 | |
| 708 | // FloatNode type of float node |
| 709 | type FloatNode struct { |
| 710 | *BaseNode |
| 711 | Token *token.Token |
| 712 | Precision int |
| 713 | Value float64 |
| 714 | } |
| 715 | |
| 716 | // Read implements (io.Reader).Read |
| 717 | func (n *FloatNode) Read(p []byte) (int, error) { |
| 718 | return readNode(p, n) |
| 719 | } |
| 720 | |
| 721 | // Type returns FloatType |
| 722 | func (n *FloatNode) Type() NodeType { return FloatType } |
| 723 | |
| 724 | // GetToken returns token instance |
| 725 | func (n *FloatNode) GetToken() *token.Token { |
| 726 | return n.Token |
| 727 | } |
| 728 | |
| 729 | // AddColumn add column number to child nodes recursively |
| 730 | func (n *FloatNode) AddColumn(col int) { |
| 731 | n.Token.AddColumn(col) |
| 732 | } |
| 733 | |
| 734 | // GetValue returns float64 value |
| 735 | func (n *FloatNode) GetValue() interface{} { |
| 736 | return n.Value |
| 737 | } |
| 738 | |
| 739 | // String float64 to text |
| 740 | func (n *FloatNode) String() string { |
| 741 | if n.Comment != nil { |
| 742 | return addCommentString(n.Token.Value, n.Comment) |
| 743 | } |
| 744 | return n.stringWithoutComment() |
| 745 | } |
| 746 | |
| 747 | func (n *FloatNode) stringWithoutComment() string { |
| 748 | return n.Token.Value |
| 749 | } |
| 750 | |
| 751 | // MarshalYAML encodes to a YAML text |
| 752 | func (n *FloatNode) MarshalYAML() ([]byte, error) { |
| 753 | return []byte(n.String()), nil |
| 754 | } |
| 755 | |
| 756 | // IsMergeKey returns whether it is a MergeKey node. |
| 757 | func (n *FloatNode) IsMergeKey() bool { |
| 758 | return false |
| 759 | } |
| 760 | |
| 761 | // StringNode type of string node |
| 762 | type StringNode struct { |
| 763 | *BaseNode |
| 764 | Token *token.Token |
| 765 | Value string |
| 766 | } |
| 767 | |
| 768 | // Read implements (io.Reader).Read |
| 769 | func (n *StringNode) Read(p []byte) (int, error) { |
| 770 | return readNode(p, n) |
| 771 | } |
| 772 | |
| 773 | // Type returns StringType |
| 774 | func (n *StringNode) Type() NodeType { return StringType } |
| 775 | |
| 776 | // GetToken returns token instance |
| 777 | func (n *StringNode) GetToken() *token.Token { |
| 778 | return n.Token |
| 779 | } |
| 780 | |
| 781 | // AddColumn add column number to child nodes recursively |
| 782 | func (n *StringNode) AddColumn(col int) { |
| 783 | n.Token.AddColumn(col) |
| 784 | } |
| 785 | |
| 786 | // GetValue returns string value |
| 787 | func (n *StringNode) GetValue() interface{} { |
| 788 | return n.Value |
| 789 | } |
| 790 | |
| 791 | // IsMergeKey returns whether it is a MergeKey node. |
| 792 | func (n *StringNode) IsMergeKey() bool { |
| 793 | return false |
| 794 | } |
| 795 | |
| 796 | // escapeSingleQuote escapes s to a single quoted scalar. |
| 797 | // https://yaml.org/spec/1.2.2/#732-single-quoted-style |
| 798 | func escapeSingleQuote(s string) string { |
| 799 | var sb strings.Builder |
| 800 | growLen := len(s) + // s includes also one ' from the doubled pair |
| 801 | 2 + // opening and closing ' |
| 802 | strings.Count(s, "'") // ' added by ReplaceAll |
| 803 | sb.Grow(growLen) |
| 804 | sb.WriteString("'") |
| 805 | sb.WriteString(strings.ReplaceAll(s, "'", "''")) |
| 806 | sb.WriteString("'") |
| 807 | return sb.String() |
| 808 | } |
| 809 | |
| 810 | // String string value to text with quote or literal header if required |
| 811 | func (n *StringNode) String() string { |
| 812 | switch n.Token.Type { |
| 813 | case token.SingleQuoteType: |
| 814 | quoted := escapeSingleQuote(n.Value) |
| 815 | if n.Comment != nil { |
| 816 | return addCommentString(quoted, n.Comment) |
| 817 | } |
| 818 | return quoted |
| 819 | case token.DoubleQuoteType: |
| 820 | quoted := strconv.Quote(n.Value) |
| 821 | if n.Comment != nil { |
| 822 | return addCommentString(quoted, n.Comment) |
| 823 | } |
| 824 | return quoted |
| 825 | } |
| 826 | |
| 827 | lbc := token.DetectLineBreakCharacter(n.Value) |
| 828 | if strings.Contains(n.Value, lbc) { |
| 829 | // This block assumes that the line breaks in this inside scalar content and the Outside scalar content are the same. |
| 830 | // It works mostly, but inconsistencies occur if line break characters are mixed. |
| 831 | header := token.LiteralBlockHeader(n.Value) |
| 832 | space := strings.Repeat(" ", n.Token.Position.Column-1) |
| 833 | indent := strings.Repeat(" ", n.Token.Position.IndentNum) |
| 834 | values := []string{} |
| 835 | for _, v := range strings.Split(n.Value, lbc) { |
| 836 | values = append(values, fmt.Sprintf("%s%s%s", space, indent, v)) |
| 837 | } |
| 838 | block := strings.TrimSuffix(strings.TrimSuffix(strings.Join(values, lbc), fmt.Sprintf("%s%s%s", lbc, indent, space)), fmt.Sprintf("%s%s", indent, space)) |
| 839 | return fmt.Sprintf("%s%s%s", header, lbc, block) |
| 840 | } else if len(n.Value) > 0 && (n.Value[0] == '{' || n.Value[0] == '[') { |
| 841 | return fmt.Sprintf(`'%s'`, n.Value) |
| 842 | } |
| 843 | if n.Comment != nil { |
| 844 | return addCommentString(n.Value, n.Comment) |
| 845 | } |
| 846 | return n.Value |
| 847 | } |
| 848 | |
| 849 | func (n *StringNode) stringWithoutComment() string { |
| 850 | switch n.Token.Type { |
| 851 | case token.SingleQuoteType: |
| 852 | quoted := fmt.Sprintf(`'%s'`, n.Value) |
| 853 | return quoted |
| 854 | case token.DoubleQuoteType: |
| 855 | quoted := strconv.Quote(n.Value) |
| 856 | return quoted |
| 857 | } |
| 858 | |
| 859 | lbc := token.DetectLineBreakCharacter(n.Value) |
| 860 | if strings.Contains(n.Value, lbc) { |
| 861 | // This block assumes that the line breaks in this inside scalar content and the Outside scalar content are the same. |
| 862 | // It works mostly, but inconsistencies occur if line break characters are mixed. |
| 863 | header := token.LiteralBlockHeader(n.Value) |
| 864 | space := strings.Repeat(" ", n.Token.Position.Column-1) |
| 865 | indent := strings.Repeat(" ", n.Token.Position.IndentNum) |
| 866 | values := []string{} |
| 867 | for _, v := range strings.Split(n.Value, lbc) { |
| 868 | values = append(values, fmt.Sprintf("%s%s%s", space, indent, v)) |
| 869 | } |
| 870 | block := strings.TrimSuffix(strings.TrimSuffix(strings.Join(values, lbc), fmt.Sprintf("%s%s%s", lbc, indent, space)), fmt.Sprintf(" %s", space)) |
| 871 | return fmt.Sprintf("%s%s%s", header, lbc, block) |
| 872 | } else if len(n.Value) > 0 && (n.Value[0] == '{' || n.Value[0] == '[') { |
| 873 | return fmt.Sprintf(`'%s'`, n.Value) |
| 874 | } |
| 875 | return n.Value |
| 876 | } |
| 877 | |
| 878 | // MarshalYAML encodes to a YAML text |
| 879 | func (n *StringNode) MarshalYAML() ([]byte, error) { |
| 880 | return []byte(n.String()), nil |
| 881 | } |
| 882 | |
| 883 | // LiteralNode type of literal node |
| 884 | type LiteralNode struct { |
| 885 | *BaseNode |
| 886 | Start *token.Token |
| 887 | Value *StringNode |
| 888 | } |
| 889 | |
| 890 | // Read implements (io.Reader).Read |
| 891 | func (n *LiteralNode) Read(p []byte) (int, error) { |
| 892 | return readNode(p, n) |
| 893 | } |
| 894 | |
| 895 | // Type returns LiteralType |
| 896 | func (n *LiteralNode) Type() NodeType { return LiteralType } |
| 897 | |
| 898 | // GetToken returns token instance |
| 899 | func (n *LiteralNode) GetToken() *token.Token { |
| 900 | return n.Start |
| 901 | } |
| 902 | |
| 903 | // AddColumn add column number to child nodes recursively |
| 904 | func (n *LiteralNode) AddColumn(col int) { |
| 905 | n.Start.AddColumn(col) |
| 906 | if n.Value != nil { |
| 907 | n.Value.AddColumn(col) |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // GetValue returns string value |
| 912 | func (n *LiteralNode) GetValue() interface{} { |
| 913 | return n.String() |
| 914 | } |
| 915 | |
| 916 | // String literal to text |
| 917 | func (n *LiteralNode) String() string { |
| 918 | origin := n.Value.GetToken().Origin |
| 919 | lit := strings.TrimRight(strings.TrimRight(origin, " "), "\n") |
| 920 | if n.Comment != nil { |
| 921 | return fmt.Sprintf("%s %s\n%s", n.Start.Value, n.Comment.String(), lit) |
| 922 | } |
| 923 | return fmt.Sprintf("%s\n%s", n.Start.Value, lit) |
| 924 | } |
| 925 | |
| 926 | func (n *LiteralNode) stringWithoutComment() string { |
| 927 | return n.String() |
| 928 | } |
| 929 | |
| 930 | // MarshalYAML encodes to a YAML text |
| 931 | func (n *LiteralNode) MarshalYAML() ([]byte, error) { |
| 932 | return []byte(n.String()), nil |
| 933 | } |
| 934 | |
| 935 | // IsMergeKey returns whether it is a MergeKey node. |
| 936 | func (n *LiteralNode) IsMergeKey() bool { |
| 937 | return false |
| 938 | } |
| 939 | |
| 940 | // MergeKeyNode type of merge key node |
| 941 | type MergeKeyNode struct { |
| 942 | *BaseNode |
| 943 | Token *token.Token |
| 944 | } |
| 945 | |
| 946 | // Read implements (io.Reader).Read |
| 947 | func (n *MergeKeyNode) Read(p []byte) (int, error) { |
| 948 | return readNode(p, n) |
| 949 | } |
| 950 | |
| 951 | // Type returns MergeKeyType |
| 952 | func (n *MergeKeyNode) Type() NodeType { return MergeKeyType } |
| 953 | |
| 954 | // GetToken returns token instance |
| 955 | func (n *MergeKeyNode) GetToken() *token.Token { |
| 956 | return n.Token |
| 957 | } |
| 958 | |
| 959 | // GetValue returns '<<' value |
| 960 | func (n *MergeKeyNode) GetValue() interface{} { |
| 961 | return n.Token.Value |
| 962 | } |
| 963 | |
| 964 | // String returns '<<' value |
| 965 | func (n *MergeKeyNode) String() string { |
| 966 | return n.stringWithoutComment() |
| 967 | } |
| 968 | |
| 969 | func (n *MergeKeyNode) stringWithoutComment() string { |
| 970 | return n.Token.Value |
| 971 | } |
| 972 | |
| 973 | // AddColumn add column number to child nodes recursively |
| 974 | func (n *MergeKeyNode) AddColumn(col int) { |
| 975 | n.Token.AddColumn(col) |
| 976 | } |
| 977 | |
| 978 | // MarshalYAML encodes to a YAML text |
| 979 | func (n *MergeKeyNode) MarshalYAML() ([]byte, error) { |
| 980 | return []byte(n.String()), nil |
| 981 | } |
| 982 | |
| 983 | // IsMergeKey returns whether it is a MergeKey node. |
| 984 | func (n *MergeKeyNode) IsMergeKey() bool { |
| 985 | return true |
| 986 | } |
| 987 | |
| 988 | // BoolNode type of boolean node |
| 989 | type BoolNode struct { |
| 990 | *BaseNode |
| 991 | Token *token.Token |
| 992 | Value bool |
| 993 | } |
| 994 | |
| 995 | // Read implements (io.Reader).Read |
| 996 | func (n *BoolNode) Read(p []byte) (int, error) { |
| 997 | return readNode(p, n) |
| 998 | } |
| 999 | |
| 1000 | // Type returns BoolType |
| 1001 | func (n *BoolNode) Type() NodeType { return BoolType } |
| 1002 | |
| 1003 | // GetToken returns token instance |
| 1004 | func (n *BoolNode) GetToken() *token.Token { |
| 1005 | return n.Token |
| 1006 | } |
| 1007 | |
| 1008 | // AddColumn add column number to child nodes recursively |
| 1009 | func (n *BoolNode) AddColumn(col int) { |
| 1010 | n.Token.AddColumn(col) |
| 1011 | } |
| 1012 | |
| 1013 | // GetValue returns boolean value |
| 1014 | func (n *BoolNode) GetValue() interface{} { |
| 1015 | return n.Value |
| 1016 | } |
| 1017 | |
| 1018 | // String boolean to text |
| 1019 | func (n *BoolNode) String() string { |
| 1020 | if n.Comment != nil { |
| 1021 | return addCommentString(n.Token.Value, n.Comment) |
| 1022 | } |
| 1023 | return n.stringWithoutComment() |
| 1024 | } |
| 1025 | |
| 1026 | func (n *BoolNode) stringWithoutComment() string { |
| 1027 | return n.Token.Value |
| 1028 | } |
| 1029 | |
| 1030 | // MarshalYAML encodes to a YAML text |
| 1031 | func (n *BoolNode) MarshalYAML() ([]byte, error) { |
| 1032 | return []byte(n.String()), nil |
| 1033 | } |
| 1034 | |
| 1035 | // IsMergeKey returns whether it is a MergeKey node. |
| 1036 | func (n *BoolNode) IsMergeKey() bool { |
| 1037 | return false |
| 1038 | } |
| 1039 | |
| 1040 | // InfinityNode type of infinity node |
| 1041 | type InfinityNode struct { |
| 1042 | *BaseNode |
| 1043 | Token *token.Token |
| 1044 | Value float64 |
| 1045 | } |
| 1046 | |
| 1047 | // Read implements (io.Reader).Read |
| 1048 | func (n *InfinityNode) Read(p []byte) (int, error) { |
| 1049 | return readNode(p, n) |
| 1050 | } |
| 1051 | |
| 1052 | // Type returns InfinityType |
| 1053 | func (n *InfinityNode) Type() NodeType { return InfinityType } |
| 1054 | |
| 1055 | // GetToken returns token instance |
| 1056 | func (n *InfinityNode) GetToken() *token.Token { |
| 1057 | return n.Token |
| 1058 | } |
| 1059 | |
| 1060 | // AddColumn add column number to child nodes recursively |
| 1061 | func (n *InfinityNode) AddColumn(col int) { |
| 1062 | n.Token.AddColumn(col) |
| 1063 | } |
| 1064 | |
| 1065 | // GetValue returns math.Inf(0) or math.Inf(-1) |
| 1066 | func (n *InfinityNode) GetValue() interface{} { |
| 1067 | return n.Value |
| 1068 | } |
| 1069 | |
| 1070 | // String infinity to text |
| 1071 | func (n *InfinityNode) String() string { |
| 1072 | if n.Comment != nil { |
| 1073 | return addCommentString(n.Token.Value, n.Comment) |
| 1074 | } |
| 1075 | return n.stringWithoutComment() |
| 1076 | } |
| 1077 | |
| 1078 | func (n *InfinityNode) stringWithoutComment() string { |
| 1079 | return n.Token.Value |
| 1080 | } |
| 1081 | |
| 1082 | // MarshalYAML encodes to a YAML text |
| 1083 | func (n *InfinityNode) MarshalYAML() ([]byte, error) { |
| 1084 | return []byte(n.String()), nil |
| 1085 | } |
| 1086 | |
| 1087 | // IsMergeKey returns whether it is a MergeKey node. |
| 1088 | func (n *InfinityNode) IsMergeKey() bool { |
| 1089 | return false |
| 1090 | } |
| 1091 | |
| 1092 | // NanNode type of nan node |
| 1093 | type NanNode struct { |
| 1094 | *BaseNode |
| 1095 | Token *token.Token |
| 1096 | } |
| 1097 | |
| 1098 | // Read implements (io.Reader).Read |
| 1099 | func (n *NanNode) Read(p []byte) (int, error) { |
| 1100 | return readNode(p, n) |
| 1101 | } |
| 1102 | |
| 1103 | // Type returns NanType |
| 1104 | func (n *NanNode) Type() NodeType { return NanType } |
| 1105 | |
| 1106 | // GetToken returns token instance |
| 1107 | func (n *NanNode) GetToken() *token.Token { |
| 1108 | return n.Token |
| 1109 | } |
| 1110 | |
| 1111 | // AddColumn add column number to child nodes recursively |
| 1112 | func (n *NanNode) AddColumn(col int) { |
| 1113 | n.Token.AddColumn(col) |
| 1114 | } |
| 1115 | |
| 1116 | // GetValue returns math.NaN() |
| 1117 | func (n *NanNode) GetValue() interface{} { |
| 1118 | return math.NaN() |
| 1119 | } |
| 1120 | |
| 1121 | // String returns .nan |
| 1122 | func (n *NanNode) String() string { |
| 1123 | if n.Comment != nil { |
| 1124 | return addCommentString(n.Token.Value, n.Comment) |
| 1125 | } |
| 1126 | return n.stringWithoutComment() |
| 1127 | } |
| 1128 | |
| 1129 | func (n *NanNode) stringWithoutComment() string { |
| 1130 | return n.Token.Value |
| 1131 | } |
| 1132 | |
| 1133 | // MarshalYAML encodes to a YAML text |
| 1134 | func (n *NanNode) MarshalYAML() ([]byte, error) { |
| 1135 | return []byte(n.String()), nil |
| 1136 | } |
| 1137 | |
| 1138 | // IsMergeKey returns whether it is a MergeKey node. |
| 1139 | func (n *NanNode) IsMergeKey() bool { |
| 1140 | return false |
| 1141 | } |
| 1142 | |
| 1143 | // MapNode interface of MappingValueNode / MappingNode |
| 1144 | type MapNode interface { |
| 1145 | MapRange() *MapNodeIter |
| 1146 | } |
| 1147 | |
| 1148 | // MapNodeIter is an iterator for ranging over a MapNode |
| 1149 | type MapNodeIter struct { |
| 1150 | values []*MappingValueNode |
| 1151 | idx int |
| 1152 | } |
| 1153 | |
| 1154 | const ( |
| 1155 | startRangeIndex = -1 |
| 1156 | ) |
| 1157 | |
| 1158 | // Next advances the map iterator and reports whether there is another entry. |
| 1159 | // It returns false when the iterator is exhausted. |
| 1160 | func (m *MapNodeIter) Next() bool { |
| 1161 | m.idx++ |
| 1162 | next := m.idx < len(m.values) |
| 1163 | return next |
| 1164 | } |
| 1165 | |
| 1166 | // Key returns the key of the iterator's current map node entry. |
| 1167 | func (m *MapNodeIter) Key() MapKeyNode { |
| 1168 | return m.values[m.idx].Key |
| 1169 | } |
| 1170 | |
| 1171 | // Value returns the value of the iterator's current map node entry. |
| 1172 | func (m *MapNodeIter) Value() Node { |
| 1173 | return m.values[m.idx].Value |
| 1174 | } |
| 1175 | |
| 1176 | // KeyValue returns the MappingValueNode of the iterator's current map node entry. |
| 1177 | func (m *MapNodeIter) KeyValue() *MappingValueNode { |
| 1178 | return m.values[m.idx] |
| 1179 | } |
| 1180 | |
| 1181 | // MappingNode type of mapping node |
| 1182 | type MappingNode struct { |
| 1183 | *BaseNode |
| 1184 | Start *token.Token |
| 1185 | End *token.Token |
| 1186 | IsFlowStyle bool |
| 1187 | Values []*MappingValueNode |
| 1188 | FootComment *CommentGroupNode |
| 1189 | } |
| 1190 | |
| 1191 | func (n *MappingNode) startPos() *token.Position { |
| 1192 | if len(n.Values) == 0 { |
| 1193 | return n.Start.Position |
| 1194 | } |
| 1195 | return n.Values[0].Key.GetToken().Position |
| 1196 | } |
| 1197 | |
| 1198 | // Merge merge key/value of map. |
| 1199 | func (n *MappingNode) Merge(target *MappingNode) { |
| 1200 | keyToMapValueMap := map[string]*MappingValueNode{} |
| 1201 | for _, value := range n.Values { |
| 1202 | key := value.Key.String() |
| 1203 | keyToMapValueMap[key] = value |
| 1204 | } |
| 1205 | column := n.startPos().Column - target.startPos().Column |
| 1206 | target.AddColumn(column) |
| 1207 | for _, value := range target.Values { |
| 1208 | mapValue, exists := keyToMapValueMap[value.Key.String()] |
| 1209 | if exists { |
| 1210 | mapValue.Value = value.Value |
| 1211 | } else { |
| 1212 | n.Values = append(n.Values, value) |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | // SetIsFlowStyle set value to IsFlowStyle field recursively. |
| 1218 | func (n *MappingNode) SetIsFlowStyle(isFlow bool) { |
| 1219 | n.IsFlowStyle = isFlow |
| 1220 | for _, value := range n.Values { |
| 1221 | value.SetIsFlowStyle(isFlow) |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | // Read implements (io.Reader).Read |
| 1226 | func (n *MappingNode) Read(p []byte) (int, error) { |
| 1227 | return readNode(p, n) |
| 1228 | } |
| 1229 | |
| 1230 | // Type returns MappingType |
| 1231 | func (n *MappingNode) Type() NodeType { return MappingType } |
| 1232 | |
| 1233 | // GetToken returns token instance |
| 1234 | func (n *MappingNode) GetToken() *token.Token { |
| 1235 | return n.Start |
| 1236 | } |
| 1237 | |
| 1238 | // AddColumn add column number to child nodes recursively |
| 1239 | func (n *MappingNode) AddColumn(col int) { |
| 1240 | n.Start.AddColumn(col) |
| 1241 | n.End.AddColumn(col) |
| 1242 | for _, value := range n.Values { |
| 1243 | value.AddColumn(col) |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | func (n *MappingNode) flowStyleString(commentMode bool) string { |
| 1248 | values := []string{} |
| 1249 | for _, value := range n.Values { |
| 1250 | values = append(values, strings.TrimLeft(value.String(), " ")) |
| 1251 | } |
| 1252 | mapText := fmt.Sprintf("{%s}", strings.Join(values, ", ")) |
| 1253 | if commentMode && n.Comment != nil { |
| 1254 | return addCommentString(mapText, n.Comment) |
| 1255 | } |
| 1256 | return mapText |
| 1257 | } |
| 1258 | |
| 1259 | func (n *MappingNode) blockStyleString(commentMode bool) string { |
| 1260 | values := []string{} |
| 1261 | for _, value := range n.Values { |
| 1262 | values = append(values, value.String()) |
| 1263 | } |
| 1264 | mapText := strings.Join(values, "\n") |
| 1265 | if commentMode && n.Comment != nil { |
| 1266 | value := values[0] |
| 1267 | var spaceNum int |
| 1268 | for i := 0; i < len(value); i++ { |
| 1269 | if value[i] != ' ' { |
| 1270 | break |
| 1271 | } |
| 1272 | spaceNum++ |
| 1273 | } |
| 1274 | comment := n.Comment.StringWithSpace(spaceNum) |
| 1275 | return fmt.Sprintf("%s\n%s", comment, mapText) |
| 1276 | } |
| 1277 | return mapText |
| 1278 | } |
| 1279 | |
| 1280 | // String mapping values to text |
| 1281 | func (n *MappingNode) String() string { |
| 1282 | if len(n.Values) == 0 { |
| 1283 | if n.Comment != nil { |
| 1284 | return addCommentString("{}", n.Comment) |
| 1285 | } |
| 1286 | return "{}" |
| 1287 | } |
| 1288 | |
| 1289 | commentMode := true |
| 1290 | if n.IsFlowStyle || len(n.Values) == 0 { |
| 1291 | return n.flowStyleString(commentMode) |
| 1292 | } |
| 1293 | return n.blockStyleString(commentMode) |
| 1294 | } |
| 1295 | |
| 1296 | // MapRange implements MapNode protocol |
| 1297 | func (n *MappingNode) MapRange() *MapNodeIter { |
| 1298 | return &MapNodeIter{ |
| 1299 | idx: startRangeIndex, |
| 1300 | values: n.Values, |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | // MarshalYAML encodes to a YAML text |
| 1305 | func (n *MappingNode) MarshalYAML() ([]byte, error) { |
| 1306 | return []byte(n.String()), nil |
| 1307 | } |
| 1308 | |
| 1309 | // MappingKeyNode type of tag node |
| 1310 | type MappingKeyNode struct { |
| 1311 | *BaseNode |
| 1312 | Start *token.Token |
| 1313 | Value Node |
| 1314 | } |
| 1315 | |
| 1316 | // Read implements (io.Reader).Read |
| 1317 | func (n *MappingKeyNode) Read(p []byte) (int, error) { |
| 1318 | return readNode(p, n) |
| 1319 | } |
| 1320 | |
| 1321 | // Type returns MappingKeyType |
| 1322 | func (n *MappingKeyNode) Type() NodeType { return MappingKeyType } |
| 1323 | |
| 1324 | // GetToken returns token instance |
| 1325 | func (n *MappingKeyNode) GetToken() *token.Token { |
| 1326 | return n.Start |
| 1327 | } |
| 1328 | |
| 1329 | // AddColumn add column number to child nodes recursively |
| 1330 | func (n *MappingKeyNode) AddColumn(col int) { |
| 1331 | n.Start.AddColumn(col) |
| 1332 | if n.Value != nil { |
| 1333 | n.Value.AddColumn(col) |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | // String tag to text |
| 1338 | func (n *MappingKeyNode) String() string { |
| 1339 | return n.stringWithoutComment() |
| 1340 | } |
| 1341 | |
| 1342 | func (n *MappingKeyNode) stringWithoutComment() string { |
| 1343 | return fmt.Sprintf("%s %s", n.Start.Value, n.Value.String()) |
| 1344 | } |
| 1345 | |
| 1346 | // MarshalYAML encodes to a YAML text |
| 1347 | func (n *MappingKeyNode) MarshalYAML() ([]byte, error) { |
| 1348 | return []byte(n.String()), nil |
| 1349 | } |
| 1350 | |
| 1351 | // IsMergeKey returns whether it is a MergeKey node. |
| 1352 | func (n *MappingKeyNode) IsMergeKey() bool { |
| 1353 | if n.Value == nil { |
| 1354 | return false |
| 1355 | } |
| 1356 | key, ok := n.Value.(MapKeyNode) |
| 1357 | if !ok { |
| 1358 | return false |
| 1359 | } |
| 1360 | return key.IsMergeKey() |
| 1361 | } |
| 1362 | |
| 1363 | // MappingValueNode type of mapping value |
| 1364 | type MappingValueNode struct { |
| 1365 | *BaseNode |
| 1366 | Start *token.Token // delimiter token ':'. |
| 1367 | CollectEntry *token.Token // collect entry token ','. |
| 1368 | Key MapKeyNode |
| 1369 | Value Node |
| 1370 | FootComment *CommentGroupNode |
| 1371 | IsFlowStyle bool |
| 1372 | } |
| 1373 | |
| 1374 | // Replace replace value node. |
| 1375 | func (n *MappingValueNode) Replace(value Node) error { |
| 1376 | column := n.Value.GetToken().Position.Column - value.GetToken().Position.Column |
| 1377 | value.AddColumn(column) |
| 1378 | n.Value = value |
| 1379 | return nil |
| 1380 | } |
| 1381 | |
| 1382 | // Read implements (io.Reader).Read |
| 1383 | func (n *MappingValueNode) Read(p []byte) (int, error) { |
| 1384 | return readNode(p, n) |
| 1385 | } |
| 1386 | |
| 1387 | // Type returns MappingValueType |
| 1388 | func (n *MappingValueNode) Type() NodeType { return MappingValueType } |
| 1389 | |
| 1390 | // GetToken returns token instance |
| 1391 | func (n *MappingValueNode) GetToken() *token.Token { |
| 1392 | return n.Start |
| 1393 | } |
| 1394 | |
| 1395 | // AddColumn add column number to child nodes recursively |
| 1396 | func (n *MappingValueNode) AddColumn(col int) { |
| 1397 | n.Start.AddColumn(col) |
| 1398 | if n.Key != nil { |
| 1399 | n.Key.AddColumn(col) |
| 1400 | } |
| 1401 | if n.Value != nil { |
| 1402 | n.Value.AddColumn(col) |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | // SetIsFlowStyle set value to IsFlowStyle field recursively. |
| 1407 | func (n *MappingValueNode) SetIsFlowStyle(isFlow bool) { |
| 1408 | n.IsFlowStyle = isFlow |
| 1409 | switch value := n.Value.(type) { |
| 1410 | case *MappingNode: |
| 1411 | value.SetIsFlowStyle(isFlow) |
| 1412 | case *MappingValueNode: |
| 1413 | value.SetIsFlowStyle(isFlow) |
| 1414 | case *SequenceNode: |
| 1415 | value.SetIsFlowStyle(isFlow) |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | // String mapping value to text |
| 1420 | func (n *MappingValueNode) String() string { |
| 1421 | var text string |
| 1422 | if n.Comment != nil { |
| 1423 | text = fmt.Sprintf( |
| 1424 | "%s\n%s", |
| 1425 | n.Comment.StringWithSpace(n.Key.GetToken().Position.Column-1), |
| 1426 | n.toString(), |
| 1427 | ) |
| 1428 | } else { |
| 1429 | text = n.toString() |
| 1430 | } |
| 1431 | if n.FootComment != nil { |
| 1432 | text += fmt.Sprintf("\n%s", n.FootComment.StringWithSpace(n.Key.GetToken().Position.Column-1)) |
| 1433 | } |
| 1434 | return text |
| 1435 | } |
| 1436 | |
| 1437 | func (n *MappingValueNode) toString() string { |
| 1438 | space := strings.Repeat(" ", n.Key.GetToken().Position.Column-1) |
| 1439 | if checkLineBreak(n.Key.GetToken()) { |
| 1440 | space = fmt.Sprintf("%s%s", "\n", space) |
| 1441 | } |
| 1442 | keyIndentLevel := n.Key.GetToken().Position.IndentLevel |
| 1443 | valueIndentLevel := n.Value.GetToken().Position.IndentLevel |
| 1444 | keyComment := n.Key.GetComment() |
| 1445 | if _, ok := n.Value.(ScalarNode); ok { |
| 1446 | value := n.Value.String() |
| 1447 | if value == "" { |
| 1448 | // implicit null value. |
| 1449 | return fmt.Sprintf("%s%s:", space, n.Key.String()) |
| 1450 | } |
| 1451 | return fmt.Sprintf("%s%s: %s", space, n.Key.String(), value) |
| 1452 | } else if keyIndentLevel < valueIndentLevel && !n.IsFlowStyle { |
| 1453 | if keyComment != nil { |
| 1454 | return fmt.Sprintf( |
| 1455 | "%s%s: %s\n%s", |
| 1456 | space, |
| 1457 | n.Key.stringWithoutComment(), |
| 1458 | keyComment.String(), |
| 1459 | n.Value.String(), |
| 1460 | ) |
| 1461 | } |
| 1462 | return fmt.Sprintf("%s%s:\n%s", space, n.Key.String(), n.Value.String()) |
| 1463 | } else if m, ok := n.Value.(*MappingNode); ok && (m.IsFlowStyle || len(m.Values) == 0) { |
| 1464 | return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String()) |
| 1465 | } else if s, ok := n.Value.(*SequenceNode); ok && (s.IsFlowStyle || len(s.Values) == 0) { |
| 1466 | return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String()) |
| 1467 | } else if _, ok := n.Value.(*AnchorNode); ok { |
| 1468 | return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String()) |
| 1469 | } else if _, ok := n.Value.(*AliasNode); ok { |
| 1470 | return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String()) |
| 1471 | } else if _, ok := n.Value.(*TagNode); ok { |
| 1472 | return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String()) |
| 1473 | } |
| 1474 | |
| 1475 | if keyComment != nil { |
| 1476 | return fmt.Sprintf( |
| 1477 | "%s%s: %s\n%s", |
| 1478 | space, |
| 1479 | n.Key.stringWithoutComment(), |
| 1480 | keyComment.String(), |
| 1481 | n.Value.String(), |
| 1482 | ) |
| 1483 | } |
| 1484 | if m, ok := n.Value.(*MappingNode); ok && m.Comment != nil { |
| 1485 | return fmt.Sprintf( |
| 1486 | "%s%s: %s", |
| 1487 | space, |
| 1488 | n.Key.String(), |
| 1489 | strings.TrimLeft(n.Value.String(), " "), |
| 1490 | ) |
| 1491 | } |
| 1492 | return fmt.Sprintf("%s%s:\n%s", space, n.Key.String(), n.Value.String()) |
| 1493 | } |
| 1494 | |
| 1495 | // MapRange implements MapNode protocol |
| 1496 | func (n *MappingValueNode) MapRange() *MapNodeIter { |
| 1497 | return &MapNodeIter{ |
| 1498 | idx: startRangeIndex, |
| 1499 | values: []*MappingValueNode{n}, |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | // MarshalYAML encodes to a YAML text |
| 1504 | func (n *MappingValueNode) MarshalYAML() ([]byte, error) { |
| 1505 | return []byte(n.String()), nil |
| 1506 | } |
| 1507 | |
| 1508 | // ArrayNode interface of SequenceNode |
| 1509 | type ArrayNode interface { |
| 1510 | ArrayRange() *ArrayNodeIter |
| 1511 | } |
| 1512 | |
| 1513 | // ArrayNodeIter is an iterator for ranging over a ArrayNode |
| 1514 | type ArrayNodeIter struct { |
| 1515 | values []Node |
| 1516 | idx int |
| 1517 | } |
| 1518 | |
| 1519 | // Next advances the array iterator and reports whether there is another entry. |
| 1520 | // It returns false when the iterator is exhausted. |
| 1521 | func (m *ArrayNodeIter) Next() bool { |
| 1522 | m.idx++ |
| 1523 | next := m.idx < len(m.values) |
| 1524 | return next |
| 1525 | } |
| 1526 | |
| 1527 | // Value returns the value of the iterator's current array entry. |
| 1528 | func (m *ArrayNodeIter) Value() Node { |
| 1529 | return m.values[m.idx] |
| 1530 | } |
| 1531 | |
| 1532 | // Len returns length of array |
| 1533 | func (m *ArrayNodeIter) Len() int { |
| 1534 | return len(m.values) |
| 1535 | } |
| 1536 | |
| 1537 | // SequenceNode type of sequence node |
| 1538 | type SequenceNode struct { |
| 1539 | *BaseNode |
| 1540 | Start *token.Token |
| 1541 | End *token.Token |
| 1542 | IsFlowStyle bool |
| 1543 | Values []Node |
| 1544 | ValueHeadComments []*CommentGroupNode |
| 1545 | Entries []*SequenceEntryNode |
| 1546 | FootComment *CommentGroupNode |
| 1547 | } |
| 1548 | |
| 1549 | // Replace replace value node. |
| 1550 | func (n *SequenceNode) Replace(idx int, value Node) error { |
| 1551 | if len(n.Values) <= idx { |
| 1552 | return fmt.Errorf( |
| 1553 | "invalid index for sequence: sequence length is %d, but specified %d index", |
| 1554 | len(n.Values), idx, |
| 1555 | ) |
| 1556 | } |
| 1557 | column := n.Values[idx].GetToken().Position.Column - value.GetToken().Position.Column |
| 1558 | value.AddColumn(column) |
| 1559 | n.Values[idx] = value |
| 1560 | return nil |
| 1561 | } |
| 1562 | |
| 1563 | // Merge merge sequence value. |
| 1564 | func (n *SequenceNode) Merge(target *SequenceNode) { |
| 1565 | column := n.Start.Position.Column - target.Start.Position.Column |
| 1566 | target.AddColumn(column) |
| 1567 | n.Values = append(n.Values, target.Values...) |
| 1568 | if len(target.ValueHeadComments) == 0 { |
| 1569 | n.ValueHeadComments = append(n.ValueHeadComments, make([]*CommentGroupNode, len(target.Values))...) |
| 1570 | return |
| 1571 | } |
| 1572 | n.ValueHeadComments = append(n.ValueHeadComments, target.ValueHeadComments...) |
| 1573 | } |
| 1574 | |
| 1575 | // SetIsFlowStyle set value to IsFlowStyle field recursively. |
| 1576 | func (n *SequenceNode) SetIsFlowStyle(isFlow bool) { |
| 1577 | n.IsFlowStyle = isFlow |
| 1578 | for _, value := range n.Values { |
| 1579 | switch value := value.(type) { |
| 1580 | case *MappingNode: |
| 1581 | value.SetIsFlowStyle(isFlow) |
| 1582 | case *MappingValueNode: |
| 1583 | value.SetIsFlowStyle(isFlow) |
| 1584 | case *SequenceNode: |
| 1585 | value.SetIsFlowStyle(isFlow) |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | // Read implements (io.Reader).Read |
| 1591 | func (n *SequenceNode) Read(p []byte) (int, error) { |
| 1592 | return readNode(p, n) |
| 1593 | } |
| 1594 | |
| 1595 | // Type returns SequenceType |
| 1596 | func (n *SequenceNode) Type() NodeType { return SequenceType } |
| 1597 | |
| 1598 | // GetToken returns token instance |
| 1599 | func (n *SequenceNode) GetToken() *token.Token { |
| 1600 | return n.Start |
| 1601 | } |
| 1602 | |
| 1603 | // AddColumn add column number to child nodes recursively |
| 1604 | func (n *SequenceNode) AddColumn(col int) { |
| 1605 | n.Start.AddColumn(col) |
| 1606 | n.End.AddColumn(col) |
| 1607 | for _, value := range n.Values { |
| 1608 | value.AddColumn(col) |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | func (n *SequenceNode) flowStyleString() string { |
| 1613 | values := []string{} |
| 1614 | for _, value := range n.Values { |
| 1615 | values = append(values, value.String()) |
| 1616 | } |
| 1617 | return fmt.Sprintf("[%s]", strings.Join(values, ", ")) |
| 1618 | } |
| 1619 | |
| 1620 | func (n *SequenceNode) blockStyleString() string { |
| 1621 | space := strings.Repeat(" ", n.Start.Position.Column-1) |
| 1622 | values := []string{} |
| 1623 | if n.Comment != nil { |
| 1624 | values = append(values, n.Comment.StringWithSpace(n.Start.Position.Column-1)) |
| 1625 | } |
| 1626 | |
| 1627 | for idx, value := range n.Values { |
| 1628 | if value == nil { |
| 1629 | continue |
| 1630 | } |
| 1631 | valueStr := value.String() |
| 1632 | newLinePrefix := "" |
| 1633 | if strings.HasPrefix(valueStr, "\n") { |
| 1634 | valueStr = valueStr[1:] |
| 1635 | newLinePrefix = "\n" |
| 1636 | } |
| 1637 | splittedValues := strings.Split(valueStr, "\n") |
| 1638 | trimmedFirstValue := strings.TrimLeft(splittedValues[0], " ") |
| 1639 | diffLength := len(splittedValues[0]) - len(trimmedFirstValue) |
| 1640 | if len(splittedValues) > 1 && value.Type() == StringType || value.Type() == LiteralType { |
| 1641 | // If multi-line string, the space characters for indent have already been added, so delete them. |
| 1642 | prefix := space + " " |
| 1643 | for i := 1; i < len(splittedValues); i++ { |
| 1644 | splittedValues[i] = strings.TrimPrefix(splittedValues[i], prefix) |
| 1645 | } |
| 1646 | } |
| 1647 | newValues := []string{trimmedFirstValue} |
| 1648 | for i := 1; i < len(splittedValues); i++ { |
| 1649 | if len(splittedValues[i]) <= diffLength { |
| 1650 | // this line is \n or white space only |
| 1651 | newValues = append(newValues, "") |
| 1652 | continue |
| 1653 | } |
| 1654 | trimmed := splittedValues[i][diffLength:] |
| 1655 | newValues = append(newValues, fmt.Sprintf("%s %s", space, trimmed)) |
| 1656 | } |
| 1657 | newValue := strings.Join(newValues, "\n") |
| 1658 | if len(n.ValueHeadComments) == len(n.Values) && n.ValueHeadComments[idx] != nil { |
| 1659 | values = append(values, fmt.Sprintf("%s%s", newLinePrefix, n.ValueHeadComments[idx].StringWithSpace(n.Start.Position.Column-1))) |
| 1660 | newLinePrefix = "" |
| 1661 | } |
| 1662 | values = append(values, fmt.Sprintf("%s%s- %s", newLinePrefix, space, newValue)) |
| 1663 | } |
| 1664 | if n.FootComment != nil { |
| 1665 | values = append(values, n.FootComment.StringWithSpace(n.Start.Position.Column-1)) |
| 1666 | } |
| 1667 | return strings.Join(values, "\n") |
| 1668 | } |
| 1669 | |
| 1670 | // String sequence to text |
| 1671 | func (n *SequenceNode) String() string { |
| 1672 | if n.IsFlowStyle || len(n.Values) == 0 { |
| 1673 | return n.flowStyleString() |
| 1674 | } |
| 1675 | return n.blockStyleString() |
| 1676 | } |
| 1677 | |
| 1678 | // ArrayRange implements ArrayNode protocol |
| 1679 | func (n *SequenceNode) ArrayRange() *ArrayNodeIter { |
| 1680 | return &ArrayNodeIter{ |
| 1681 | idx: startRangeIndex, |
| 1682 | values: n.Values, |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | // MarshalYAML encodes to a YAML text |
| 1687 | func (n *SequenceNode) MarshalYAML() ([]byte, error) { |
| 1688 | return []byte(n.String()), nil |
| 1689 | } |
| 1690 | |
| 1691 | // SequenceEntryNode is the sequence entry. |
| 1692 | type SequenceEntryNode struct { |
| 1693 | *BaseNode |
| 1694 | HeadComment *CommentGroupNode // head comment. |
| 1695 | LineComment *CommentGroupNode // line comment e.g.) - # comment. |
| 1696 | Start *token.Token // entry token. |
| 1697 | Value Node // value node. |
| 1698 | } |
| 1699 | |
| 1700 | // String node to text |
| 1701 | func (n *SequenceEntryNode) String() string { |
| 1702 | return "" // TODO |
| 1703 | } |
| 1704 | |
| 1705 | // GetToken returns token instance |
| 1706 | func (n *SequenceEntryNode) GetToken() *token.Token { |
| 1707 | return n.Start |
| 1708 | } |
| 1709 | |
| 1710 | // Type returns type of node |
| 1711 | func (n *SequenceEntryNode) Type() NodeType { |
| 1712 | return SequenceEntryType |
| 1713 | } |
| 1714 | |
| 1715 | // AddColumn add column number to child nodes recursively |
| 1716 | func (n *SequenceEntryNode) AddColumn(col int) { |
| 1717 | n.Start.AddColumn(col) |
| 1718 | } |
| 1719 | |
| 1720 | // SetComment set line comment. |
| 1721 | func (n *SequenceEntryNode) SetComment(cm *CommentGroupNode) error { |
| 1722 | n.LineComment = cm |
| 1723 | return nil |
| 1724 | } |
| 1725 | |
| 1726 | // Comment returns comment token instance |
| 1727 | func (n *SequenceEntryNode) GetComment() *CommentGroupNode { |
| 1728 | return n.LineComment |
| 1729 | } |
| 1730 | |
| 1731 | // MarshalYAML |
| 1732 | func (n *SequenceEntryNode) MarshalYAML() ([]byte, error) { |
| 1733 | return []byte(n.String()), nil |
| 1734 | } |
| 1735 | |
| 1736 | func (n *SequenceEntryNode) Read(p []byte) (int, error) { |
| 1737 | return readNode(p, n) |
| 1738 | } |
| 1739 | |
| 1740 | // SequenceEntry creates SequenceEntryNode instance. |
| 1741 | func SequenceEntry(start *token.Token, value Node, headComment *CommentGroupNode) *SequenceEntryNode { |
| 1742 | return &SequenceEntryNode{ |
| 1743 | BaseNode: &BaseNode{}, |
| 1744 | HeadComment: headComment, |
| 1745 | Start: start, |
| 1746 | Value: value, |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | // SequenceMergeValue creates SequenceMergeValueNode instance. |
| 1751 | func SequenceMergeValue(values ...MapNode) *SequenceMergeValueNode { |
| 1752 | return &SequenceMergeValueNode{ |
| 1753 | values: values, |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | // SequenceMergeValueNode is used to convert the Sequence node specified for the merge key into a MapNode format. |
| 1758 | type SequenceMergeValueNode struct { |
| 1759 | values []MapNode |
| 1760 | } |
| 1761 | |
| 1762 | // MapRange returns MapNodeIter instance. |
| 1763 | func (n *SequenceMergeValueNode) MapRange() *MapNodeIter { |
| 1764 | ret := &MapNodeIter{idx: startRangeIndex} |
| 1765 | for _, value := range n.values { |
| 1766 | iter := value.MapRange() |
| 1767 | ret.values = append(ret.values, iter.values...) |
| 1768 | } |
| 1769 | return ret |
| 1770 | } |
| 1771 | |
| 1772 | // AnchorNode type of anchor node |
| 1773 | type AnchorNode struct { |
| 1774 | *BaseNode |
| 1775 | Start *token.Token |
| 1776 | Name Node |
| 1777 | Value Node |
| 1778 | } |
| 1779 | |
| 1780 | func (n *AnchorNode) stringWithoutComment() string { |
| 1781 | return n.Value.String() |
| 1782 | } |
| 1783 | |
| 1784 | func (n *AnchorNode) SetName(name string) error { |
| 1785 | if n.Name == nil { |
| 1786 | return ErrInvalidAnchorName |
| 1787 | } |
| 1788 | s, ok := n.Name.(*StringNode) |
| 1789 | if !ok { |
| 1790 | return ErrInvalidAnchorName |
| 1791 | } |
| 1792 | s.Value = name |
| 1793 | return nil |
| 1794 | } |
| 1795 | |
| 1796 | // Read implements (io.Reader).Read |
| 1797 | func (n *AnchorNode) Read(p []byte) (int, error) { |
| 1798 | return readNode(p, n) |
| 1799 | } |
| 1800 | |
| 1801 | // Type returns AnchorType |
| 1802 | func (n *AnchorNode) Type() NodeType { return AnchorType } |
| 1803 | |
| 1804 | // GetToken returns token instance |
| 1805 | func (n *AnchorNode) GetToken() *token.Token { |
| 1806 | return n.Start |
| 1807 | } |
| 1808 | |
| 1809 | func (n *AnchorNode) GetValue() any { |
| 1810 | return n.Value.GetToken().Value |
| 1811 | } |
| 1812 | |
| 1813 | // AddColumn add column number to child nodes recursively |
| 1814 | func (n *AnchorNode) AddColumn(col int) { |
| 1815 | n.Start.AddColumn(col) |
| 1816 | if n.Name != nil { |
| 1817 | n.Name.AddColumn(col) |
| 1818 | } |
| 1819 | if n.Value != nil { |
| 1820 | n.Value.AddColumn(col) |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | // String anchor to text |
| 1825 | func (n *AnchorNode) String() string { |
| 1826 | anchor := "&" + n.Name.String() |
| 1827 | value := n.Value.String() |
| 1828 | if s, ok := n.Value.(*SequenceNode); ok && !s.IsFlowStyle { |
| 1829 | return fmt.Sprintf("%s\n%s", anchor, value) |
| 1830 | } else if m, ok := n.Value.(*MappingNode); ok && !m.IsFlowStyle { |
| 1831 | return fmt.Sprintf("%s\n%s", anchor, value) |
| 1832 | } |
| 1833 | if value == "" { |
| 1834 | // implicit null value. |
| 1835 | return anchor |
| 1836 | } |
| 1837 | return fmt.Sprintf("%s %s", anchor, value) |
| 1838 | } |
| 1839 | |
| 1840 | // MarshalYAML encodes to a YAML text |
| 1841 | func (n *AnchorNode) MarshalYAML() ([]byte, error) { |
| 1842 | return []byte(n.String()), nil |
| 1843 | } |
| 1844 | |
| 1845 | // IsMergeKey returns whether it is a MergeKey node. |
| 1846 | func (n *AnchorNode) IsMergeKey() bool { |
| 1847 | if n.Value == nil { |
| 1848 | return false |
| 1849 | } |
| 1850 | key, ok := n.Value.(MapKeyNode) |
| 1851 | if !ok { |
| 1852 | return false |
| 1853 | } |
| 1854 | return key.IsMergeKey() |
| 1855 | } |
| 1856 | |
| 1857 | // AliasNode type of alias node |
| 1858 | type AliasNode struct { |
| 1859 | *BaseNode |
| 1860 | Start *token.Token |
| 1861 | Value Node |
| 1862 | } |
| 1863 | |
| 1864 | func (n *AliasNode) stringWithoutComment() string { |
| 1865 | return n.Value.String() |
| 1866 | } |
| 1867 | |
| 1868 | func (n *AliasNode) SetName(name string) error { |
| 1869 | if n.Value == nil { |
| 1870 | return ErrInvalidAliasName |
| 1871 | } |
| 1872 | s, ok := n.Value.(*StringNode) |
| 1873 | if !ok { |
| 1874 | return ErrInvalidAliasName |
| 1875 | } |
| 1876 | s.Value = name |
| 1877 | return nil |
| 1878 | } |
| 1879 | |
| 1880 | // Read implements (io.Reader).Read |
| 1881 | func (n *AliasNode) Read(p []byte) (int, error) { |
| 1882 | return readNode(p, n) |
| 1883 | } |
| 1884 | |
| 1885 | // Type returns AliasType |
| 1886 | func (n *AliasNode) Type() NodeType { return AliasType } |
| 1887 | |
| 1888 | // GetToken returns token instance |
| 1889 | func (n *AliasNode) GetToken() *token.Token { |
| 1890 | return n.Start |
| 1891 | } |
| 1892 | |
| 1893 | func (n *AliasNode) GetValue() any { |
| 1894 | return n.Value.GetToken().Value |
| 1895 | } |
| 1896 | |
| 1897 | // AddColumn add column number to child nodes recursively |
| 1898 | func (n *AliasNode) AddColumn(col int) { |
| 1899 | n.Start.AddColumn(col) |
| 1900 | if n.Value != nil { |
| 1901 | n.Value.AddColumn(col) |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | // String alias to text |
| 1906 | func (n *AliasNode) String() string { |
| 1907 | return fmt.Sprintf("*%s", n.Value.String()) |
| 1908 | } |
| 1909 | |
| 1910 | // MarshalYAML encodes to a YAML text |
| 1911 | func (n *AliasNode) MarshalYAML() ([]byte, error) { |
| 1912 | return []byte(n.String()), nil |
| 1913 | } |
| 1914 | |
| 1915 | // IsMergeKey returns whether it is a MergeKey node. |
| 1916 | func (n *AliasNode) IsMergeKey() bool { |
| 1917 | return false |
| 1918 | } |
| 1919 | |
| 1920 | // DirectiveNode type of directive node |
| 1921 | type DirectiveNode struct { |
| 1922 | *BaseNode |
| 1923 | // Start is '%' token. |
| 1924 | Start *token.Token |
| 1925 | // Name is directive name e.g.) "YAML" or "TAG". |
| 1926 | Name Node |
| 1927 | // Values is directive values e.g.) "1.2" or "!!" and "tag:clarkevans.com,2002:app/". |
| 1928 | Values []Node |
| 1929 | } |
| 1930 | |
| 1931 | // Read implements (io.Reader).Read |
| 1932 | func (n *DirectiveNode) Read(p []byte) (int, error) { |
| 1933 | return readNode(p, n) |
| 1934 | } |
| 1935 | |
| 1936 | // Type returns DirectiveType |
| 1937 | func (n *DirectiveNode) Type() NodeType { return DirectiveType } |
| 1938 | |
| 1939 | // GetToken returns token instance |
| 1940 | func (n *DirectiveNode) GetToken() *token.Token { |
| 1941 | return n.Start |
| 1942 | } |
| 1943 | |
| 1944 | // AddColumn add column number to child nodes recursively |
| 1945 | func (n *DirectiveNode) AddColumn(col int) { |
| 1946 | if n.Name != nil { |
| 1947 | n.Name.AddColumn(col) |
| 1948 | } |
| 1949 | for _, value := range n.Values { |
| 1950 | value.AddColumn(col) |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | // String directive to text |
| 1955 | func (n *DirectiveNode) String() string { |
| 1956 | values := make([]string, 0, len(n.Values)) |
| 1957 | for _, val := range n.Values { |
| 1958 | values = append(values, val.String()) |
| 1959 | } |
| 1960 | return strings.Join(append([]string{"%" + n.Name.String()}, values...), " ") |
| 1961 | } |
| 1962 | |
| 1963 | // MarshalYAML encodes to a YAML text |
| 1964 | func (n *DirectiveNode) MarshalYAML() ([]byte, error) { |
| 1965 | return []byte(n.String()), nil |
| 1966 | } |
| 1967 | |
| 1968 | // TagNode type of tag node |
| 1969 | type TagNode struct { |
| 1970 | *BaseNode |
| 1971 | Directive *DirectiveNode |
| 1972 | Start *token.Token |
| 1973 | Value Node |
| 1974 | } |
| 1975 | |
| 1976 | func (n *TagNode) GetValue() any { |
| 1977 | scalar, ok := n.Value.(ScalarNode) |
| 1978 | if !ok { |
| 1979 | return nil |
| 1980 | } |
| 1981 | return scalar.GetValue() |
| 1982 | } |
| 1983 | |
| 1984 | func (n *TagNode) stringWithoutComment() string { |
| 1985 | return n.Value.String() |
| 1986 | } |
| 1987 | |
| 1988 | // Read implements (io.Reader).Read |
| 1989 | func (n *TagNode) Read(p []byte) (int, error) { |
| 1990 | return readNode(p, n) |
| 1991 | } |
| 1992 | |
| 1993 | // Type returns TagType |
| 1994 | func (n *TagNode) Type() NodeType { return TagType } |
| 1995 | |
| 1996 | // GetToken returns token instance |
| 1997 | func (n *TagNode) GetToken() *token.Token { |
| 1998 | return n.Start |
| 1999 | } |
| 2000 | |
| 2001 | // AddColumn add column number to child nodes recursively |
| 2002 | func (n *TagNode) AddColumn(col int) { |
| 2003 | n.Start.AddColumn(col) |
| 2004 | if n.Value != nil { |
| 2005 | n.Value.AddColumn(col) |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | // String tag to text |
| 2010 | func (n *TagNode) String() string { |
| 2011 | value := n.Value.String() |
| 2012 | if s, ok := n.Value.(*SequenceNode); ok && !s.IsFlowStyle { |
| 2013 | return fmt.Sprintf("%s\n%s", n.Start.Value, value) |
| 2014 | } else if m, ok := n.Value.(*MappingNode); ok && !m.IsFlowStyle { |
| 2015 | return fmt.Sprintf("%s\n%s", n.Start.Value, value) |
| 2016 | } |
| 2017 | |
| 2018 | return fmt.Sprintf("%s %s", n.Start.Value, value) |
| 2019 | } |
| 2020 | |
| 2021 | // MarshalYAML encodes to a YAML text |
| 2022 | func (n *TagNode) MarshalYAML() ([]byte, error) { |
| 2023 | return []byte(n.String()), nil |
| 2024 | } |
| 2025 | |
| 2026 | // IsMergeKey returns whether it is a MergeKey node. |
| 2027 | func (n *TagNode) IsMergeKey() bool { |
| 2028 | if n.Value == nil { |
| 2029 | return false |
| 2030 | } |
| 2031 | key, ok := n.Value.(MapKeyNode) |
| 2032 | if !ok { |
| 2033 | return false |
| 2034 | } |
| 2035 | return key.IsMergeKey() |
| 2036 | } |
| 2037 | |
| 2038 | func (n *TagNode) ArrayRange() *ArrayNodeIter { |
| 2039 | arr, ok := n.Value.(ArrayNode) |
| 2040 | if !ok { |
| 2041 | return nil |
| 2042 | } |
| 2043 | return arr.ArrayRange() |
| 2044 | } |
| 2045 | |
| 2046 | // CommentNode type of comment node |
| 2047 | type CommentNode struct { |
| 2048 | *BaseNode |
| 2049 | Token *token.Token |
| 2050 | } |
| 2051 | |
| 2052 | // Read implements (io.Reader).Read |
| 2053 | func (n *CommentNode) Read(p []byte) (int, error) { |
| 2054 | return readNode(p, n) |
| 2055 | } |
| 2056 | |
| 2057 | // Type returns TagType |
| 2058 | func (n *CommentNode) Type() NodeType { return CommentType } |
| 2059 | |
| 2060 | // GetToken returns token instance |
| 2061 | func (n *CommentNode) GetToken() *token.Token { return n.Token } |
| 2062 | |
| 2063 | // AddColumn add column number to child nodes recursively |
| 2064 | func (n *CommentNode) AddColumn(col int) { |
| 2065 | if n.Token == nil { |
| 2066 | return |
| 2067 | } |
| 2068 | n.Token.AddColumn(col) |
| 2069 | } |
| 2070 | |
| 2071 | // String comment to text |
| 2072 | func (n *CommentNode) String() string { |
| 2073 | return fmt.Sprintf("#%s", n.Token.Value) |
| 2074 | } |
| 2075 | |
| 2076 | // MarshalYAML encodes to a YAML text |
| 2077 | func (n *CommentNode) MarshalYAML() ([]byte, error) { |
| 2078 | return []byte(n.String()), nil |
| 2079 | } |
| 2080 | |
| 2081 | // CommentGroupNode type of comment node |
| 2082 | type CommentGroupNode struct { |
| 2083 | *BaseNode |
| 2084 | Comments []*CommentNode |
| 2085 | } |
| 2086 | |
| 2087 | // Read implements (io.Reader).Read |
| 2088 | func (n *CommentGroupNode) Read(p []byte) (int, error) { |
| 2089 | return readNode(p, n) |
| 2090 | } |
| 2091 | |
| 2092 | // Type returns TagType |
| 2093 | func (n *CommentGroupNode) Type() NodeType { return CommentType } |
| 2094 | |
| 2095 | // GetToken returns token instance |
| 2096 | func (n *CommentGroupNode) GetToken() *token.Token { |
| 2097 | if len(n.Comments) > 0 { |
| 2098 | return n.Comments[0].Token |
| 2099 | } |
| 2100 | return nil |
| 2101 | } |
| 2102 | |
| 2103 | // AddColumn add column number to child nodes recursively |
| 2104 | func (n *CommentGroupNode) AddColumn(col int) { |
| 2105 | for _, comment := range n.Comments { |
| 2106 | comment.AddColumn(col) |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | // String comment to text |
| 2111 | func (n *CommentGroupNode) String() string { |
| 2112 | values := []string{} |
| 2113 | for _, comment := range n.Comments { |
| 2114 | values = append(values, comment.String()) |
| 2115 | } |
| 2116 | return strings.Join(values, "\n") |
| 2117 | } |
| 2118 | |
| 2119 | func (n *CommentGroupNode) StringWithSpace(col int) string { |
| 2120 | values := []string{} |
| 2121 | space := strings.Repeat(" ", col) |
| 2122 | for _, comment := range n.Comments { |
| 2123 | space := space |
| 2124 | if checkLineBreak(comment.Token) { |
| 2125 | space = fmt.Sprintf("%s%s", "\n", space) |
| 2126 | } |
| 2127 | values = append(values, space+comment.String()) |
| 2128 | } |
| 2129 | return strings.Join(values, "\n") |
| 2130 | } |
| 2131 | |
| 2132 | // MarshalYAML encodes to a YAML text |
| 2133 | func (n *CommentGroupNode) MarshalYAML() ([]byte, error) { |
| 2134 | return []byte(n.String()), nil |
| 2135 | } |
| 2136 | |
| 2137 | // Visitor has Visit method that is invokded for each node encountered by Walk. |
| 2138 | // If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, |
| 2139 | // followed by a call of w.Visit(nil). |
| 2140 | type Visitor interface { |
| 2141 | Visit(Node) Visitor |
| 2142 | } |
| 2143 | |
| 2144 | // Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil. |
| 2145 | // If the visitor w returned by v.Visit(node) is not nil, |
| 2146 | // Walk is invoked recursively with visitor w for each of the non-nil children of node, |
| 2147 | // followed by a call of w.Visit(nil). |
| 2148 | func Walk(v Visitor, node Node) { |
| 2149 | if v = v.Visit(node); v == nil { |
| 2150 | return |
| 2151 | } |
| 2152 | |
| 2153 | switch n := node.(type) { |
| 2154 | case *CommentNode: |
| 2155 | case *NullNode: |
| 2156 | walkComment(v, n.BaseNode) |
| 2157 | case *IntegerNode: |
| 2158 | walkComment(v, n.BaseNode) |
| 2159 | case *FloatNode: |
| 2160 | walkComment(v, n.BaseNode) |
| 2161 | case *StringNode: |
| 2162 | walkComment(v, n.BaseNode) |
| 2163 | case *MergeKeyNode: |
| 2164 | walkComment(v, n.BaseNode) |
| 2165 | case *BoolNode: |
| 2166 | walkComment(v, n.BaseNode) |
| 2167 | case *InfinityNode: |
| 2168 | walkComment(v, n.BaseNode) |
| 2169 | case *NanNode: |
| 2170 | walkComment(v, n.BaseNode) |
| 2171 | case *LiteralNode: |
| 2172 | walkComment(v, n.BaseNode) |
| 2173 | Walk(v, n.Value) |
| 2174 | case *DirectiveNode: |
| 2175 | walkComment(v, n.BaseNode) |
| 2176 | Walk(v, n.Name) |
| 2177 | for _, value := range n.Values { |
| 2178 | Walk(v, value) |
| 2179 | } |
| 2180 | case *TagNode: |
| 2181 | walkComment(v, n.BaseNode) |
| 2182 | Walk(v, n.Value) |
| 2183 | case *DocumentNode: |
| 2184 | walkComment(v, n.BaseNode) |
| 2185 | Walk(v, n.Body) |
| 2186 | case *MappingNode: |
| 2187 | walkComment(v, n.BaseNode) |
| 2188 | for _, value := range n.Values { |
| 2189 | Walk(v, value) |
| 2190 | } |
| 2191 | case *MappingKeyNode: |
| 2192 | walkComment(v, n.BaseNode) |
| 2193 | Walk(v, n.Value) |
| 2194 | case *MappingValueNode: |
| 2195 | walkComment(v, n.BaseNode) |
| 2196 | Walk(v, n.Key) |
| 2197 | Walk(v, n.Value) |
| 2198 | case *SequenceNode: |
| 2199 | walkComment(v, n.BaseNode) |
| 2200 | for _, value := range n.Values { |
| 2201 | Walk(v, value) |
| 2202 | } |
| 2203 | case *AnchorNode: |
| 2204 | walkComment(v, n.BaseNode) |
| 2205 | Walk(v, n.Name) |
| 2206 | Walk(v, n.Value) |
| 2207 | case *AliasNode: |
| 2208 | walkComment(v, n.BaseNode) |
| 2209 | Walk(v, n.Value) |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | func walkComment(v Visitor, base *BaseNode) { |
| 2214 | if base == nil { |
| 2215 | return |
| 2216 | } |
| 2217 | if base.Comment == nil { |
| 2218 | return |
| 2219 | } |
| 2220 | Walk(v, base.Comment) |
| 2221 | } |
| 2222 | |
| 2223 | type filterWalker struct { |
| 2224 | typ NodeType |
| 2225 | results []Node |
| 2226 | } |
| 2227 | |
| 2228 | func (v *filterWalker) Visit(n Node) Visitor { |
| 2229 | if v.typ == n.Type() { |
| 2230 | v.results = append(v.results, n) |
| 2231 | } |
| 2232 | return v |
| 2233 | } |
| 2234 | |
| 2235 | type parentFinder struct { |
| 2236 | target Node |
| 2237 | } |
| 2238 | |
| 2239 | func (f *parentFinder) walk(parent, node Node) Node { |
| 2240 | if f.target == node { |
| 2241 | return parent |
| 2242 | } |
| 2243 | switch n := node.(type) { |
| 2244 | case *CommentNode: |
| 2245 | return nil |
| 2246 | case *NullNode: |
| 2247 | return nil |
| 2248 | case *IntegerNode: |
| 2249 | return nil |
| 2250 | case *FloatNode: |
| 2251 | return nil |
| 2252 | case *StringNode: |
| 2253 | return nil |
| 2254 | case *MergeKeyNode: |
| 2255 | return nil |
| 2256 | case *BoolNode: |
| 2257 | return nil |
| 2258 | case *InfinityNode: |
| 2259 | return nil |
| 2260 | case *NanNode: |
| 2261 | return nil |
| 2262 | case *LiteralNode: |
| 2263 | return f.walk(node, n.Value) |
| 2264 | case *DirectiveNode: |
| 2265 | if found := f.walk(node, n.Name); found != nil { |
| 2266 | return found |
| 2267 | } |
| 2268 | for _, value := range n.Values { |
| 2269 | if found := f.walk(node, value); found != nil { |
| 2270 | return found |
| 2271 | } |
| 2272 | } |
| 2273 | case *TagNode: |
| 2274 | return f.walk(node, n.Value) |
| 2275 | case *DocumentNode: |
| 2276 | return f.walk(node, n.Body) |
| 2277 | case *MappingNode: |
| 2278 | for _, value := range n.Values { |
| 2279 | if found := f.walk(node, value); found != nil { |
| 2280 | return found |
| 2281 | } |
| 2282 | } |
| 2283 | case *MappingKeyNode: |
| 2284 | return f.walk(node, n.Value) |
| 2285 | case *MappingValueNode: |
| 2286 | if found := f.walk(node, n.Key); found != nil { |
| 2287 | return found |
| 2288 | } |
| 2289 | return f.walk(node, n.Value) |
| 2290 | case *SequenceNode: |
| 2291 | for _, value := range n.Values { |
| 2292 | if found := f.walk(node, value); found != nil { |
| 2293 | return found |
| 2294 | } |
| 2295 | } |
| 2296 | case *AnchorNode: |
| 2297 | if found := f.walk(node, n.Name); found != nil { |
| 2298 | return found |
| 2299 | } |
| 2300 | return f.walk(node, n.Value) |
| 2301 | case *AliasNode: |
| 2302 | return f.walk(node, n.Value) |
| 2303 | } |
| 2304 | return nil |
| 2305 | } |
| 2306 | |
| 2307 | // Parent get parent node from child node. |
| 2308 | func Parent(root, child Node) Node { |
| 2309 | finder := &parentFinder{target: child} |
| 2310 | return finder.walk(root, root) |
| 2311 | } |
| 2312 | |
| 2313 | // Filter returns a list of nodes that match the given type. |
| 2314 | func Filter(typ NodeType, node Node) []Node { |
| 2315 | walker := &filterWalker{typ: typ} |
| 2316 | Walk(walker, node) |
| 2317 | return walker.results |
| 2318 | } |
| 2319 | |
| 2320 | // FilterFile returns a list of nodes that match the given type. |
| 2321 | func FilterFile(typ NodeType, file *File) []Node { |
| 2322 | results := []Node{} |
| 2323 | for _, doc := range file.Docs { |
| 2324 | walker := &filterWalker{typ: typ} |
| 2325 | Walk(walker, doc) |
| 2326 | results = append(results, walker.results...) |
| 2327 | } |
| 2328 | return results |
| 2329 | } |
| 2330 | |
| 2331 | type ErrInvalidMergeType struct { |
| 2332 | dst Node |
| 2333 | src Node |
| 2334 | } |
| 2335 | |
| 2336 | func (e *ErrInvalidMergeType) Error() string { |
| 2337 | return fmt.Sprintf("cannot merge %s into %s", e.src.Type(), e.dst.Type()) |
| 2338 | } |
| 2339 | |
| 2340 | // Merge merge document, map, sequence node. |
| 2341 | func Merge(dst Node, src Node) error { |
| 2342 | if doc, ok := src.(*DocumentNode); ok { |
| 2343 | src = doc.Body |
| 2344 | } |
| 2345 | err := &ErrInvalidMergeType{dst: dst, src: src} |
| 2346 | switch dst.Type() { |
| 2347 | case DocumentType: |
| 2348 | node, _ := dst.(*DocumentNode) |
| 2349 | return Merge(node.Body, src) |
| 2350 | case MappingType: |
| 2351 | node, _ := dst.(*MappingNode) |
| 2352 | target, ok := src.(*MappingNode) |
| 2353 | if !ok { |
| 2354 | return err |
| 2355 | } |
| 2356 | node.Merge(target) |
| 2357 | return nil |
| 2358 | case SequenceType: |
| 2359 | node, _ := dst.(*SequenceNode) |
| 2360 | target, ok := src.(*SequenceNode) |
| 2361 | if !ok { |
| 2362 | return err |
| 2363 | } |
| 2364 | node.Merge(target) |
| 2365 | return nil |
| 2366 | } |
| 2367 | return err |
| 2368 | } |