terraform-plugin-framework icon indicating copy to clipboard operation
terraform-plugin-framework copied to clipboard

"Follow" Embedded Structs

Open frankgreco opened this issue 3 years ago • 10 comments

terraform-plugin-go version

v0.4.0

Use cases

Consider the following type 👇🏼

type Port struct {
	FromPort int `json:"fromPort" tfsdk:"from_port"`
	ToPort   int `json:"toPort" tfsdk:"to_port"`
}

type Source struct {
	Address string `json:"address" tfsdk:"address"`
	MAC     string `json:"mac" tfsdk:"mac"`
	*Port
}

In JSON, address, mac, fromPort, and toPort would all be at the same level. I want this identical behavior for tdsdk. Currently, it'll complain that the embedded *Port field doesn't contain a tag.

Attempted solutions

I have a workaround worth about 100 lines of code that implements FromTerraform5Value and ToTerraform5Value.

Proposal

Implement the same behavior as encoding/json so that...

"destination": {
	Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
		"from_port": {
			Type:     types.NumberType,
			Required: true,
		},
		"to_port": {
			Type:     types.NumberType,
			Required: true,
		},
		"address": {
			Type:     types.StringType,
			Optional: true,
		},
	}),
	Required: true,
},

...will natively be unmarshaled into the aforementioned type.

References

frankgreco avatar Dec 28 '21 08:12 frankgreco

This issue appears to be specific to terraform-plugin-framework, so going to transfer it over there.

bflad avatar Jan 03 '22 18:01 bflad

I would love this feature. It would allow me to embed a BaseModel in specific ResourcesModel and DataSourceModel. Currently I need to duplicate all the fields.

@frankgreco If you still have the workaround, would you be willing to share it?

skirsten avatar Feb 09 '23 19:02 skirsten

@skirsten I gave up and restructured my Go types. TBH I'm kind of disappointed in the level of engagement these GH issues get from developing trying to try the new framework.

frankgreco avatar Feb 09 '23 20:02 frankgreco

Additional HashiCorp Discuss references:

  • https://discuss.hashicorp.com/t/struct-embedding-in-attribute-models/51456
  • https://discuss.hashicorp.com/t/terraform-plugin-framework-nested-itself/51504

Will bring this up during our team's next regularly scheduled triage meeting to get it on the schedule. To set expectations, efforts (reviews or implementations) in this area may not happen until late April as we already have other features queued up in the meantime.

bflad avatar Mar 15 '23 14:03 bflad

  • 1 here, also want this feature.

shufanhao avatar Mar 16 '23 00:03 shufanhao

Time has passed and this is a useful feature. @bflad, has the team had a chance to look into merging the PR implementing this? I'm facing the same issue today.

aybabtme avatar Sep 19 '23 21:09 aybabtme

UP ?

miton18 avatar Dec 12 '23 13:12 miton18

I just got done refactoring a provider because I never in 100 years would've thought that embedding a struct would not be supported.

ddouglas avatar Apr 02 '24 19:04 ddouglas

This is a much needed feature. I am also facing the need of refactoring a complete provider because I cannot share a BaseModel between datasource and resource, if this feature is not implemented

vicentepinto98 avatar Apr 22 '24 09:04 vicentepinto98

+1 🙏

DariuszPorowski avatar May 18 '24 17:05 DariuszPorowski