hashicorp / terraform-plugin-framework

A next-generation framework for building Terraform providers.
https://developer.hashicorp.com/terraform/plugin/framework
Mozilla Public License 2.0
301 stars 92 forks source link

"Follow" Embedded Structs #242

Closed frankgreco closed 2 months ago

frankgreco commented 2 years ago

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 commented 2 years ago

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

skirsten commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago
aybabtme commented 1 year ago

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 commented 10 months ago

UP ?

ddouglas commented 6 months ago

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 commented 6 months ago

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 commented 5 months ago

+1 🙏

tmeckel commented 3 months ago

@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 commented 2 months ago

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