Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Follow" Embedded Structs #242

Closed
frankgreco opened this issue Dec 28, 2021 · 12 comments · Fixed by #1021
Closed

"Follow" Embedded Structs #242

frankgreco opened this issue Dec 28, 2021 · 12 comments · Fixed by #1021
Labels
enhancement New feature or request reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values.

Comments

@frankgreco
Copy link

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

@bflad
Copy link
Contributor

bflad commented Jan 3, 2022

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

@bflad bflad transferred this issue from hashicorp/terraform-plugin-go Jan 3, 2022
@bflad bflad added enhancement New feature or request reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values. labels Jan 3, 2022
@skirsten
Copy link
Contributor

skirsten commented Feb 9, 2023

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?

@frankgreco
Copy link
Author

@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.

@bflad
Copy link
Contributor

bflad commented Mar 15, 2023

Additional HashiCorp Discuss references:

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.

@shufanhao
Copy link

  • 1 here, also want this feature.

@aybabtme
Copy link

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.

@miton18
Copy link

miton18 commented Dec 12, 2023

UP ?

@ddouglas
Copy link

ddouglas commented Apr 2, 2024

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

@vicentepinto98
Copy link

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

@DariuszPorowski
Copy link

+1 🙏

@tmeckel
Copy link

tmeckel commented Jul 16, 2024

@bflad @austinvalle can #941 be merged soon? It's somewhat odd that a so much vital feature, that helps to write clean and maintainable code, is not resolved in 4 (!!!) years.

@austinvalle
Copy link
Member

Hi all 👋🏻 , we've merged a solution to this issue which will release with terraform-plugin-framework v1.11.0 sometime next week!

In the meantime, if you'd like to test the feature out, you can use the latest commit from main:

go get -u github.com/hashicorp/terraform-plugin-framework@main

The eventual website documentation can be found here: https://github.com/hashicorp/terraform-plugin-framework/blob/main/website/docs/plugin/framework/handling-data/types/object.mdx#struct-embedding

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values.
Projects
None yet