This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.go
176 lines (154 loc) · 4.41 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"io/ioutil"
"log"
"sort"
"strings"
)
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func valueStringYaml(propPackage, obj, name string, property CfnProperty) string {
omitempty := ",omitempty"
if property.Required {
omitempty = ""
}
// if len(property.PrimitiveType) > 0 {
// return name + " interface{} `yaml:" + `"` + name + omitempty + `"` + "`"
// } else if len(property.Type) > 0 && property.Type != "List" && property.Type != "Map" {
// subPropertyName := propertyNameFromResourceType(obj, property.Type)
// return name + " *" + propPackage + subPropertyName + " `yaml:" + `"` + name + omitempty + `"` + "`"
// }
return name + " interface{} `yaml:" + `"` + name + omitempty + `"` + "`"
}
// func validatorYaml(obj, name string, property CfnProperty) string {
// if !property.Required {
// return ""
// }
// buf := bytes.NewBufferString("")
// t := template.Must(template.New("").Parse(validatorTemplate))
// err := t.Execute(buf, map[string]interface{}{
// "Name": name,
// "PrimitiveType": len(property.PrimitiveType) > 0,
// "ListMapType": len(property.Type) > 0 && property.Type != "List" && property.Type != "Map",
// })
// checkError(err)
// return buf.String()
// }
func sortSpecList(specList map[string]string) []string {
regions := make([]string, len(specList))
i := 0
for region := range specList {
regions[i] = region
i++
}
sort.Strings(regions)
return regions
}
func sortProperties(properties map[string]CfnProperty) []NamedCfnProperty {
primitives := []NamedCfnProperty{}
nonPrimitives := []NamedCfnProperty{}
for name, property := range properties {
namedProperty := NamedCfnProperty{CfnProperty: property, name: name}
if len(property.PrimitiveType) > 0 {
primitives = append(primitives, namedProperty)
} else {
nonPrimitives = append(nonPrimitives, namedProperty)
}
}
sort.Sort(ByName(primitives))
sort.Sort(ByName(nonPrimitives))
sort.Sort(ByType(nonPrimitives))
return append(primitives, nonPrimitives...)
}
func needsFmtImport(cfnType CfnType) bool {
for _, property := range cfnType.Properties {
if len(property.PrimitiveType) > 0 {
if property.Required {
return true
}
} else if len(property.Type) > 0 {
if property.Required {
return true
}
}
}
return false
}
func needsPropertiesImport(cfnType CfnType) bool {
for _, property := range cfnType.Properties {
if len(property.Type) > 0 && property.Type != "List" && property.Type != "Map" {
return true
}
}
return false
}
func sortAttributeNames(attributes map[string]CfnAttribute) []string {
names := make([]string, len(attributes))
i := 0
for name := range attributes {
names[i] = name
i++
}
sort.Strings(names)
return names
}
func sortTypeNames(types map[string]CfnType) []string {
names := make([]string, len(types))
i := 0
for name := range types {
names[i] = name
i++
}
sort.Strings(names)
return names
}
func isPropertyGlobal(typeName string) bool {
return !strings.Contains(typeName, "::")
}
func propertyNameFromPropertyType(typeName string) string {
if isPropertyGlobal(typeName) {
return typeName
}
parts := strings.Split(typeName, "::")
subParts := strings.Split(parts[len(parts)-1], ".")
return strings.Join(subParts, "")
}
func propertyNameFromResourceType(typeName, propertyName string) string {
for _, v := range globalPropertyTypes {
if v == propertyName {
return propertyName
}
}
parts := strings.Split(typeName, "::")
subParts := strings.Split(parts[len(parts)-1], ".")
return subParts[0] + propertyName
}
func fileNameFromCfnType(typeName string) string {
parts := strings.Split(typeName, "::")
return fmt.Sprint(strings.Title(parts[1]), "-", strings.Title(parts[2]))
}
func titleCaseNameFromCfnType(typeName string) string {
parts := strings.Split(typeName, "::")
return fmt.Sprint(strings.Title(parts[1]), strings.Title(parts[2]))
}
func generateTypes(types string) {
err := ioutil.WriteFile("parsers/types.go", []byte(types), 0644)
checkError(err)
}
// Sort types
type ByType []NamedCfnProperty
func (a ByType) Len() int { return len(a) }
func (a ByType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByType) Less(i, j int) bool {
return a[i].Type > a[j].Type
}
type ByName []NamedCfnProperty
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool {
return a[i].name < a[j].name
}