Skip to content

Commit

Permalink
Revert "quick rewrite of semantic equality for sets"
Browse files Browse the repository at this point in the history
This reverts commit 37749fd.
  • Loading branch information
austinvalle committed Jan 17, 2025
1 parent e0b7f9c commit b37dde1
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions internal/fwschemadata/value_semantic_equality_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ func ValueSemanticEqualitySetElements(ctx context.Context, req ValueSemanticEqua
// Short circuit flag
updatedElements := false

// The underlying loop will mutate priorValueElements to avoid keeping
// duplicate semantically equal elements. Need the original length to avoid panicks
originalPriorElementsLength := len(priorValueElements)

// Loop through proposed elements by delegating to the recursive semantic
// equality logic. This ensures that recursion will catch a further
// underlying element type has its semantic equality logic checked, even if
Expand All @@ -140,44 +136,33 @@ func ValueSemanticEqualitySetElements(ctx context.Context, req ValueSemanticEqua
// Ensure new value always contains all of proposed new value
newValueElements[idx] = proposedNewValueElement

if idx >= originalPriorElementsLength {
if idx >= len(priorValueElements) {
continue
}

// Loop through all prior value elements and see if there are any semantically equal elements
for pIdx, priorValue := range priorValueElements {
elementReq := ValueSemanticEqualityRequest{
Path: req.Path.AtSetValue(proposedNewValueElement),
PriorValue: priorValue,
ProposedNewValue: proposedNewValueElement,
}
elementResp := &ValueSemanticEqualityResponse{
NewValue: elementReq.ProposedNewValue,
}

ValueSemanticEquality(ctx, elementReq, elementResp)

resp.Diagnostics.Append(elementResp.Diagnostics...)

if resp.Diagnostics.HasError() {
return
}
elementReq := ValueSemanticEqualityRequest{
Path: req.Path.AtSetValue(proposedNewValueElement),
PriorValue: priorValueElements[idx],
ProposedNewValue: proposedNewValueElement,
}
elementResp := &ValueSemanticEqualityResponse{
NewValue: elementReq.ProposedNewValue,
}

if elementResp.NewValue.Equal(elementReq.ProposedNewValue) {
// This prior value element didn't match, but there could be other elements that do
continue
}
ValueSemanticEquality(ctx, elementReq, elementResp)

// Prior state was kept, meaning that we found a semantically equal element
updatedElements = true
resp.Diagnostics.Append(elementResp.Diagnostics...)

// Remove the semantically equal element from the slice of candidates
priorValueElements = append(priorValueElements[:pIdx], priorValueElements[pIdx+1:]...)
if resp.Diagnostics.HasError() {
return
}

// Order doesn't matter, so we can just set the prior state element to this index
newValueElements[idx] = elementResp.NewValue
break
if elementResp.NewValue.Equal(elementReq.ProposedNewValue) {
continue
}

updatedElements = true
newValueElements[idx] = elementResp.NewValue
}

// No changes required if the elements were not updated.
Expand Down

0 comments on commit b37dde1

Please # to comment.