Skip to content

Commit

Permalink
Prevent declaration of unused variables (#93)
Browse files Browse the repository at this point in the history
* Only generate variables if AttrTypes value passed into template is non-empty (#92)

* Adding tests for ValueFromObject and ToTerraformValue when no attributes present (#92)

* Adding changelog entry (#92)
  • Loading branch information
bendbennett authored Nov 22, 2023
1 parent 2867569 commit 868c558
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20231122-070804.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'schema: Prevent compilation errors due to the generation of unused variables'
time: 2023-11-22T07:08:04.939979Z
custom:
Issue: "93"
46 changes: 46 additions & 0 deletions internal/schema/custom_nested_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,23 @@ return ExampleValue{
ExampleType: typeVal,
state: attr.ValueStateKnown,
}, diags
}`),
},
"no-attributes": {
name: "Example",
expected: []byte(`
func (t ExampleType) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
var diags diag.Diagnostics
if diags.HasError() {
return nil, diags
}
return ExampleValue{
state: attr.ValueStateKnown,
}, diags
}`),
},
}
Expand Down Expand Up @@ -1410,6 +1427,35 @@ vals["type"] = val
if err := tftypes.ValidateValue(objectType, vals); err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
return tftypes.NewValue(objectType, vals), nil
case attr.ValueStateNull:
return tftypes.NewValue(objectType, nil), nil
case attr.ValueStateUnknown:
return tftypes.NewValue(objectType, tftypes.UnknownValue), nil
default:
panic(fmt.Sprintf("unhandled Object state in ToTerraformValue: %s", v.state))
}
}`),
},
"no-attributes": {
name: "Example",
expected: []byte(`func (v ExampleValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 0)
objectType := tftypes.Object{AttributeTypes: attrTypes}
switch v.state {
case attr.ValueStateKnown:
vals := make(map[string]tftypes.Value, 0)
if err := tftypes.ValidateValue(objectType, vals); err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
func (t {{.Name}}Type) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
var diags diag.Diagnostics

{{- if .AttrValues}}

attributes := in.Attributes()
{{- end}}

{{range $key, $value := .AttrValues }}
{{$key.ToCamelCase}}Attribute, ok := attributes["{{$key}}"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
func (v {{.Name}}Value) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, {{len .AttrTypes}})

{{- if .AttrTypes}}

var val tftypes.Value
var err error
{{- end}}

{{range $key, $value := .AttrTypes }}
attrTypes["{{$key}}"] = {{$value}}.TerraformType(ctx)
Expand Down

0 comments on commit 868c558

Please sign in to comment.