-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.go
139 lines (117 loc) · 2.96 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package parser
import (
"strconv"
"github.com/aquasecurity/trivy-iac/pkg/scanners/cloudformation/cftypes"
"github.com/liamg/jfather"
"gopkg.in/yaml.v3"
)
func setPropertyValueFromJson(node jfather.Node, propertyData *PropertyInner) error {
switch node.Kind() {
case jfather.KindNumber:
propertyData.Type = cftypes.Float64
return node.Decode(&propertyData.Value)
case jfather.KindBoolean:
propertyData.Type = cftypes.Bool
return node.Decode(&propertyData.Value)
case jfather.KindString:
propertyData.Type = cftypes.String
return node.Decode(&propertyData.Value)
case jfather.KindObject:
var childData map[string]*Property
if err := node.Decode(&childData); err != nil {
return err
}
propertyData.Type = cftypes.Map
propertyData.Value = childData
return nil
case jfather.KindArray:
var childData []*Property
if err := node.Decode(&childData); err != nil {
return err
}
propertyData.Type = cftypes.List
propertyData.Value = childData
return nil
default:
propertyData.Type = cftypes.String
return node.Decode(&propertyData.Value)
}
}
func setPropertyValueFromYaml(node *yaml.Node, propertyData *PropertyInner) error {
if IsIntrinsicFunc(node) {
var newContent []*yaml.Node
newContent = append(newContent, &yaml.Node{
Tag: "!!str",
Value: getIntrinsicTag(node.Tag),
Kind: yaml.ScalarNode,
})
newContent = createNode(node, newContent)
node.Tag = "!!map"
node.Kind = yaml.MappingNode
node.Content = newContent
}
if node.Content == nil {
switch node.Tag {
case "!!int":
propertyData.Type = cftypes.Int
propertyData.Value, _ = strconv.Atoi(node.Value)
case "!!bool":
propertyData.Type = cftypes.Bool
propertyData.Value, _ = strconv.ParseBool(node.Value)
case "!!str", "!!string":
propertyData.Type = cftypes.String
propertyData.Value = node.Value
}
return nil
}
switch node.Tag {
case "!!map":
var childData map[string]*Property
if err := node.Decode(&childData); err != nil {
return err
}
propertyData.Type = cftypes.Map
propertyData.Value = childData
return nil
case "!!seq":
var childData []*Property
if err := node.Decode(&childData); err != nil {
return err
}
propertyData.Type = cftypes.List
propertyData.Value = childData
return nil
}
return nil
}
func createNode(node *yaml.Node, newContent []*yaml.Node) []*yaml.Node {
if node.Content == nil {
newContent = append(newContent, &yaml.Node{
Tag: "!!str",
Value: node.Value,
Kind: yaml.ScalarNode,
})
} else {
newNode := &yaml.Node{
Content: node.Content,
Kind: node.Kind,
}
switch node.Kind {
case yaml.SequenceNode:
newNode.Tag = "!!seq"
case yaml.MappingNode:
newNode.Tag = "!!map"
case yaml.ScalarNode:
default:
newNode.Tag = node.Tag
}
newContent = append(newContent, newNode)
}
return newContent
}
func calculateEndLine(node *yaml.Node) int {
if node.Content == nil {
return node.Line
}
return calculateEndLine(node.Content[len(node.Content)-1])
}