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

internal/fwschemadata: Rewrite SetValue semantic equality logic to ignore order #1064

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

austinvalle
Copy link
Member

@austinvalle austinvalle commented Dec 6, 2024

Closes #1061

This PR fixes the set semantic equality logic, which prior to this change, was written equivalent to list semantic equality.

Sets are unordered, so to correctly compare the semantic equality of two sets, we must compare each element in the set against all other elements to determine if there is a semantically equivalent element in the set. Once we find a semantically equal element, we remove it from the slice of candidates and continue.


As an example, consider a set of case insensitive strings.

With the previous logic, the following sets would be considered semantically equal:

["value-a", "value-b", "value-c"]
["VALUE-A", "VALUE-B", "VALUE-C"]

However the following set would be incorrectly considered semantically not equal, due to the difference in order:

["value-a", "value-b", "value-c"]
["VALUE-B", "VALUE-C", "VALUE-A"]

This change will result in both of these examples being considered semantically equal.

Corner tests

I also wrote some acceptance tests to make it a little easier to understand the impact (using IPv6 address strings as an example)

hashicorp/terraform-provider-corner#297

Unit test failures before the fix

https://github.com/hashicorp/terraform-plugin-framework/actions/runs/12832297072/job/35784696031?pr=1064

--- FAIL: TestValueSemanticEqualitySet (0.00s)
    --- FAIL: TestValueSemanticEqualitySet/SetValue-SetValue-StringValuableWithSemanticEquals-true-diff-order (0.00s)
        value_semantic_equality_set_test.go:1021: unexpected difference:   &fwschemadata.ValueSemanticEqualityResponse{
            - 	NewValue:    s`[["KEEP-LOWERCASE-012","KEEP-LOWERCASE-789"],["KEEP-LOWERCASE-456","KEEP-LOWERCASE-123"]]`,
            + 	NewValue:    s`[["keep-lowercase-123","keep-lowercase-456"],["keep-lowercase-789","keep-lowercase-012"]]`,
              	Diagnostics: nil,
              }
    --- FAIL: TestValueSemanticEqualitySet/SetValue-StringValuableWithSemanticEquals-true-diff-order (0.00s)
        value_semantic_equality_set_test.go:10[21](https://github.com/hashicorp/terraform-plugin-framework/actions/runs/12832297072/job/35784696031?pr=1064#step:5:22): unexpected difference:   &fwschemadata.ValueSemanticEqualityResponse{
            - 	NewValue:    s`["KEEP-LOWERCASE-456","KEEP-LOWERCASE-123"]`,
            + 	NewValue:    s`["keep-lowercase-123","keep-lowercase-456"]`,
              	Diagnostics: nil,
              }

@austinvalle austinvalle marked this pull request as ready for review January 17, 2025 16:01
@austinvalle austinvalle requested a review from a team as a code owner January 17, 2025 16:01
@austinvalle austinvalle added this to the v1.14.1 milestone Jan 17, 2025
@austinvalle
Copy link
Member Author

austinvalle commented Jan 17, 2025

Since we have write-only attributes landing soon, this bug fix will go in after in v1.14.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Semantic Equal on Sets expects identical order of elements
1 participant