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
/
generate.go
124 lines (110 loc) · 3.87 KB
/
generate.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
package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"log"
"strings"
"text/template"
)
var globalPropertyTypes []string
func buildYamlParsers(cfnSpec CfnSpec) {
// check for global types
globalPropertyTypes := []string{}
for k := range cfnSpec.PropertyTypes {
if isPropertyGlobal(k) {
globalPropertyTypes = append(globalPropertyTypes, k)
}
}
var resourceTypes []string
log.Println("Generate Resource Map")
resourceParsersObject := buildParserMapping(cfnSpec, "resources")
filePath := fmt.Sprintf("%vresources.go", parsersDir)
formatted, err := format.Source([]byte(resourceParsersObject))
checkError(err)
err = ioutil.WriteFile(filePath, formatted, 0644)
checkError(err)
log.Println("Generate Properties")
// properties
for k, cfnType := range cfnSpec.PropertyTypes {
propertyObject := buildPropertyYaml(k, cfnType)
filePath := fmt.Sprintf("%v%v.go", propertiesDir, propertyNameFromPropertyType(k))
formatted, err := format.Source([]byte(propertyObject))
checkError(err)
err = ioutil.WriteFile(filePath, formatted, 0644)
checkError(err)
}
log.Println("Generate Resources")
// Resource parsers
for k, cfnType := range cfnSpec.ResourceTypes {
resourceObject := buildResourceYaml(k, cfnType)
filePath := fmt.Sprintf("%v%v.go", resourcesDir, fileNameFromCfnType(k))
formatted, err := format.Source([]byte(resourceObject))
checkError(err)
err = ioutil.WriteFile(filePath, formatted, 0644)
checkError(err)
resourceTypes = append(resourceTypes, titleCaseNameFromCfnType(k))
}
}
func buildParserMapping(cfnSpec CfnSpec, packageName string) string {
resourceTypes := make(map[string]string)
for _, k := range sortTypeNames(cfnSpec.ResourceTypes) {
name := titleCaseNameFromCfnType(k)
resourceTypes[k] = name
}
buf := bytes.NewBufferString("")
t := template.Must(template.New("").Parse(parserMapTemplate))
err := t.Execute(buf, map[string]interface{}{
"PackageNameTitle": strings.Title(packageName),
"PackageName": packageName,
"ResourceTypes": resourceTypes,
"MainPackageName": mainPackageName,
})
checkError(err)
return buf.String()
}
func buildPropertyYaml(obj string, cfnType CfnType) string {
propertyStrings := make([]string, len(cfnType.Properties))
validatorStrings := make([]string, len(cfnType.Properties))
for _, property := range sortProperties(cfnType.Properties) {
if str := valueStringYaml("", obj, property.name, property.CfnProperty); len(str) > 0 {
propertyStrings = append(propertyStrings, str)
}
}
buf := bytes.NewBufferString("")
t := template.Must(template.New("").Parse(propertyTemplate))
err := t.Execute(buf, map[string]interface{}{
"PropertyName": propertyNameFromPropertyType(obj),
"Documentation": cfnType.Documentation,
"PropertyStrings": propertyStrings,
"ValidatorStrings": validatorStrings,
"NeedsFmtImport": needsFmtImport(cfnType),
"MainPackageName": mainPackageName,
})
checkError(err)
return buf.String()
}
func buildResourceYaml(obj string, cfnType CfnType) string {
propertyStrings := make([]string, 0, len(cfnType.Properties))
validatorStrings := make([]string, 0, len(cfnType.Properties))
for _, property := range sortProperties(cfnType.Properties) {
if str := valueStringYaml("properties.", obj, property.name, property.CfnProperty); len(str) > 0 {
propertyStrings = append(propertyStrings, str)
}
}
buf := bytes.NewBufferString("")
t := template.Must(template.New("").Parse(resourceTemplate))
err := t.Execute(buf, map[string]interface{}{
"Type": obj,
"ResourceName": titleCaseNameFromCfnType(obj),
"Documentation": cfnType.Documentation,
"PropertyStrings": propertyStrings,
"ValidatorStrings": validatorStrings,
"NeedsFmtImport": needsFmtImport(cfnType),
"NeedsPropertiesImport": needsPropertiesImport(cfnType),
"MainPackageName": mainPackageName,
})
checkError(err)
return buf.String()
}