Skip to content

Commit b37fbd7

Browse files
committed
[TGC] Suppress schema default values in CAI-to-HCL flattener templates
1 parent 8a7e6d0 commit b37fbd7

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

mmv1/api/type.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,3 +1562,15 @@ func (t Type) HasRequiredProperty() bool {
15621562
}
15631563
return false
15641564
}
1565+
1566+
func (t *Type) HasAncestorCustomSet() bool {
1567+
p := t.ParentMetadata
1568+
for p != nil {
1569+
if p.IsSet && p.SetHashFunc != "" {
1570+
return true
1571+
}
1572+
p = p.ParentMetadata
1573+
}
1574+
return false
1575+
}
1576+

mmv1/templates/tgc_next/cai2hcl/flatten_property_method_tgc.go.tmpl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,23 @@ func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.Reso
140140
// Handles the string fixed64 format
141141
if strVal, ok := v.(string); ok {
142142
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
143+
{{- if and (ne $.DefaultValue nil) (not $.Required) (not $.HasAncestorCustomSet) }}
144+
if intVal == {{ $.GoLiteral $.DefaultValue }} {
145+
return nil
146+
}
147+
{{- end }}
143148
return intVal
144149
}
145150
}
146151

147152
// number values are represented as float64
148153
if floatVal, ok := v.(float64); ok {
149154
intVal := int(floatVal)
155+
{{- if and (ne $.DefaultValue nil) (not $.Required) (not $.HasAncestorCustomSet) }}
156+
if intVal == {{ $.GoLiteral $.DefaultValue }} {
157+
return nil
158+
}
159+
{{- end }}
150160
return intVal
151161
}
152162

@@ -156,6 +166,15 @@ func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.Reso
156166
}
157167
{{- end }}
158168

169+
{{- if and (ne $.DefaultValue nil) (not $.Required) (not $.HasAncestorCustomSet) }}
170+
if intVal, ok := v.(int); ok && intVal == {{ $.GoLiteral $.DefaultValue }} {
171+
return nil
172+
}
173+
if floatVal, ok := v.(float64); ok && floatVal == {{ $.GoLiteral $.DefaultValue }} {
174+
return nil
175+
}
176+
{{- end }}
177+
159178
return v // let terraform core handle it otherwise
160179
{{- else if and ($.IsA "Array") ($.ItemType.IsA "ResourceRef")}}
161180
if v == nil {
@@ -196,6 +215,21 @@ func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.Reso
196215
}
197216
return v
198217
{{- else }}
218+
{{- if and (ne $.DefaultValue nil) (not $.Required) (not $.HasAncestorCustomSet) }}
219+
{{- if or ($.IsA "String") (or ($.IsA "Enum") ($.IsA "ResourceRef")) }}
220+
if strVal, ok := v.(string); ok && strVal == {{ $.GoLiteral $.DefaultValue }} {
221+
return nil
222+
}
223+
{{- else if $.IsA "Boolean" }}
224+
if boolVal, ok := v.(bool); ok && boolVal == {{ $.GoLiteral $.DefaultValue }} {
225+
return nil
226+
}
227+
{{- else if $.IsA "Double" }}
228+
if floatVal, ok := v.(float64); ok && floatVal == {{ $.GoLiteral $.DefaultValue }} {
229+
return nil
230+
}
231+
{{- end }}
232+
{{- end }}
199233
return v
200234
{{- end }}
201235
}

0 commit comments

Comments
 (0)