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

Allow using ValueAs into attr.Value. #232

Merged
merged 2 commits into from
Dec 7, 2021
Merged

Conversation

paddycarver
Copy link
Contributor

Allow for generic handling of attr.Values by special-casing ValueAs
(and, consequently, GetAttribute) to bypass reflection when we want to
just obtain the raw attr.Value.

Fixes #230.

Allow for generic handling of attr.Values by special-casing ValueAs
(and, consequently, GetAttribute) to bypass reflection when we want to
just obtain the raw attr.Value.
@paddycarver paddycarver added the enhancement New feature or request label Dec 3, 2021
@paddycarver paddycarver requested a review from a team December 3, 2021 15:29
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Looks good to me 🚀

I'd love to unit test this to match the CHANGELOG entry since we have expectations for what is to occur "at a distance", but it would not be a trivial inclusion. e.g. adding the below test case to TestStateGetAttribute

		"attr.Value": {
			state: State{
				Raw: tftypes.NewValue(tftypes.Object{
					AttributeTypes: map[string]tftypes.Type{
						"name": tftypes.String,
					},
				}, map[string]tftypes.Value{
					"name": tftypes.NewValue(tftypes.String, "namevalue"),
				}),
				Schema: Schema{
					Attributes: map[string]Attribute{
						"name": {
							Type:     types.StringType,
							Required: true,
						},
					},
				},
			},
			target:   new(attr.Value),
			expected: &types.String{Value: "namevalue"},
		},

Yields:

--- FAIL: TestStateGetAttribute (0.00s)
    --- FAIL: TestStateGetAttribute/attr.Value (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/state_test.go:1070: unexpected value (+wanted, -got):   interface{}(
            - 	&⟪0xc00020ffb0⟫"github.com/hashicorp/terraform-plugin-framework/types".String{Value: "namevalue"},
            + 	&⟪0xc00020c7f8⟫"github.com/hashicorp/terraform-plugin-framework/types".String{Value: "namevalue"},
              )

Because the types.String Equal method is implemented as a value receiver and not as a pointer receiver. Semi-relates to proposal 2 of #201 and #221.

@paddycarver
Copy link
Contributor Author

Yeah, I think that's what I found. Which is a bummer. Need to think about how to do that properly.... :(

@paddycarver paddycarver merged commit 934250c into main Dec 7, 2021
@paddycarver paddycarver deleted the paddy_generic_valueas branch December 7, 2021 20:22
@github-actions
Copy link

github-actions bot commented Jan 7, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow "generic" use of State.GetAttribute
2 participants