mirror of https://github.com/hashicorp/terraform
cty now guarantees that sets of primitive values will iterate in a reasonable order. Previously it was the caller's responsibility to deal with that, but we invariably neglected to do so, causing inconsistent ordering. Since cty prioritizes consistent behavior over performance, it now imposes its own sort on set elements as part of iterating over them so that calling applications don't have to worry so much about it. This change also causes cty to consistently push unknown and null values in sets to the end of iteration, where before that was undefined. This means that our diff output will now consistently list additions before removals when showing sets, rather than the ordering being undefined as before. The ordering of known, non-null, non-primitive values is still not contractually fixed but remains consistent for a particular version of cty.pull/21169/head
parent
4b83ddb025
commit
6adcc7ab73
@ -1,36 +1,15 @@
|
||||
package set
|
||||
|
||||
type Iterator struct {
|
||||
bucketIds []int
|
||||
vals map[int][]interface{}
|
||||
bucketIdx int
|
||||
valIdx int
|
||||
vals []interface{}
|
||||
idx int
|
||||
}
|
||||
|
||||
func (it *Iterator) Value() interface{} {
|
||||
return it.currentBucket()[it.valIdx]
|
||||
return it.vals[it.idx]
|
||||
}
|
||||
|
||||
func (it *Iterator) Next() bool {
|
||||
if it.bucketIdx == -1 {
|
||||
// init
|
||||
if len(it.bucketIds) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
it.valIdx = 0
|
||||
it.bucketIdx = 0
|
||||
return true
|
||||
}
|
||||
|
||||
it.valIdx++
|
||||
if it.valIdx >= len(it.currentBucket()) {
|
||||
it.valIdx = 0
|
||||
it.bucketIdx++
|
||||
}
|
||||
return it.bucketIdx < len(it.bucketIds)
|
||||
}
|
||||
|
||||
func (it *Iterator) currentBucket() []interface{} {
|
||||
return it.vals[it.bucketIds[it.bucketIdx]]
|
||||
it.idx++
|
||||
return it.idx < len(it.vals)
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue