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

tftypes: Adjust (tftypes.Value).Equal function to support dynamic value comparisons #383

Merged
merged 6 commits into from
Feb 29, 2024

Conversation

austinvalle
Copy link
Member

@austinvalle austinvalle commented Feb 27, 2024

Ref: hashicorp/terraform-plugin-framework#931

This change adjusts (tftypes.Value).Equal to not panic if the concrete values don't match 😄 .

This is an edge-case that was originally uncovered in the linked dynamic PR because it's possible that the types may be equal, but the concrete values are different when compared: https://github.com/hashicorp/terraform-plugin-framework/pull/931/files?show-viewed-files=true&file-filters%5B%5D=#diff-d827ef82ad83975f9b9a8cd0128db4a0798d0fb6878aea019ad4e1d642cb07b1

This can be caused with a types.Dynamic when changing the type during a refresh, i.e. ReadResource RPC

tftypes/value.go Outdated Show resolved Hide resolved
@austinvalle austinvalle changed the title (WIP) - Proposed (tftypes.Value).SafeEqual function to support dynamic value comparisons tftypes - Adjust (tftypes.Value).Equal function to support dynamic value comparisons Feb 28, 2024
@austinvalle austinvalle added this to the v0.22.1 milestone Feb 28, 2024
@austinvalle austinvalle marked this pull request as ready for review February 28, 2024 22:18
@austinvalle austinvalle requested a review from a team as a code owner February 28, 2024 22:18
@austinvalle
Copy link
Member Author

I'd love to release a v0.22.1 with this change whenever the PR is approved 🙂

@austinvalle austinvalle changed the title tftypes - Adjust (tftypes.Value).Equal function to support dynamic value comparisons tftypes: Adjust (tftypes.Value).Equal function to support dynamic value comparisons Feb 28, 2024
Copy link
Contributor

@bendbennett bendbennett left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

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, although I would also suggest adding a unit test case which previously panicked. 🚀

@bflad bflad added the bug Something isn't working label Feb 29, 2024
@austinvalle
Copy link
Member Author

@bflad Looks good to me, although I would also suggest adding a unit test case which previously panicked. 🚀

Whoops, completely forgot about the tests 😅 . Added a bunch of tests (including some missing equality type diffs that weren't related to the panic) in 72f9762.

I'm also going to hold off on creating a v0.22.1 release to avoid creating too much noise around the function changes. I don't need the release until we get closer to merging the dynamic PR 👍🏻

Before changes (tuple test)

--- FAIL: TestValueEqual (0.00s)
    --- FAIL: TestValueEqual/DynamicPseudoType-tupleDiff-different-value-types (0.00s)
panic: ElementKeyInt(0): cannot convert bool into string [recovered]
	panic: ElementKeyInt(0): cannot convert bool into string

goroutine 37 [running]:
testing.tRunner.func1.2({0x10446ba20, 0x1400020ea50})
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1545 +0x1c8
testing.tRunner.func1()
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1548 +0x360
panic({0x10446ba20?, 0x1400020ea50?})
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/runtime/panic.go:914 +0x218
github.com/hashicorp/terraform-plugin-go/tftypes.Value.Equal({{0x10448d868?, 0x14000184000?}, {0x10442aae0?, 0x1400000c948?}}, {{0x10448d868?, 0x140001840f0?}, {0x10442aae0?, 0x1400000c978?}})
	/Users/austin.valle/code/terraform-plugin-go/tftypes/value.go:226 +0x114
github.com/hashicorp/terraform-plugin-go/tftypes.TestValueEqual.func1(0x140001b41a0)
	/Users/austin.valle/code/terraform-plugin-go/tftypes/value_test.go:1213 +0x6c
testing.tRunner(0x140001b41a0, 0x14000010c80)
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1595 +0xe8
created by testing.(*T).Run in goroutine 6
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1648 +0x33c
FAIL	github.com/hashicorp/terraform-plugin-go/tftypes	0.463s
FAIL

Before changes (object test)

--- FAIL: TestValueEqual (0.00s)
    --- FAIL: TestValueEqual/DynamicPseudoType-objectDiff-different-value-types (0.00s)
panic: AttributeName("dyn_val"): cannot convert *big.Float into string [recovered]
	panic: AttributeName("dyn_val"): cannot convert *big.Float into string

goroutine 36 [running]:
testing.tRunner.func1.2({0x102513a20, 0x1400028e0c0})
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1545 +0x1c8
testing.tRunner.func1()
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1548 +0x360
panic({0x102513a20?, 0x1400028e0c0?})
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/runtime/panic.go:914 +0x218
github.com/hashicorp/terraform-plugin-go/tftypes.Value.Equal({{0x1025357b8?, 0x140001be300?}, {0x1024e91c0?, 0x140001be240?}}, {{0x1025357b8?, 0x140001be480?}, {0x1024e91c0?, 0x140001be390?}})
	/Users/austin.valle/code/terraform-plugin-go/tftypes/value.go:226 +0x114
github.com/hashicorp/terraform-plugin-go/tftypes.TestValueEqual.func1(0x140001ce680)
	/Users/austin.valle/code/terraform-plugin-go/tftypes/value_test.go:1213 +0x6c
testing.tRunner(0x140001ce680, 0x14000130780)
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1595 +0xe8
created by testing.(*T).Run in goroutine 18
	/opt/homebrew/Cellar/go/1.21.4/libexec/src/testing/testing.go:1648 +0x33c
FAIL	github.com/hashicorp/terraform-plugin-go/tftypes	0.502s
FAIL

@austinvalle austinvalle merged commit 570828e into main Feb 29, 2024
66 checks passed
@austinvalle austinvalle deleted the av/to-dpt branch February 29, 2024 18:34
@bflad
Copy link
Contributor

bflad commented Mar 1, 2024

Nice work on those tests 👍

Copy link

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 May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants