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

RawEquals: Sets with the same unknowns are equal enough for RawEquals #64

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions cty/value_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,35 +465,27 @@ func (val Value) RawEquals(other Value) bool {
return false

case ty.IsSetType():
s1 := val.v.(set.Set)
s2 := other.v.(set.Set)

// Raw equality for sets is a little tricky because our rules for a
// set of values say that unknown values are never equal. However,
// if both physical sets have the same length and they have all of
// their _known_ values in common, we know that both sets also have
// the same number of unknown values.
if s1.Length() != s2.Length() {
// Convert the set values into a slice so that we can compare each
// value. This is safe because the underlying sets are ordered (see
// setRules in set_internals.go), and so the results are guaranteed to
// be in a consistent order for two equal sets
setList1 := val.AsValueSlice()
setList2 := other.AsValueSlice()

// If both physical sets have the same length and they have all of their
// _known_ values in common, we know that both sets also have the same
// number of unknown values.
if len(setList1) != len(setList2) {
return false
}
for it := s1.Iterator(); it.Next(); {
v := it.Value()
if v == unknown { // "unknown" is the internal representation of unknown-ness
continue
}
if !s2.Has(v) {
return false
}
}
for it := s2.Iterator(); it.Next(); {
v := it.Value()
if v == unknown { // "unknown" is the internal representation of unknown-ness
continue
}
if !s1.Has(v) {

for i := range setList1 {
eq := setList1[i].RawEquals(setList2[i])
if !eq {
return false
}
}

// If we got here without returning false already then our sets are
// equal enough for RawEquals purposes.
return true
Expand Down
Loading