Fix up yaml.Nodes on the way out of a template
(n *yaml.Node)
| 27 | |
| 28 | // Fix up yaml.Nodes on the way out of a template |
| 29 | func formatNode(n *yaml.Node) *yaml.Node { |
| 30 | n = node.Clone(n) |
| 31 | |
| 32 | // Is it a map? |
| 33 | if n.Kind == yaml.MappingNode { |
| 34 | // Does it have just one key/value pair? |
| 35 | if len(n.Content) == 2 { |
| 36 | |
| 37 | // Is the key relevant? |
| 38 | for tag, funcName := range cft.Tags { |
| 39 | if n.Content[0].Value == funcName { |
| 40 | // Prepare comments |
| 41 | headComments := []string{n.HeadComment, n.Content[0].HeadComment, n.Content[1].HeadComment} |
| 42 | lineComments := []string{n.LineComment, n.Content[0].LineComment, n.Content[1].LineComment} |
| 43 | footComments := []string{n.FootComment, n.Content[0].FootComment, n.Content[1].FootComment} |
| 44 | |
| 45 | n = n.Content[1] |
| 46 | n.Tag = tag |
| 47 | |
| 48 | // Is it a GetAtt and is currently a sequence? |
| 49 | if funcName == "Fn::GetAtt" && n.Kind == yaml.SequenceNode { |
| 50 | // Are both parts scalars? |
| 51 | allScalar := true |
| 52 | parts := make([]string, len(n.Content)) |
| 53 | for i, child := range n.Content { |
| 54 | if child.Kind != yaml.ScalarNode { |
| 55 | allScalar = false |
| 56 | break |
| 57 | } |
| 58 | |
| 59 | parts[i] = child.Value |
| 60 | |
| 61 | headComments = append(headComments, child.HeadComment) |
| 62 | lineComments = append(lineComments, child.LineComment) |
| 63 | footComments = append(footComments, child.FootComment) |
| 64 | } |
| 65 | |
| 66 | if allScalar { |
| 67 | n.Content = []*yaml.Node{} |
| 68 | n.Kind = yaml.ScalarNode |
| 69 | n.Value = strings.Join(parts, ".") |
| 70 | } |
| 71 | |
| 72 | n.HeadComment = mergeComments(headComments) |
| 73 | n.LineComment = mergeComments(lineComments) |
| 74 | n.FootComment = mergeComments(footComments) |
| 75 | } |
| 76 | |
| 77 | break |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Is it a scalar? |
| 84 | if n.Kind == yaml.ScalarNode { |
| 85 | // Is it a string |
| 86 | if n.Tag == "!!str" { |
no test coverage detected