Skip to content

Commit bf1d023

Browse files
authored
internal/fwschemadata: Rewrite SetValue semantic equality logic to ignore order (#1064)
* quick rewrite of semantic equality for sets * add unit tests for fix * Revert "quick rewrite of semantic equality for sets" This reverts commit 37749fd. * fix the semantic equal custom type bump * Revert "Revert "quick rewrite of semantic equality for sets"" This reverts commit b37dde1. * changelog * small change * comment * comment updates * remove unnecessary check on prior element length
1 parent e1e6866 commit bf1d023

File tree

4 files changed

+424
-28
lines changed

4 files changed

+424
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'internal/fwschemadata: Set semantic equality logic has been adjusted and will
3+
now ignore order of elements during comparison.'
4+
time: 2025-01-17T11:01:09.848503-05:00
5+
custom:
6+
Issue: "1061"

internal/fwschemadata/value_semantic_equality_set.go

+33-26
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,40 @@ func ValueSemanticEqualitySetElements(ctx context.Context, req ValueSemanticEqua
136136
// Ensure new value always contains all of proposed new value
137137
newValueElements[idx] = proposedNewValueElement
138138

139-
if idx >= len(priorValueElements) {
140-
continue
139+
// Loop through all prior value elements and see if there are any semantically equal elements
140+
for pIdx, priorValueElement := range priorValueElements {
141+
elementReq := ValueSemanticEqualityRequest{
142+
Path: req.Path.AtSetValue(proposedNewValueElement),
143+
PriorValue: priorValueElement,
144+
ProposedNewValue: proposedNewValueElement,
145+
}
146+
elementResp := &ValueSemanticEqualityResponse{
147+
NewValue: elementReq.ProposedNewValue,
148+
}
149+
150+
ValueSemanticEquality(ctx, elementReq, elementResp)
151+
152+
resp.Diagnostics.Append(elementResp.Diagnostics...)
153+
154+
if resp.Diagnostics.HasError() {
155+
return
156+
}
157+
158+
if elementResp.NewValue.Equal(elementReq.ProposedNewValue) {
159+
// This prior value element didn't match, but there could be other elements that do
160+
continue
161+
}
162+
163+
// Prior state was kept, meaning that we found a semantically equal element
164+
updatedElements = true
165+
166+
// Remove the semantically equal element from the slice of candidates
167+
priorValueElements = append(priorValueElements[:pIdx], priorValueElements[pIdx+1:]...)
168+
169+
// Order doesn't matter, so we can just set the prior state element to this index
170+
newValueElements[idx] = elementResp.NewValue
171+
break
141172
}
142-
143-
elementReq := ValueSemanticEqualityRequest{
144-
Path: req.Path.AtSetValue(proposedNewValueElement),
145-
PriorValue: priorValueElements[idx],
146-
ProposedNewValue: proposedNewValueElement,
147-
}
148-
elementResp := &ValueSemanticEqualityResponse{
149-
NewValue: elementReq.ProposedNewValue,
150-
}
151-
152-
ValueSemanticEquality(ctx, elementReq, elementResp)
153-
154-
resp.Diagnostics.Append(elementResp.Diagnostics...)
155-
156-
if resp.Diagnostics.HasError() {
157-
return
158-
}
159-
160-
if elementResp.NewValue.Equal(elementReq.ProposedNewValue) {
161-
continue
162-
}
163-
164-
updatedElements = true
165-
newValueElements[idx] = elementResp.NewValue
166173
}
167174

168175
// No changes required if the elements were not updated.

0 commit comments

Comments
 (0)