Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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