-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema2code.go
118 lines (97 loc) · 3.03 KB
/
schema2code.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
package schema
import (
"bytes"
"encoding/json"
"fmt"
"github.com/iancoleman/strcase"
"go/format"
"strings"
"text/template"
)
type Package string
const PackageResource = "resource"
const PackageData = "data"
const funcTemplate = `package {{ .Package }}
import (
"encoding/json"
tfjson "github.com/hashicorp/terraform-json"
)
const {{.ResourceName}} = ` + "`{{.ResourceSchema}}`" + `
func {{.ResourceNameCamel}}Schema() *tfjson.Schema {
var result tfjson.Schema
_ = json.Unmarshal([]byte({{.ResourceName}}), &result)
return &result
}
`
const testTemplate = `package {{ .Package }}_test
import (
"testing"
tfjson "github.com/hashicorp/terraform-json"
"github.com/{{ .RepoOwner }}/{{ .GoModule }}/generated/{{ .Package }}"
"github.com/stretchr/testify/assert"
)
func Test{{.ResourceNameCamel}}Schema(t *testing.T) {
defaultSchema := &tfjson.Schema{}
s := {{ .Package }}.{{.ResourceNameCamel}}Schema()
assert.NotNil(t, s)
assert.NotEqual(t, defaultSchema, s)
}
`
type TemplateData struct {
Package string
ResourceName string
ResourceSchema string
ResourceNameCamel string
RepoOwner string
GoModule string
}
func GenerateGoFileContent(blockName, schema string, pkg Package) (string, error) {
return generateGoCode(blockName, funcTemplate, &schema, pkg)
}
func GenerateGoTestFileContent(blockName string, pkg Package) (string, error) {
return generateGoCode(blockName, testTemplate, nil, pkg)
}
func generateGoCode(blockName, fileTemplate string, schema *string, pkg Package) (string, error) {
parameter, err := buildTemplate(blockName, schema, pkg)
if err != nil {
return "", err
}
tmpl, err := template.New("goFileContent").Parse(fileTemplate)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, parameter); err != nil {
return "", fmt.Errorf("error executing template: %w", err)
}
formattedSource, err := format.Source(buf.Bytes())
if err != nil {
return "", fmt.Errorf("error formatting generated Go file content: %w", err)
}
return string(formattedSource), nil
}
func buildTemplate(blockName string, schema *string, pkg Package) (*TemplateData, error) {
var escapedSchema string
if schema != nil {
var schemaMap map[string]interface{}
if err := json.Unmarshal([]byte(*schema), &schemaMap); err != nil {
return nil, fmt.Errorf("error unmarshalling schema JSON: %s", err)
}
// Marshal the schema map back to a well-formatted JSON string
formattedSchemaBytes, err := json.MarshalIndent(schemaMap, "", " ")
if err != nil {
return nil, fmt.Errorf("error marshalling schema map: %w", err)
}
formattedSchema := string(formattedSchemaBytes)
escapedSchema = strings.ReplaceAll(formattedSchema, "`", "` + \"`\" + `")
}
data := TemplateData{
Package: string(pkg),
ResourceName: strcase.ToLowerCamel(blockName),
ResourceSchema: escapedSchema,
ResourceNameCamel: strcase.ToCamel(blockName),
RepoOwner: "lonegunmanb",
GoModule: "terraform-awscc-schema",
}
return &data, nil
}