Diff returns an array of strings describing differences between two nodes
(node1, node2 *yaml.Node)
| 301 | |
| 302 | // Diff returns an array of strings describing differences between two nodes |
| 303 | func Diff(node1, node2 *yaml.Node) (diffs []string) { |
| 304 | switch { |
| 305 | case node1 == nil && node2 == nil: |
| 306 | return nil |
| 307 | case node1 == nil: |
| 308 | diffs = append(diffs, fmt.Sprintf("Node2: %v", node2.Value)) |
| 309 | case node2 == nil: |
| 310 | diffs = append(diffs, fmt.Sprintf("Node1: %v", node1.Value)) |
| 311 | default: |
| 312 | if node1.Kind != node2.Kind { |
| 313 | diffs = append(diffs, fmt.Sprintf("Node1: %v, Node2: %v", node1.Value, node2.Value)) |
| 314 | } else { |
| 315 | switch node1.Kind { |
| 316 | case yaml.MappingNode: |
| 317 | diffs = appendMappingDiffs(diffs, node1, node2) |
| 318 | case yaml.SequenceNode: |
| 319 | diffs = appendSequenceDiffs(diffs, node1, node2) |
| 320 | case yaml.ScalarNode: |
| 321 | if node1.Value != node2.Value { |
| 322 | diffs = append(diffs, |
| 323 | fmt.Sprintf("Node1: %v, Node2: %v", node1.Value, node2.Value)) |
| 324 | } |
| 325 | default: |
| 326 | diffs = append(diffs, fmt.Sprintf("Unsupported node kind: %v", node1.Kind)) |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return diffs |
| 331 | } |
| 332 | |
| 333 | func appendMappingDiffs(diffs []string, node1, node2 *yaml.Node) []string { |
| 334 | keys1 := make(map[string]yaml.Node) |