@@ -974,6 +974,7 @@ func RenderLocationStructs(resourceTyp properties.ResourceType, names *NameProvi
974
974
975
975
const locationSchemaGetterTmpl = `
976
976
{{- define "renderLocationAttribute" }}
977
+ {{- with .Attribute }}
977
978
"{{ .Name.Underscore }}": {{ .SchemaType }}{
978
979
Description: "{{ .Description }}",
979
980
{{- if .Required }}
@@ -990,7 +991,7 @@ const locationSchemaGetterTmpl = `
990
991
{{- if .Attributes }}
991
992
Attributes: map[string]rsschema.Attribute{
992
993
{{- range .Attributes }}
993
- {{- template "renderLocationAttribute" . }}
994
+ {{- template "renderLocationAttribute" Map "Schema" $.Schema "Attribute" . }}
994
995
{{- end }}
995
996
},
996
997
{{- end }}
@@ -1003,28 +1004,29 @@ const locationSchemaGetterTmpl = `
1003
1004
{{- range .Functions }}
1004
1005
{{ $package }}.{{ .Function }}(path.Expressions{
1005
1006
{{- range .Expressions }}
1006
- {{ . }},
1007
+ {{ .RenderAsString false }},
1007
1008
{{- end }}
1008
1009
}...),
1009
1010
{{- end }}
1010
1011
},
1011
1012
{{- end }}
1012
1013
},
1013
1014
{{- end }}
1015
+ {{- end }}
1014
1016
1015
1017
func {{ .StructName }}LocationSchema() rsschema.Attribute {
1016
- {{- with .Schema }}
1018
+ {{- with .Schema }}
1017
1019
return rsschema.SingleNestedAttribute{
1018
1020
Description: "{{ .Description }}",
1019
1021
Required: true,
1020
1022
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 }}
1024
1026
},
1025
1027
}
1026
1028
}
1027
- {{- end }}
1029
+ {{- end }}
1028
1030
`
1029
1031
1030
1032
type defaultCtx struct {
@@ -1037,10 +1039,31 @@ type modifierCtx struct {
1037
1039
Modifiers []string
1038
1040
}
1039
1041
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
+
1040
1063
type validatorFunctionCtx struct {
1041
1064
Type string
1042
1065
Function string
1043
- Expressions []string
1066
+ Expressions []validatorExpressionCtx
1044
1067
Values []string
1045
1068
}
1046
1069
@@ -1070,18 +1093,19 @@ type attributeCtx struct {
1070
1093
}
1071
1094
1072
1095
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
1085
1109
}
1086
1110
1087
1111
func RenderLocationSchemaGetter (names * NameProvider , spec * properties.Normalization , manager * imports.Manager ) (string , error ) {
@@ -1153,9 +1177,9 @@ func RenderLocationSchemaGetter(names *NameProvider, spec *properties.Normalizat
1153
1177
1154
1178
var validators * validatorCtx
1155
1179
if len (locations ) > 1 && idx == 0 {
1156
- var expressions []string
1180
+ var expressions []validatorExpressionCtx
1157
1181
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 ) ))
1159
1183
}
1160
1184
1161
1185
functions := []validatorFunctionCtx {{
@@ -1339,7 +1363,7 @@ func generateValidatorFnsMapForVariants(variants []*properties.SpecParam) map[in
1339
1363
}
1340
1364
1341
1365
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 ) )
1343
1367
1344
1368
validatorFns [elt .VariantGroupId ] = validator
1345
1369
}
@@ -1463,18 +1487,19 @@ func createSchemaSpecForParameter(schemaTyp properties.SchemaType, manager *impo
1463
1487
}
1464
1488
1465
1489
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 ,
1478
1503
})
1479
1504
1480
1505
for _ , elt := range param .Spec .SortedParams () {
@@ -1533,6 +1558,7 @@ func createSchemaSpecForParameter(schemaTyp properties.SchemaType, manager *impo
1533
1558
Functions : []validatorFunctionCtx {* validatorFn },
1534
1559
}
1535
1560
}
1561
+
1536
1562
schemas = append (schemas , createSchemaSpecForParameter (schemaTyp , manager , structName , packageName , elt , validators )... )
1537
1563
}
1538
1564
}
@@ -1967,6 +1993,7 @@ func createSchemaSpecForNormalization(resourceTyp properties.ResourceType, schem
1967
1993
1968
1994
const renderSchemaTemplate = `
1969
1995
{{- define "renderSchemaListAttribute" }}
1996
+ {{- with .Attribute }}
1970
1997
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
1971
1998
Description: "{{ .Description }}",
1972
1999
Required: {{ .Required }},
@@ -1981,7 +2008,7 @@ const renderSchemaTemplate = `
1981
2008
{{- if eq .Type "Expressions" }}
1982
2009
{{ $package }}.{{ .Function }}(path.Expressions{
1983
2010
{{- range .Expressions }}
1984
- {{ . }},
2011
+ {{ .RenderAsString $.Schema.IsListAttribute }},
1985
2012
{{- end }}
1986
2013
}...),
1987
2014
@@ -1997,8 +2024,10 @@ const renderSchemaTemplate = `
1997
2024
{{- end }}
1998
2025
},
1999
2026
{{- end }}
2027
+ {{- end }}
2000
2028
2001
2029
{{- define "renderSchemaMapAttribute" }}
2030
+ {{- with .Attribute }}
2002
2031
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
2003
2032
Description: "{{ .Description }}",
2004
2033
Required: {{ .Required }},
@@ -2008,18 +2037,19 @@ const renderSchemaTemplate = `
2008
2037
ElementType: {{ .ElementType }},
2009
2038
},
2010
2039
{{- end }}
2040
+ {{- end }}
2011
2041
2012
2042
{{- define "renderSchemaListNestedAttribute" }}
2013
- {{- with .Attribute }}
2043
+ {{- with .Attribute }}
2014
2044
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
2015
2045
Description: "{{ .Description }}",
2016
2046
Required: {{ .Required }},
2017
2047
Optional: {{ .Optional }},
2018
2048
Computed: {{ .Computed }},
2019
2049
Sensitive: {{ .Sensitive }},
2020
- NestedObject: {{ $.StructName }}{{ .Name.CamelCase }}Schema(),
2050
+ NestedObject: {{ $.Schema. StructName }}{{ .Name.CamelCase }}Schema(),
2021
2051
},
2022
- {{- end }}
2052
+ {{- end }}
2023
2053
{{- end }}
2024
2054
2025
2055
{{- define "renderSchemaMapNestedAttribute" }}
@@ -2028,18 +2058,19 @@ const renderSchemaTemplate = `
2028
2058
2029
2059
2030
2060
{{- 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 }}
2034
2064
{{- end }}
2035
2065
2036
2066
{{- define "renderSchemaExternalAttribute" }}
2037
- {{- with .Attribute }}
2067
+ {{- with .Attribute }}
2038
2068
"{{ .Name.Underscore }}": {{ .ExternalType }}Schema(),
2039
- {{- end }}
2069
+ {{- end }}
2040
2070
{{- end }}
2041
2071
2042
2072
{{- define "renderSchemaSimpleAttribute" }}
2073
+ {{- with .Attribute }}
2043
2074
"{{ .Name.Underscore }}": {{ .Package }}.{{ .SchemaType }} {
2044
2075
Description: "{{ .Description }}",
2045
2076
Computed: {{ .Computed }},
@@ -2064,7 +2095,7 @@ const renderSchemaTemplate = `
2064
2095
{{- if eq .Type "Expressions" }}
2065
2096
{{ $package }}.{{ .Function }}(path.Expressions{
2066
2097
{{- range .Expressions }}
2067
- {{ . }},
2098
+ {{ .RenderAsString $.Schema.IsListAttribute }},
2068
2099
{{- end }}
2069
2100
}...),
2070
2101
{{- else if eq .Type "Values" }}
@@ -2079,23 +2110,24 @@ const renderSchemaTemplate = `
2079
2110
{{- end }}
2080
2111
},
2081
2112
{{- end }}
2113
+ {{- end }}
2082
2114
2083
2115
{{- define "renderSchemaAttribute" }}
2084
2116
{{- with .Attribute }}
2085
2117
{{ if or (eq .SchemaType "ListAttribute") (eq .SchemaType "SetAttribute") }}
2086
- {{- template "renderSchemaListAttribute" . }}
2118
+ {{- template "renderSchemaListAttribute" Map "Schema" $.Schema "Attribute" . }}
2087
2119
{{- else if eq .SchemaType "MapAttribute" }}
2088
- {{- template "renderSchemaMapAttribute" . }}
2120
+ {{- template "renderSchemaMapAttribute" Map "Schema" $.Schema "Attribute" . }}
2089
2121
{{- else if eq .SchemaType "ListNestedAttribute" }}
2090
- {{- template "renderSchemaListNestedAttribute" Map "StructName " $.StructName "Attribute" . }}
2122
+ {{- template "renderSchemaListNestedAttribute" Map "Schema " $.Schema "Attribute" . }}
2091
2123
{{ else if eq .SchemaType "MapNestedAttribute" }}
2092
- {{- template "renderSchemaMapNestedAttribute" Map "StructName " $.StructName "Attribute" . }}
2124
+ {{- template "renderSchemaMapNestedAttribute" Map "Schema " $.Schema "Attribute" . }}
2093
2125
{{- else if eq .SchemaType "SingleNestedAttribute" }}
2094
- {{- template "renderSchemaSingleNestedAttribute" Map "StructName " $.StructName "Attribute" . }}
2126
+ {{- template "renderSchemaSingleNestedAttribute" Map "Schema " $.Schema "Attribute" . }}
2095
2127
{{- else if eq .SchemaType "ExternalAttribute" }}
2096
- {{- template "renderSchemaExternalAttribute" Map "Attribute" . }}
2128
+ {{- template "renderSchemaExternalAttribute" Map "Schema" $.Schema " Attribute" . }}
2097
2129
{{- else }}
2098
- {{- template "renderSchemaSimpleAttribute" . }}
2130
+ {{- template "renderSchemaSimpleAttribute" Map "Schema" $.Schema "Attribute" . }}
2099
2131
{{- end }}
2100
2132
{{- end }}
2101
2133
{{- end }}
@@ -2118,15 +2150,15 @@ func {{ .StructName }}Schema() {{ .Package }}.{{ .ReturnType }} {
2118
2150
{{- range .Functions }}
2119
2151
{{ $package }}.{{ .Function }}(path.Expressions{
2120
2152
{{- range .Expressions }}
2121
- {{ . }},
2153
+ {{ .RenderAsString $schema.IsListAttribute }},
2122
2154
{{- end }}
2123
2155
}...),
2124
2156
{{- end }}
2125
2157
},
2126
2158
{{- end }}
2127
2159
Attributes: map[string]{{ .Package }}.Attribute{
2128
2160
{{- range .Attributes -}}
2129
- {{- template "renderSchemaAttribute" Map "StructName " $schema.StructName "Attribute" . }}
2161
+ {{- template "renderSchemaAttribute" Map "Schema " $schema "Attribute" . }}
2130
2162
{{- end }}
2131
2163
},
2132
2164
}
0 commit comments