func parse()

in internal/pkg/template/diff/diff.go [140:187]


func parse(from, to *yaml.Node, key string, overriders ...overrider) (diffNode, error) {
	for _, overrider := range overriders {
		if overrider.match(from, to, key, overrider) {
			return overrider.parse(from, to, key, overrider)
		}
	}
	// Handle base cases.
	if to == nil || from == nil || to.Kind != from.Kind {
		return &keyNode{
			keyValue: key,
			newV:     to,
			oldV:     from,
		}, nil
	}
	if isYAMLLeaf(to) && isYAMLLeaf(from) {
		if to.Value == from.Value {
			return nil, nil
		}
		return &keyNode{
			keyValue: key,
			newV:     to,
			oldV:     from,
		}, nil
	}

	var children []diffNode
	var err error
	switch {
	case to.Kind == yaml.SequenceNode && from.Kind == yaml.SequenceNode:
		children, err = parseSequence(from, to, overriders...)
	case to.Kind == yaml.DocumentNode && from.Kind == yaml.DocumentNode:
		fallthrough
	case to.Kind == yaml.MappingNode && from.Kind == yaml.MappingNode:
		children, err = parseMap(from, to, overriders...)
	default:
		return nil, fmt.Errorf("unknown combination of node kinds: %v, %v", to.Kind, from.Kind)
	}
	if err != nil {
		return nil, fmt.Errorf("parse YAML content with key %s: %w", key, err)
	}
	if len(children) == 0 {
		return nil, nil
	}
	return &keyNode{
		keyValue:   key,
		childNodes: children,
	}, nil
}