Remove a map key-value pair from node.Content
(node *yaml.Node, name string)
| 197 | |
| 198 | // Remove a map key-value pair from node.Content |
| 199 | func RemoveFromMap(node *yaml.Node, name string) error { |
| 200 | |
| 201 | if len(node.Content) == 0 { |
| 202 | return nil |
| 203 | } |
| 204 | |
| 205 | idx := -1 |
| 206 | |
| 207 | for i, n := range node.Content { |
| 208 | if n.Value == name { |
| 209 | idx = i |
| 210 | break |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if idx == -1 { |
| 215 | return fmt.Errorf("unable to remove %v from map", name) |
| 216 | } |
| 217 | |
| 218 | newContent := make([]*yaml.Node, 0) |
| 219 | newContent = append(newContent, node.Content[:idx]...) |
| 220 | // Remove 2 elements, since the key and value are consecutive elements in the Content array |
| 221 | newContent = append(newContent, node.Content[idx+2:]...) |
| 222 | |
| 223 | node.Content = newContent |
| 224 | |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | // Add or replace a map value |
| 229 | func SetMapValue(parent *yaml.Node, name string, val *yaml.Node) { |
no outgoing calls