Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

all: Add support for embedded struct types in object conversions#1021

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
austinvalle merged 17 commits intomainfromav/embedded-structs
Aug 2, 2024

Conversation

@austinvalle
Copy link
Member

@austinvalleaustinvalle commentedJul 22, 2024
edited
Loading

Closes#242

Previous proposals:#941,#667

This PR introduces support fortypes.Object conversions with Go structs that utilize struct embedding:

This PR supports N levels of nested embedded struct types and applies the sametfsdk tag restrictions/validations regardless of where the tag appears in the struct.

Example

typethingResourceModelstruct {Attr1 types.String`tfsdk:"attr_1"`Attr2 types.Bool`tfsdk:"attr_2"`CommonModel}typeCommonModelstruct {Attr3 types.Int64`tfsdk:"attr_3"`Attr4 types.List`tfsdk:"attr_4"`EvenMoreCommonModel}typeEvenMoreCommonModelstruct {Attr5 types.String`tfsdk:"attr_5"`Attr6 types.Map`tfsdk:"attr_6"`}func (r*thingResource)Schema(ctx context.Context,req resource.SchemaRequest,resp*resource.SchemaResponse) {resp.Schema= schema.Schema{Attributes:map[string]schema.Attribute{"attr_1": schema.StringAttribute{Required:true,            },"attr_2": schema.BoolAttribute{Computed:true,PlanModifiers: []planmodifier.Bool{boolplanmodifier.UseStateForUnknown(),                },            },"attr_3": schema.Int64Attribute{Required:true,            },"attr_4": schema.ListAttribute{ElementType:types.StringType,Computed:true,PlanModifiers: []planmodifier.List{listplanmodifier.UseStateForUnknown(),                },            },"attr_5": schema.StringAttribute{Required:true,            },"attr_6": schema.MapAttribute{ElementType:types.BoolType,Computed:true,PlanModifiers: []planmodifier.Map{mapplanmodifier.UseStateForUnknown(),                },            },        },    }}

The same example schema could also be represented by these model combinations:

typethingResourceModelstruct {Attr1 types.String`tfsdk:"attr_1"`Attr2 types.Bool`tfsdk:"attr_2"`CommonModelEvenMoreCommonModel}typeCommonModelstruct {Attr3 types.Int64`tfsdk:"attr_3"`Attr4 types.List`tfsdk:"attr_4"`}typeEvenMoreCommonModelstruct {Attr5 types.String`tfsdk:"attr_5"`Attr6 types.Map`tfsdk:"attr_6"`}

Notes

  • Additional test coverage:Add reflection tests for object models withtfsdk tags in framework providers terraform-provider-corner#263
  • To avoid having to refactor a good chunk of our reflection logic, we explicitly are not supporting struct embedding with pointers. That doesn't stop us from adding support in the future if it's deemed necessary (although supporting unexported struct embeds from pointers I don't think we can do)
    • Theencoding/json library supports exported struct embedding via pointers, but it was also historically relying on a bug in Go that allowed setting unexported struct embedding with pointers to work. When that bug was fixed, they chose not to completely remove pointer struct embedding for compatibility reasons.
  • This PR could be considered a change in behavior, as it adds support for unexported embedded structs which promote exported fields
    • These would be previously ignored, but now could potentially raise an error diagnostic iftfsdk ignore tags are not included
typethingResourceModelstruct {Attr1 types.String`tfsdk:"attr_1"`Attr2 types.Bool`tfsdk:"attr_2"`existingModel}typeexistingModelstruct {FieldNotRelatedstring// Needs a tfsdk tag now!}
  • You can ignore an entire embedded struct via the typical ignore tag
typethingResourceModelstruct {Attr1 types.String`tfsdk:"attr_1"`Attr2 types.Bool`tfsdk:"attr_2"`existingModel`tfsdk:"-"`// Ignored!}typeexistingModelstruct {FieldNotRelatedstring}

svengreb, maciaszczykm, and DariuszPorowski reacted with rocket emoji
@austinvalleaustinvalle added the enhancementNew feature or request labelJul 22, 2024
@austinvalleaustinvalle added this to thev1.11.0 milestoneJul 22, 2024
@austinvalleaustinvalle requested a review froma team as acode ownerJuly 22, 2024 21:49
Copy link
Contributor

@SBGoodsSBGoods left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Great job, Austin! Just one comment about empty struct tags.

@austinvalleaustinvalle merged commit4d10c17 intomainAug 2, 2024
@austinvalleaustinvalle deleted the av/embedded-structs branchAugust 2, 2024 13:41
@SBGoodsSBGoods mentioned this pull requestAug 5, 2024
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsSep 1, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@SBGoodsSBGoodsSBGoods approved these changes

Assignees

No one assigned

Labels

enhancementNew feature or request

Projects

None yet

Milestone

v1.11.0

Development

Successfully merging this pull request may close these issues.

"Follow" Embedded Structs

2 participants

@austinvalle@SBGoods

[8]ページ先頭

©2009-2025 Movatter.jp