Skip to content

Commit

Permalink
Using a struct for attributes nested within a block (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Aug 7, 2023
1 parent 2894156 commit b7ef884
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/cmd/testdata/custom_and_external/ir.json
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@
"bool": {
"computed_optional_required": "computed"
}
},
{
"name": "int64_attribute",
"int64": {
"computed_optional_required": "computed_optional"
}
}
]
}
Expand Down
60 changes: 38 additions & 22 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ func (g GeneratorSchema) ModelsToFromBytes() ([]byte, error) {
continue
}

// TODO: Type check is only required until all blocks and attributes
// implement AssocExtType().
// TODO: Type check is only required until all blocks and attributes implement AssocExtType().
var blockAssocExtType GeneratorBlockAssocExtType
var ok bool

Expand All @@ -627,36 +626,53 @@ func (g GeneratorSchema) ModelsToFromBytes() ([]byte, error) {
return nil, fmt.Errorf("all block types must implement Blocks, %s does not", k)
}

attributes := make(map[string]map[string]string)
type attribute struct {
Name string
HasAssocExtType bool
To string
From string
}

var attributes []attribute

nestedAttributes := a.GetAttributes()

// Using sorted blockKeys to guarantee block order as maps are unordered in Go.
var nestedAttributeKeys = make([]string, 0, len(nestedAttributes))

for nk := range nestedAttributes {
nestedAttributeKeys = append(nestedAttributeKeys, nk)
}

sort.Strings(nestedAttributeKeys)

// TODO: Check whether v implements AssocExtType, and use that in preference to "default".
for x, v := range a.GetAttributes() {
switch v.AttrType() {
for _, x := range nestedAttributeKeys {
switch nestedAttributes[x].AttrType() {
case types.BoolType:
// TODO: Remove type assertion once all attributes and blocks implement AssocExtType()
if y, ok := v.(GeneratorAttributeAssocExtType); ok {
if y, ok := nestedAttributes[x].(GeneratorAttributeAssocExtType); ok {
if y.AssocExtType() != nil {

attributes[model.SnakeCaseToCamelCase(x)] = map[string]string{
"toAssocExtType": fmt.Sprintf("To%s(ctx, tfModel.%s)", model.SnakeCaseToCamelCase(x), model.SnakeCaseToCamelCase(x)),
"toAssocExtTypeVar": fmt.Sprintf("to%s", model.SnakeCaseToCamelCase(x)),
"fromAssocExtType": fmt.Sprintf("From%s(ctx, apiObject.%s)", model.SnakeCaseToCamelCase(x), model.SnakeCaseToCamelCase(x)),
"fromAssocExtTypeVar": fmt.Sprintf("from%s", model.SnakeCaseToCamelCase(x)),
}
attributes = append(attributes, attribute{
Name: model.SnakeCaseToCamelCase(x),
HasAssocExtType: true,
})

continue
}

attributes[model.SnakeCaseToCamelCase(x)] = map[string]string{
"to": "ValueBoolPointer",
"from": "BoolPointerValue",
}
attributes = append(attributes, attribute{
Name: model.SnakeCaseToCamelCase(x),
To: "ValueBoolPointer",
From: "BoolPointerValue",
})
}
case types.Int64Type:
attributes[model.SnakeCaseToCamelCase(x)] = map[string]string{
"to": "ValueInt64Pointer",
"from": "Int64PointerValue",
}
attributes = append(attributes, attribute{
Name: model.SnakeCaseToCamelCase(x),
To: "ValueInt64Pointer",
From: "Int64PointerValue",
})
}
}

Expand All @@ -678,7 +694,7 @@ func (g GeneratorSchema) ModelsToFromBytes() ([]byte, error) {
Name string
Type string
TypeReference string
Attributes map[string]map[string]string
Attributes []attribute
}{
Name: model.SnakeCaseToCamelCase(k),
Type: assocExtType.Type(),
Expand Down
28 changes: 14 additions & 14 deletions internal/templates/model_object_to_from.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ if diags.HasError() {
return nil, diags
}

{{range $key, $value := .Attributes }}
{{- if $value.toAssocExtType}}
{{$value.toAssocExtTypeVar}}, d := {{$value.toAssocExtType}}
{{range $attr := .Attributes }}
{{- if $attr.HasAssocExtType}}
to{{$attr.Name}}, d := To{{$attr.Name}}(ctx, tfModel.{{$attr.Name}})

diags.Append(d...)

Expand All @@ -26,11 +26,11 @@ return nil, diags
{{- end}}

apiObject := &{{.TypeReference}}{
{{- range $key, $value := .Attributes }}
{{- if $value.toAssocExtType}}
{{$key}}: {{$value.toAssocExtTypeVar}},
{{- range $attr := .Attributes }}
{{- if $attr.HasAssocExtType}}
{{$attr.Name}}: to{{$attr.Name}},
{{- else}}
{{$key}}: tfModel.{{$key}}.{{$value.to}}(),
{{$attr.Name}}: tfModel.{{$attr.Name}}.{{$attr.To}}(),
{{- end}}
{{- end}}
}
Expand All @@ -46,9 +46,9 @@ if apiObject == nil {
return tfModel.ObjectNull(ctx), diags
}

{{range $key, $value := .Attributes }}
{{- if $value.fromAssocExtType}}
{{$value.fromAssocExtTypeVar}}, d := {{$value.fromAssocExtType}}
{{range $attr := .Attributes }}
{{- if $attr.HasAssocExtType}}
from{{$attr.Name}}, d := From{{$attr.Name}}(ctx, apiObject.{{$attr.Name}})

diags.Append(d...)

Expand All @@ -58,11 +58,11 @@ return tfModel.ObjectNull(ctx), diags
{{- end}}
{{- end}}

{{range $key, $value := .Attributes }}
{{- if $value.fromAssocExtType}}
tfModel.{{$key}} = {{$value.fromAssocExtTypeVar}}
{{range $attr := .Attributes }}
{{- if $attr.HasAssocExtType}}
tfModel.{{$attr.Name}} = from{{$attr.Name}}
{{- else}}
tfModel.{{$key}} = types.{{$value.from}}(apiObject.{{$key}})
tfModel.{{$attr.Name}} = types.{{$attr.From}}(apiObject.{{$attr.Name}})
{{- end}}
{{- end}}

Expand Down

0 comments on commit b7ef884

Please sign in to comment.