-
Notifications
You must be signed in to change notification settings - Fork 2
/
property.go
executable file
·264 lines (212 loc) · 8.02 KB
/
property.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
)
// Property represents an AWS CloudFormation resource property
type Property struct {
// Documentation - A link to the AWS CloudFormation User Guide that provides information about the property.
Documentation string `json:"Documentation"`
// DuplicatesAllowed - If the value of the Type field is List, indicates whether AWS CloudFormation allows duplicate values.
// If the value is true, AWS CloudFormation ignores duplicate values. If the value is false,
// AWS CloudFormation returns an error if you submit duplicate values.
DuplicatesAllowed bool `json:"DuplicatesAllowed"`
// ItemType - If the value of the Type field is List or Map, indicates the type of list or map if they contain
// non-primitive types. Otherwise, this field is omitted. For lists or maps that contain primitive
// types, the PrimitiveItemType property indicates the valid value type.
//
// A subproperty name is a valid item type. For example, if the type value is List and the item type
// value is PortMapping, you can specify a list of port mapping properties.
ItemType string `json:"ItemType"`
// PrimitiveItemType - If the value of the Type field is List or Map, indicates the type of list or map
// if they contain primitive types. Otherwise, this field is omitted. For lists or maps that contain
// non-primitive types, the ItemType property indicates the valid value type.
// The valid primitive types for lists and maps are String, Long, Integer, Double, Boolean, or Timestamp.
// For example, if the type value is List and the item type value is String, you can specify a list of strings
// for the property. If the type value is Map and the item type value is Boolean, you can specify a string
// to Boolean mapping for the property.
PrimitiveItemType string `json:"PrimitiveItemType"`
// PrimitiveType - For primitive values, the valid primitive type for the property. A primitive type is a
// basic data type for resource property values.
// The valid primitive types are String, Long, Integer, Double, Boolean, Timestamp or Json.
// If valid values are a non-primitive type, this field is omitted and the Type field indicates the valid value type.
PrimitiveType string `json:"PrimitiveType"`
// Required indicates whether the property is required.
Required bool `json:"Required"`
// Type - For non-primitive types, valid values for the property. The valid types are a subproperty name,
// List or Map. If valid values are a primitive type, this field is omitted and the PrimitiveType field
// indicates the valid value type. A list is a comma-separated list of values. A map is a set of key-value pairs,
// where the keys are always strings. The value type for lists and maps are indicated by the ItemType
// or PrimitiveItemType field.
Type string `json:"Type"`
// UpdateType - During a stack update, the update behavior when you add, remove, or modify the property.
// AWS CloudFormation replaces the resource when you change Immutable properties. AWS CloudFormation doesn't
// replace the resource when you change mutable properties. Conditional updates can be mutable or immutable,
// depending on, for example, which other properties you updated. For more information, see the relevant
// resource type documentation.
UpdateType string `json:"UpdateType"`
Value struct {
ValueType string `json:"ValueType"`
ListValueType string `json:"ListValueType"`
} `json:"Value"`
// Types - if a property can be different types, they will be listed here
PrimitiveTypes []string `json:"PrimitiveTypes"`
PrimitiveItemTypes []string `json:"PrimitiveItemTypes"`
ItemTypes []string `json:"ItemTypes"`
Types []string `json:"Types"`
}
// Schema returns a JSON Schema for the resource (as a string)
func (p Property) Schema(name, parent string) string {
// Open the schema template and setup a counter function that will
// available in the template to be used to detect when trailing commas
// are required in the JSON when looping through maps
tmpl, err := template.New("schema-property.template").Funcs(template.FuncMap{
"counter": counter,
"convertToJSONType": convertTypeToJSON,
}).ParseFiles("generate/templates/schema-property.template")
var buf bytes.Buffer
parentpaths := strings.Split(parent, ".")
templateData := struct {
Name string
Parent string
Property Property
}{
Name: name,
Parent: parentpaths[0],
Property: p,
}
// Execute the template, writing it to the buffer
err = tmpl.Execute(&buf, templateData)
if err != nil {
fmt.Printf("Error: Failed to generate property %s\n%s\n", name, err)
os.Exit(1)
}
return buf.String()
}
// IsPolymorphic checks whether a property can be multiple different types
func (p Property) IsPolymorphic() bool {
return len(p.PrimitiveTypes) > 0 || len(p.PrimitiveItemTypes) > 0 || len(p.PrimitiveItemTypes) > 0 || len(p.ItemTypes) > 0 || len(p.Types) > 0
}
// IsPrimitive checks whether a property is a primitive type
func (p Property) IsPrimitive() bool {
return p.PrimitiveType != ""
}
func (p Property) IsNumeric() bool {
return p.IsPrimitive() &&
(p.PrimitiveType == "Long" ||
p.PrimitiveType == "Integer" ||
p.PrimitiveType == "Double" ||
p.PrimitiveType == "Boolean")
}
// IsMap checks whether a property should be a map (map[string]...)
func (p Property) IsMap() bool {
return p.Type == "Map"
}
// IsMapOfPrimitives checks whether a map contains primitive values
func (p Property) IsMapOfPrimitives() bool {
return p.IsMap() && p.PrimitiveItemType != ""
}
// IsList checks whether a property should be a list ([]...)
func (p Property) IsList() bool {
return p.Type == "List"
}
// IsListOfPrimitives checks whether a list containers primitive values
func (p Property) IsListOfPrimitives() bool {
return p.IsList() && p.PrimitiveItemType != ""
}
// IsCustomType checks wither a property is a custom type
func (p Property) IsCustomType() bool {
return p.PrimitiveType == "" && p.ItemType == "" && p.PrimitiveItemType == ""
}
// GoType returns the correct type for this property
// within a Go struct. For example, []string or map[string]AWSLambdaFunction_VpcConfig
func (p Property) GoType(basename string, name string) string {
if p.IsPolymorphic() {
generatePolymorphicProperty(basename+"_"+name, p)
return basename + "_" + name
}
if p.IsMap() {
if p.IsMapOfPrimitives() {
return "map[string]" + convertTypeToGo(p.PrimitiveItemType)
}
if p.ItemType == "Tag" {
return "map[string]Tag"
}
return "map[string]" + basename + "_" + p.ItemType
}
if p.IsList() {
if p.IsListOfPrimitives() {
return "[]" + convertTypeToGo(p.PrimitiveItemType)
}
if p.ItemType == "Tag" {
return "[]Tag"
}
return "[]" + basename + "_" + p.ItemType
}
if p.IsCustomType() {
return basename + "_" + p.Type
}
// Must be a primitive value
return convertTypeToGo(p.PrimitiveType)
}
// GetJSONPrimitiveType returns the correct primitive property type for a JSON Schema.
// If the property is a list/map, then it will return the type of the items.
func (p Property) GetJSONPrimitiveType() string {
if p.IsPrimitive() {
return convertTypeToJSON(p.PrimitiveType)
}
if p.IsMap() && p.IsMapOfPrimitives() {
return convertTypeToJSON(p.PrimitiveItemType)
}
if p.IsList() && p.IsListOfPrimitives() {
return convertTypeToJSON(p.PrimitiveItemType)
}
return "unknown"
}
func convertTypeToGo(pt string) string {
switch pt {
case "String":
return "string"
case "Long":
return "int64"
case "Integer":
return "int"
case "Double":
return "float64"
case "Boolean":
return "bool"
case "Timestamp":
return "string"
case "Json":
return "interface{}"
case "Map":
return "interface{}"
default:
return pt
}
}
func convertTypeToJSON(name string) string {
switch name {
case "String":
return "string"
case "Long":
return "number"
case "Integer":
return "number"
case "Double":
return "number"
case "Boolean":
return "boolean"
case "Timestamp":
return "string"
case "Json":
return "object"
case "Map":
return "object"
default:
return name
}
}