Skip to content

Commit 9f4c88c

Browse files
committed
fix(codegen): change how variants schema validation is rendered
1 parent ee10b98 commit 9f4c88c

File tree

1 file changed

+86
-54
lines changed
  • pkg/translate/terraform_provider

1 file changed

+86
-54
lines changed

pkg/translate/terraform_provider/funcs.go

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,7 @@ func RenderLocationStructs(resourceTyp properties.ResourceType, names *NameProvi
974974

975975
const locationSchemaGetterTmpl = `
976976
{{- define "renderLocationAttribute" }}
977+
{{- with .Attribute }}
977978
"{{ .Name.Underscore }}": {{ .SchemaType }}{
978979
Description: "{{ .Description }}",
979980
{{- if .Required }}
@@ -990,7 +991,7 @@ const locationSchemaGetterTmpl = `
990991
{{- if .Attributes }}
991992
Attributes: map[string]rsschema.Attribute{
992993
{{- range .Attributes }}
993-
{{- template "renderLocationAttribute" . }}
994+
{{- template "renderLocationAttribute" Map "Schema" $.Schema "Attribute" . }}
994995
{{- end }}
995996
},
996997
{{- end }}
@@ -1003,28 +1004,29 @@ const locationSchemaGetterTmpl = `
10031004
{{- range .Functions }}
10041005
{{ $package }}.{{ .Function }}(path.Expressions{
10051006
{{- range .Expressions }}
1006-
{{ . }},
1007+
{{ .RenderAsString false }},
10071008
{{- end }}
10081009
}...),
10091010
{{- end }}
10101011
},
10111012
{{- end }}
10121013
},
10131014
{{- end }}
1015+
{{- end }}
10141016
10151017
func {{ .StructName }}LocationSchema() rsschema.Attribute {
1016-
{{- with .Schema }}
1018+
{{- with .Schema }}
10171019
return rsschema.SingleNestedAttribute{
10181020
Description: "{{ .Description }}",
10191021
Required: true,
10201022
Attributes: map[string]rsschema.Attribute{
1021-
{{- range .Attributes }}
1022-
{{- template "renderLocationAttribute" . }}
1023-
{{- end }}
1023+
{{- range .Attributes }}
1024+
{{- template "renderLocationAttribute" Map "Schema" $.Schema "Attribute" . }}
1025+
{{- end }}
10241026
},
10251027
}
10261028
}
1027-
{{- end }}
1029+
{{- end }}
10281030
`
10291031

10301032
type defaultCtx struct {
@@ -1037,10 +1039,31 @@ type modifierCtx struct {
10371039
Modifiers []string
10381040
}
10391041

1042+
type validatorExpressionCtx struct {
1043+
Elements []string
1044+
}
1045+
1046+
func validatorExpressionCtxFromString(expr string) *validatorExpressionCtx {
1047+
return &validatorExpressionCtx{
1048+
Elements: strings.Split(expr, "."),
1049+
}
1050+
}
1051+
1052+
func (o validatorExpressionCtx) RenderAsString(attributeIsList bool) string {
1053+
var processed []string
1054+
for idx, elt := range o.Elements {
1055+
if attributeIsList && idx == len(o.Elements)-1 {
1056+
processed = append(processed, "AtParent()")
1057+
}
1058+
processed = append(processed, elt)
1059+
}
1060+
return strings.Join(processed, ".")
1061+
}
1062+
10401063
type validatorFunctionCtx struct {
10411064
Type string
10421065
Function string
1043-
Expressions []string
1066+
Expressions []validatorExpressionCtx
10441067
Values []string
10451068
}
10461069

@@ -1070,18 +1093,19 @@ type attributeCtx struct {
10701093
}
10711094

10721095
type schemaCtx struct {
1073-
IsResource bool
1074-
ObjectOrModel string
1075-
StructName string
1076-
ReturnType string
1077-
Package string
1078-
Description string
1079-
Required bool
1080-
Computed bool
1081-
Optional bool
1082-
Sensitive bool
1083-
Attributes []attributeCtx
1084-
Validators *validatorCtx
1096+
IsResource bool
1097+
IsListAttribute bool
1098+
ObjectOrModel string
1099+
StructName string
1100+
ReturnType string
1101+
Package string
1102+
Description string
1103+
Required bool
1104+
Computed bool
1105+
Optional bool
1106+
Sensitive bool
1107+
Attributes []attributeCtx
1108+
Validators *validatorCtx
10851109
}
10861110

10871111
func RenderLocationSchemaGetter(names *NameProvider, spec *properties.Normalization, manager *imports.Manager) (string, error) {
@@ -1153,9 +1177,9 @@ func RenderLocationSchemaGetter(names *NameProvider, spec *properties.Normalizat
11531177

11541178
var validators *validatorCtx
11551179
if len(locations) > 1 && idx == 0 {
1156-
var expressions []string
1180+
var expressions []validatorExpressionCtx
11571181
for _, location := range locations {
1158-
expressions = append(expressions, fmt.Sprintf(`path.MatchRelative().AtParent().AtName("%s")`, location))
1182+
expressions = append(expressions, *validatorExpressionCtxFromString(fmt.Sprintf(`path.MatchRelative().AtParent().AtName("%s")`, location)))
11591183
}
11601184

11611185
functions := []validatorFunctionCtx{{
@@ -1339,7 +1363,7 @@ func generateValidatorFnsMapForVariants(variants []*properties.SpecParam) map[in
13391363
}
13401364

13411365
pathExpr := fmt.Sprintf(`path.MatchRelative().AtParent().AtName("%s")`, elt.TerraformNameVariant().Underscore)
1342-
validator.Expressions = append(validator.Expressions, pathExpr)
1366+
validator.Expressions = append(validator.Expressions, *validatorExpressionCtxFromString(pathExpr))
13431367

13441368
validatorFns[elt.VariantGroupId] = validator
13451369
}
@@ -1463,18 +1487,19 @@ func createSchemaSpecForParameter(schemaTyp properties.SchemaType, manager *impo
14631487
}
14641488

14651489
schemas = append(schemas, schemaCtx{
1466-
IsResource: isResource,
1467-
ObjectOrModel: "Object",
1468-
Package: packageName,
1469-
StructName: structName,
1470-
ReturnType: returnType,
1471-
Description: "",
1472-
Required: required,
1473-
Optional: !param.FinalRequired(),
1474-
Computed: computed,
1475-
Sensitive: param.FinalSensitive(),
1476-
Attributes: attributes,
1477-
Validators: validators,
1490+
IsResource: isResource,
1491+
IsListAttribute: param.Type == "list" && param.Items.Type == "entry",
1492+
ObjectOrModel: "Object",
1493+
Package: packageName,
1494+
StructName: structName,
1495+
ReturnType: returnType,
1496+
Description: "",
1497+
Required: required,
1498+
Optional: !param.FinalRequired(),
1499+
Computed: computed,
1500+
Sensitive: param.FinalSensitive(),
1501+
Attributes: attributes,
1502+
Validators: validators,
14781503
})
14791504

14801505
for _, elt := range param.Spec.SortedParams() {
@@ -1533,6 +1558,7 @@ func createSchemaSpecForParameter(schemaTyp properties.SchemaType, manager *impo
15331558
Functions: []validatorFunctionCtx{*validatorFn},
15341559
}
15351560
}
1561+
15361562
schemas = append(schemas, createSchemaSpecForParameter(schemaTyp, manager, structName, packageName, elt, validators)...)
15371563
}
15381564
}
@@ -1967,6 +1993,7 @@ func createSchemaSpecForNormalization(resourceTyp properties.ResourceType, schem
19671993

19681994
const renderSchemaTemplate = `
19691995
{{- define "renderSchemaListAttribute" }}
1996+
{{- with .Attribute }}
19701997
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
19711998
Description: "{{ .Description }}",
19721999
Required: {{ .Required }},
@@ -1981,7 +2008,7 @@ const renderSchemaTemplate = `
19812008
{{- if eq .Type "Expressions" }}
19822009
{{ $package }}.{{ .Function }}(path.Expressions{
19832010
{{- range .Expressions }}
1984-
{{ . }},
2011+
{{ .RenderAsString $.Schema.IsListAttribute }},
19852012
{{- end }}
19862013
}...),
19872014
@@ -1997,8 +2024,10 @@ const renderSchemaTemplate = `
19972024
{{- end }}
19982025
},
19992026
{{- end }}
2027+
{{- end }}
20002028
20012029
{{- define "renderSchemaMapAttribute" }}
2030+
{{- with .Attribute }}
20022031
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
20032032
Description: "{{ .Description }}",
20042033
Required: {{ .Required }},
@@ -2008,18 +2037,19 @@ const renderSchemaTemplate = `
20082037
ElementType: {{ .ElementType }},
20092038
},
20102039
{{- end }}
2040+
{{- end }}
20112041
20122042
{{- define "renderSchemaListNestedAttribute" }}
2013-
{{- with .Attribute }}
2043+
{{- with .Attribute }}
20142044
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
20152045
Description: "{{ .Description }}",
20162046
Required: {{ .Required }},
20172047
Optional: {{ .Optional }},
20182048
Computed: {{ .Computed }},
20192049
Sensitive: {{ .Sensitive }},
2020-
NestedObject: {{ $.StructName }}{{ .Name.CamelCase }}Schema(),
2050+
NestedObject: {{ $.Schema.StructName }}{{ .Name.CamelCase }}Schema(),
20212051
},
2022-
{{- end }}
2052+
{{- end }}
20232053
{{- end }}
20242054
20252055
{{- define "renderSchemaMapNestedAttribute" }}
@@ -2028,18 +2058,19 @@ const renderSchemaTemplate = `
20282058
20292059
20302060
{{- define "renderSchemaSingleNestedAttribute" }}
2031-
{{- with .Attribute }}
2032-
"{{ .Name.Underscore }}": {{ $.StructName }}{{ .Name.CamelCase }}Schema(),
2033-
{{- end }}
2061+
{{- with .Attribute }}
2062+
"{{ .Name.Underscore }}": {{ $.Schema.StructName }}{{ .Name.CamelCase }}Schema(),
2063+
{{- end }}
20342064
{{- end }}
20352065
20362066
{{- define "renderSchemaExternalAttribute" }}
2037-
{{- with .Attribute }}
2067+
{{- with .Attribute }}
20382068
"{{ .Name.Underscore }}": {{ .ExternalType }}Schema(),
2039-
{{- end }}
2069+
{{- end }}
20402070
{{- end }}
20412071
20422072
{{- define "renderSchemaSimpleAttribute" }}
2073+
{{- with .Attribute }}
20432074
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
20442075
Description: "{{ .Description }}",
20452076
Computed: {{ .Computed }},
@@ -2064,7 +2095,7 @@ const renderSchemaTemplate = `
20642095
{{- if eq .Type "Expressions" }}
20652096
{{ $package }}.{{ .Function }}(path.Expressions{
20662097
{{- range .Expressions }}
2067-
{{ . }},
2098+
{{ .RenderAsString $.Schema.IsListAttribute }},
20682099
{{- end }}
20692100
}...),
20702101
{{- else if eq .Type "Values" }}
@@ -2079,23 +2110,24 @@ const renderSchemaTemplate = `
20792110
{{- end }}
20802111
},
20812112
{{- end }}
2113+
{{- end }}
20822114
20832115
{{- define "renderSchemaAttribute" }}
20842116
{{- with .Attribute }}
20852117
{{ if or (eq .SchemaType "ListAttribute") (eq .SchemaType "SetAttribute") }}
2086-
{{- template "renderSchemaListAttribute" . }}
2118+
{{- template "renderSchemaListAttribute" Map "Schema" $.Schema "Attribute" . }}
20872119
{{- else if eq .SchemaType "MapAttribute" }}
2088-
{{- template "renderSchemaMapAttribute" . }}
2120+
{{- template "renderSchemaMapAttribute" Map "Schema" $.Schema "Attribute" . }}
20892121
{{- else if eq .SchemaType "ListNestedAttribute" }}
2090-
{{- template "renderSchemaListNestedAttribute" Map "StructName" $.StructName "Attribute" . }}
2122+
{{- template "renderSchemaListNestedAttribute" Map "Schema" $.Schema "Attribute" . }}
20912123
{{ else if eq .SchemaType "MapNestedAttribute" }}
2092-
{{- template "renderSchemaMapNestedAttribute" Map "StructName" $.StructName "Attribute" . }}
2124+
{{- template "renderSchemaMapNestedAttribute" Map "Schema" $.Schema "Attribute" . }}
20932125
{{- else if eq .SchemaType "SingleNestedAttribute" }}
2094-
{{- template "renderSchemaSingleNestedAttribute" Map "StructName" $.StructName "Attribute" . }}
2126+
{{- template "renderSchemaSingleNestedAttribute" Map "Schema" $.Schema "Attribute" . }}
20952127
{{- else if eq .SchemaType "ExternalAttribute" }}
2096-
{{- template "renderSchemaExternalAttribute" Map "Attribute" . }}
2128+
{{- template "renderSchemaExternalAttribute" Map "Schema" $.Schema "Attribute" . }}
20972129
{{- else }}
2098-
{{- template "renderSchemaSimpleAttribute" . }}
2130+
{{- template "renderSchemaSimpleAttribute" Map "Schema" $.Schema "Attribute" . }}
20992131
{{- end }}
21002132
{{- end }}
21012133
{{- end }}
@@ -2118,15 +2150,15 @@ func {{ .StructName }}Schema() {{ .Package }}.{{ .ReturnType }} {
21182150
{{- range .Functions }}
21192151
{{ $package }}.{{ .Function }}(path.Expressions{
21202152
{{- range .Expressions }}
2121-
{{ . }},
2153+
{{ .RenderAsString $schema.IsListAttribute }},
21222154
{{- end }}
21232155
}...),
21242156
{{- end }}
21252157
},
21262158
{{- end }}
21272159
Attributes: map[string]{{ .Package }}.Attribute{
21282160
{{- range .Attributes -}}
2129-
{{- template "renderSchemaAttribute" Map "StructName" $schema.StructName "Attribute" . }}
2161+
{{- template "renderSchemaAttribute" Map "Schema" $schema "Attribute" . }}
21302162
{{- end }}
21312163
},
21322164
}

0 commit comments

Comments
 (0)