|
| 1 | +// Package expand provides YAML key delimiter expansion for yaml.Node trees. |
| 2 | +// It walks mapping nodes and expands unquoted keys containing a configurable |
| 3 | +// delimiter into nested map structures, modeled after Viper's deepSearch(). |
| 4 | +package expand |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + |
| 9 | + goyaml "gopkg.in/yaml.v3" |
| 10 | + |
| 11 | + "github.com/cloudposse/atmos/pkg/perf" |
| 12 | +) |
| 13 | + |
| 14 | +// KeyDelimiters walks a yaml.Node tree and expands unquoted mapping keys |
| 15 | +// containing the delimiter into nested mapping structures. Quoted keys |
| 16 | +// (single or double) are preserved as literal keys. |
| 17 | +// |
| 18 | +// For example, with delimiter ".", the unquoted key "metadata.component: vpc-base" |
| 19 | +// becomes the nested structure "metadata: { component: vpc-base }". |
| 20 | +// |
| 21 | +// This is modeled after Viper's deepSearch() approach (viper@v1.21.0/util.go). |
| 22 | +func KeyDelimiters(node *goyaml.Node, delimiter string) { |
| 23 | + defer perf.Track(nil, "yaml.expand.KeyDelimiters")() |
| 24 | + |
| 25 | + if node == nil || delimiter == "" { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + keyDelimitersRecursive(node, delimiter) |
| 30 | +} |
| 31 | + |
| 32 | +// keyDelimitersRecursive is the recursive implementation. |
| 33 | +// Separated from the public entry point so perf.Track fires only once. |
| 34 | +func keyDelimitersRecursive(node *goyaml.Node, delimiter string) { |
| 35 | + if node == nil { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + switch node.Kind { |
| 40 | + case goyaml.DocumentNode: |
| 41 | + for _, child := range node.Content { |
| 42 | + keyDelimitersRecursive(child, delimiter) |
| 43 | + } |
| 44 | + case goyaml.MappingNode: |
| 45 | + expandMappingKeys(node, delimiter) |
| 46 | + case goyaml.SequenceNode: |
| 47 | + for _, child := range node.Content { |
| 48 | + keyDelimitersRecursive(child, delimiter) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// expandMappingKeys processes a single MappingNode, expanding unquoted delimited keys |
| 54 | +// into nested structures. |
| 55 | +func expandMappingKeys(node *goyaml.Node, delimiter string) { |
| 56 | + // First, recurse into all value nodes so nested maps are expanded bottom-up. |
| 57 | + for i := 1; i < len(node.Content); i += 2 { |
| 58 | + keyDelimitersRecursive(node.Content[i], delimiter) |
| 59 | + } |
| 60 | + |
| 61 | + // Collect expanded entries: for each expandable key, build nested nodes. |
| 62 | + // Non-expandable keys pass through unchanged. |
| 63 | + var newContent []*goyaml.Node |
| 64 | + |
| 65 | + for i := 0; i < len(node.Content); i += 2 { |
| 66 | + keyNode := node.Content[i] |
| 67 | + valueNode := node.Content[i+1] |
| 68 | + |
| 69 | + if shouldExpand(keyNode, delimiter) { |
| 70 | + parts := strings.Split(keyNode.Value, delimiter) |
| 71 | + nested := buildNestedNodes(parts, valueNode) |
| 72 | + // Merge the expanded key-value pair into newContent. |
| 73 | + mergeIntoContent(&newContent, nested[0], nested[1]) |
| 74 | + } else { |
| 75 | + // Non-expandable: merge as-is (handles duplicate keys by last-wins). |
| 76 | + mergeIntoContent(&newContent, keyNode, valueNode) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + node.Content = newContent |
| 81 | +} |
| 82 | + |
| 83 | +// shouldExpand returns true if the key node should be expanded: |
| 84 | +// - Must be a scalar node. |
| 85 | +// - Must be unquoted (Style == 0). |
| 86 | +// - Must contain the delimiter. |
| 87 | +// - Must not have leading, trailing, or consecutive delimiters. |
| 88 | +func shouldExpand(keyNode *goyaml.Node, delimiter string) bool { |
| 89 | + if keyNode.Kind != goyaml.ScalarNode { |
| 90 | + return false |
| 91 | + } |
| 92 | + |
| 93 | + // Quoted keys are never expanded. |
| 94 | + if keyNode.Style == goyaml.DoubleQuotedStyle || keyNode.Style == goyaml.SingleQuotedStyle { |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + value := keyNode.Value |
| 99 | + if !strings.Contains(value, delimiter) { |
| 100 | + return false |
| 101 | + } |
| 102 | + |
| 103 | + // Reject malformed patterns: leading, trailing, or consecutive delimiters. |
| 104 | + if strings.HasPrefix(value, delimiter) || strings.HasSuffix(value, delimiter) { |
| 105 | + return false |
| 106 | + } |
| 107 | + if strings.Contains(value, delimiter+delimiter) { |
| 108 | + return false |
| 109 | + } |
| 110 | + |
| 111 | + // All parts must be non-empty after splitting. |
| 112 | + parts := strings.Split(value, delimiter) |
| 113 | + for _, part := range parts { |
| 114 | + if part == "" { |
| 115 | + return false |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return true |
| 120 | +} |
| 121 | + |
| 122 | +// buildNestedNodes creates a chain of nested MappingNodes from the key parts. |
| 123 | +// For parts ["a", "b", "c"] and a value node, it creates: |
| 124 | +// |
| 125 | +// a: { b: { c: value } } |
| 126 | +// |
| 127 | +// Returns [keyNode, valueNode] where valueNode may be a nested MappingNode. |
| 128 | +func buildNestedNodes(parts []string, valueNode *goyaml.Node) [2]*goyaml.Node { |
| 129 | + // Build from innermost to outermost. |
| 130 | + currentValue := valueNode |
| 131 | + for i := len(parts) - 1; i >= 1; i-- { |
| 132 | + innerKey := &goyaml.Node{ |
| 133 | + Kind: goyaml.ScalarNode, |
| 134 | + Tag: "!!str", |
| 135 | + Value: parts[i], |
| 136 | + } |
| 137 | + innerMap := &goyaml.Node{ |
| 138 | + Kind: goyaml.MappingNode, |
| 139 | + Tag: "!!map", |
| 140 | + Content: []*goyaml.Node{innerKey, currentValue}, |
| 141 | + } |
| 142 | + currentValue = innerMap |
| 143 | + } |
| 144 | + |
| 145 | + outerKey := &goyaml.Node{ |
| 146 | + Kind: goyaml.ScalarNode, |
| 147 | + Tag: "!!str", |
| 148 | + Value: parts[0], |
| 149 | + } |
| 150 | + |
| 151 | + return [2]*goyaml.Node{outerKey, currentValue} |
| 152 | +} |
| 153 | + |
| 154 | +// mergeIntoContent adds a key-value pair to the content slice. |
| 155 | +// If the key already exists and both old and new values are MappingNodes, |
| 156 | +// the entries are merged (new entries win on conflict). |
| 157 | +// Otherwise, the old entry is replaced (last-wins semantics). |
| 158 | +func mergeIntoContent(content *[]*goyaml.Node, keyNode, valueNode *goyaml.Node) { |
| 159 | + // Look for an existing key with the same value. |
| 160 | + for i := 0; i < len(*content); i += 2 { |
| 161 | + existingKey := (*content)[i] |
| 162 | + if existingKey.Kind == goyaml.ScalarNode && existingKey.Value == keyNode.Value { |
| 163 | + existingValue := (*content)[i+1] |
| 164 | + |
| 165 | + // If both are mappings, merge the entries. |
| 166 | + if existingValue.Kind == goyaml.MappingNode && valueNode.Kind == goyaml.MappingNode { |
| 167 | + mergeMappingNodes(existingValue, valueNode) |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + // Otherwise, replace (last-wins). |
| 172 | + (*content)[i+1] = valueNode |
| 173 | + return |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Key not found: append. |
| 178 | + *content = append(*content, keyNode, valueNode) |
| 179 | +} |
| 180 | + |
| 181 | +// mergeMappingNodes merges entries from src into dst. |
| 182 | +// If a key exists in both and both values are MappingNodes, they are recursively merged. |
| 183 | +// Otherwise, src wins (last-wins). |
| 184 | +func mergeMappingNodes(dst, src *goyaml.Node) { |
| 185 | + for i := 0; i < len(src.Content); i += 2 { |
| 186 | + srcKey := src.Content[i] |
| 187 | + srcValue := src.Content[i+1] |
| 188 | + mergeIntoContent(&dst.Content, srcKey, srcValue) |
| 189 | + } |
| 190 | +} |
0 commit comments