| 1 | // Copyright 2025 The go-yaml Project Contributors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // Desolver stage: Removes inferable tags from YAML nodes. |
| 5 | // This is the inverse of the Resolver - it walks a tagged node tree and |
| 6 | // removes tags that can be inferred during parsing, producing cleaner YAML |
| 7 | // output without unnecessary type annotations. |
| 8 | |
| 9 | package libyaml |
| 10 | |
| 11 | // Desolver handles tag removal for YAML nodes during serialization. |
| 12 | // It removes tags that would be automatically resolved to the same type |
| 13 | // during parsing, making the output cleaner and more readable. |
| 14 | type Desolver struct { |
| 15 | opts *Options |
| 16 | } |
| 17 | |
| 18 | // NewDesolver creates a new Desolver with the given options. |
| 19 | func NewDesolver(opts *Options) *Desolver { |
| 20 | return &Desolver{opts: opts} |
| 21 | } |
| 22 | |
| 23 | // Desolve walks the node tree and removes tags that can be inferred. |
| 24 | // This is the inverse of Resolver - it takes a fully-tagged node tree |
| 25 | // (from Representer) and removes unnecessary tags to produce clean output. |
| 26 | // |
| 27 | // For scalar nodes: if the value would resolve to the same tag when parsed, |
| 28 | // the tag is removed. For strings that would resolve differently, the tag is |
| 29 | // removed and quoting style is set to preserve the string type. |
| 30 | // |
| 31 | // For collection nodes (maps/sequences): default tags (!!map, !!seq) are |
| 32 | // removed since they're implied by the structure. |
| 33 | func (d *Desolver) Desolve(n *Node) { |
| 34 | if n == nil { |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | switch n.Kind { |
| 39 | case ScalarNode: |
| 40 | d.desolveScalar(n) |
| 41 | case DocumentNode, SequenceNode, MappingNode: |
| 42 | d.desolveCollection(n) |
| 43 | // Recursively desolve children |
| 44 | for _, child := range n.Content { |
| 45 | d.Desolve(child) |
| 46 | } |
| 47 | case AliasNode: |
| 48 | // Alias nodes don't have tags to remove |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // desolveScalar removes tags from scalar nodes when they can be inferred. |
| 53 | func (d *Desolver) desolveScalar(n *Node) { |
| 54 | // If explicitly tagged by user (TaggedStyle), keep it |
| 55 | if n.Style&TaggedStyle != 0 { |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | // Empty tag means it's already untagged - nothing to do |
| 60 | if n.Tag == "" { |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | stag := shortTag(n.Tag) |
| 65 | |
| 66 | // Check if this is a standard scalar tag that we can potentially remove |
| 67 | isStandardTag := false |
| 68 | switch stag { |
| 69 | case nullTag, boolTag, strTag, intTag, floatTag, timestampTag: |
| 70 | isStandardTag = true |
| 71 | case binaryTag: |
| 72 | // Binary scalars are not implicitly resolvable - never remove. |
| 73 | return |
| 74 | case mergeTag: |
| 75 | // Elide the implicit !!merge tag when the value is the canonical |
| 76 | // merge key marker. The TaggedStyle early-return above already |
| 77 | // preserves !!merge when it was explicit in the source. |
| 78 | if n.Value == "<<" { |
| 79 | n.Tag = "" |
| 80 | } |
| 81 | return |
| 82 | default: |
| 83 | // Custom tag - preserve it |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | // Only process standard tags from here |
| 88 | if !isStandardTag { |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | // What tag would this value resolve to? |
| 93 | rtag, _ := resolve("", n.Value) |
| 94 | |
| 95 | // If resolved tag matches current tag, we can elide the tag |
| 96 | if rtag == stag { |
| 97 | // Tag can be inferred - remove it |
| 98 | n.Tag = "" |
| 99 | } else if stag == strTag { |
| 100 | // This is a string type, but would resolve to something else. |
| 101 | // Remove the tag and force quoting to preserve string type. |
| 102 | n.Tag = "" |
| 103 | // If not already quoted, set quote style based on content |
| 104 | if n.Style&(SingleQuotedStyle|DoubleQuotedStyle|LiteralStyle|FoldedStyle) == 0 { |
| 105 | // Determine quote style based on options or default to single quotes |
| 106 | if d.opts != nil { |
| 107 | // Convert ScalarStyle to Style |
| 108 | switch d.opts.QuotePreference.ScalarStyle() { |
| 109 | case DOUBLE_QUOTED_SCALAR_STYLE: |
| 110 | n.Style |= DoubleQuotedStyle |
| 111 | default: |
| 112 | n.Style |= SingleQuotedStyle |
| 113 | } |
| 114 | } else { |
| 115 | n.Style |= SingleQuotedStyle |
| 116 | } |
| 117 | } |
| 118 | } else if stag == floatTag || stag == intTag { |
| 119 | // For numeric type mismatches (like float64(1) → "1" with !!float tag): |
| 120 | // Elide the tag and let YAML resolve naturally. |
| 121 | // Without the tag, "1" resolves as !!int, which may change the type, |
| 122 | // but that's acceptable for cleaner output (and matches old behavior). |
| 123 | n.Tag = "" |
| 124 | } |
| 125 | // For other standard tags with mismatches, keep the tag to preserve type |
| 126 | } |
| 127 | |
| 128 | // desolveCollection removes default tags from collection nodes. |
| 129 | func (d *Desolver) desolveCollection(n *Node) { |
| 130 | // If explicitly tagged by user, keep it |
| 131 | if n.Style&TaggedStyle != 0 { |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | stag := shortTag(n.Tag) |
| 136 | switch n.Kind { |
| 137 | case MappingNode: |
| 138 | // !!map is the default for mappings - remove it |
| 139 | if stag == mapTag { |
| 140 | n.Tag = "" |
| 141 | } |
| 142 | case SequenceNode: |
| 143 | // !!seq is the default for sequences - remove it |
| 144 | if stag == seqTag { |
| 145 | n.Tag = "" |
| 146 | } |
| 147 | case DocumentNode: |
| 148 | // Documents don't have tags in YAML output |
| 149 | n.Tag = "" |
| 150 | } |
| 151 | // For other tags, keep them - they're explicit type information |
| 152 | } |