Returns the parent node of node, paired with its name if it's a map pair. If it is not a map pair, only NodePair.Value is not nil. (YAML maps are arrays with even indexes being names and odd indexes being values) node Content 0: Name 1: Map Content 0: a 1: b In the above
(node *yaml.Node, rootNode *yaml.Node, priorNode *yaml.Node)
| 72 | // |
| 73 | // For sequence elements that are maps, the Key will be nil |
| 74 | func GetParent(node *yaml.Node, rootNode *yaml.Node, priorNode *yaml.Node) NodePair { |
| 75 | if node == rootNode { |
| 76 | config.Debugf("getParent node and rootNode are the same") |
| 77 | return NodePair{Key: node, Value: node} |
| 78 | } |
| 79 | |
| 80 | if node == nil { |
| 81 | config.Debugf("node is nil") |
| 82 | return NodePair{nil, nil, nil} |
| 83 | } |
| 84 | |
| 85 | var found *yaml.Node |
| 86 | var before *yaml.Node |
| 87 | var pair NodePair |
| 88 | |
| 89 | if rootNode.Kind == yaml.DocumentNode || rootNode.Kind == yaml.SequenceNode { |
| 90 | for _, n := range rootNode.Content { |
| 91 | if n == node { |
| 92 | found = rootNode |
| 93 | before = priorNode |
| 94 | break |
| 95 | } |
| 96 | pair = GetParent(node, n, nil) |
| 97 | if pair.Value != nil { |
| 98 | found = pair.Value |
| 99 | before = pair.Key |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | } else if rootNode.Kind == yaml.MappingNode { |
| 104 | for i := 0; i < len(rootNode.Content); i += 2 { |
| 105 | n := rootNode.Content[i+1] |
| 106 | if n == node { |
| 107 | found = rootNode |
| 108 | before = priorNode |
| 109 | break |
| 110 | } |
| 111 | pair = GetParent(node, n, rootNode.Content[i]) |
| 112 | if pair.Value != nil { |
| 113 | found = pair.Value |
| 114 | before = pair.Key |
| 115 | break |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | return NodePair{Key: before, Value: found} |
| 120 | } |
| 121 | |
| 122 | type SNode struct { |
| 123 | Kind string |