Skip to content

Commit

Permalink
make semantic equality symmetrical
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Dec 10, 2024
1 parent 19ee77e commit 265d56e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 10 additions & 0 deletions apstra/custom_types/string_with_alt_values_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (v StringWithAltValues) Equal(o attr.Value) bool {
return v.StringValue.Equal(other.StringValue)
}

// StringSemanticEquals implements the semantic equality check. According to this
// (https://discuss.hashicorp.com/t/can-semantic-equality-check-in-custom-types-be-asymmetrical/60644/2?u=hqnvylrx)
// semantic equality checks on custom types are always implementeed as oldValue.SemanticEquals(ctx, newValue)
func (v StringWithAltValues) StringSemanticEquals(_ context.Context, newValuable basetypes.StringValuable) (bool, diag.Diagnostics) {
var diags diag.Diagnostics

Expand Down Expand Up @@ -59,6 +62,13 @@ func (v StringWithAltValues) StringSemanticEquals(_ context.Context, newValuable
}
}

// check old value against new "alt" values
for _, a := range newValue.altValues {
if a.Equal(v) {
return true, diags
}
}

return false, diags
}

Expand Down
7 changes: 6 additions & 1 deletion apstra/custom_types/string_with_alt_values_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ func TestStringWithAltValues_StringSemanticEquals(t *testing.T) {
givenValue: customtypes.NewStringWithAltValuesValue("foo"),
expectedMatch: true,
},
"semantically equal": {
"semantically equal - given matches an alt value": {
currentValue: customtypes.NewStringWithAltValuesValue("foo", "bar", "baz", "bang"),
givenValue: customtypes.NewStringWithAltValuesValue("baz"),
expectedMatch: true,
},
"semantically equal - current matches an alt value": {
currentValue: customtypes.NewStringWithAltValuesValue("baz"),
givenValue: customtypes.NewStringWithAltValuesValue("foo", "bar", "baz", "bang"),
expectedMatch: true,
},
"not equal": {
currentValue: customtypes.NewStringWithAltValuesValue("foo", "bar", "baz", "bang"),
givenValue: customtypes.NewStringWithAltValuesValue("FOO"),
Expand Down

0 comments on commit 265d56e

Please sign in to comment.