diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty/cty/set')
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/set/iterator.go | 31 | ||||
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/set/ops.go | 51 | ||||
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/set/rules.go | 18 |
3 files changed, 54 insertions, 46 deletions
diff --git a/vendor/github.com/zclconf/go-cty/cty/set/iterator.go b/vendor/github.com/zclconf/go-cty/cty/set/iterator.go index f15498e..4a60494 100644 --- a/vendor/github.com/zclconf/go-cty/cty/set/iterator.go +++ b/vendor/github.com/zclconf/go-cty/cty/set/iterator.go | |||
@@ -1,36 +1,15 @@ | |||
1 | package set | 1 | package set |
2 | 2 | ||
3 | type Iterator struct { | 3 | type Iterator struct { |
4 | bucketIds []int | 4 | vals []interface{} |
5 | vals map[int][]interface{} | 5 | idx int |
6 | bucketIdx int | ||
7 | valIdx int | ||
8 | } | 6 | } |
9 | 7 | ||
10 | func (it *Iterator) Value() interface{} { | 8 | func (it *Iterator) Value() interface{} { |
11 | return it.currentBucket()[it.valIdx] | 9 | return it.vals[it.idx] |
12 | } | 10 | } |
13 | 11 | ||
14 | func (it *Iterator) Next() bool { | 12 | func (it *Iterator) Next() bool { |
15 | if it.bucketIdx == -1 { | 13 | it.idx++ |
16 | // init | 14 | return it.idx < len(it.vals) |
17 | if len(it.bucketIds) == 0 { | ||
18 | return false | ||
19 | } | ||
20 | |||
21 | it.valIdx = 0 | ||
22 | it.bucketIdx = 0 | ||
23 | return true | ||
24 | } | ||
25 | |||
26 | it.valIdx++ | ||
27 | if it.valIdx >= len(it.currentBucket()) { | ||
28 | it.valIdx = 0 | ||
29 | it.bucketIdx++ | ||
30 | } | ||
31 | return it.bucketIdx < len(it.bucketIds) | ||
32 | } | ||
33 | |||
34 | func (it *Iterator) currentBucket() []interface{} { | ||
35 | return it.vals[it.bucketIds[it.bucketIdx]] | ||
36 | } | 15 | } |
diff --git a/vendor/github.com/zclconf/go-cty/cty/set/ops.go b/vendor/github.com/zclconf/go-cty/cty/set/ops.go index 726e707..fd1555f 100644 --- a/vendor/github.com/zclconf/go-cty/cty/set/ops.go +++ b/vendor/github.com/zclconf/go-cty/cty/set/ops.go | |||
@@ -75,8 +75,12 @@ func (s Set) Copy() Set { | |||
75 | return ret | 75 | return ret |
76 | } | 76 | } |
77 | 77 | ||
78 | // Iterator returns an iterator over values in the set, in an undefined order | 78 | // Iterator returns an iterator over values in the set. If the set's rules |
79 | // that callers should not depend on. | 79 | // implement OrderedRules then the result is ordered per those rules. If |
80 | // no order is provided, or if it is not a total order, then the iteration | ||
81 | // order is undefined but consistent for a particular version of cty. Do not | ||
82 | // rely on specific ordering between cty releases unless the rules order is a | ||
83 | // total order. | ||
80 | // | 84 | // |
81 | // The pattern for using the returned iterator is: | 85 | // The pattern for using the returned iterator is: |
82 | // | 86 | // |
@@ -89,18 +93,11 @@ func (s Set) Copy() Set { | |||
89 | // Once an iterator has been created for a set, the set *must not* be mutated | 93 | // Once an iterator has been created for a set, the set *must not* be mutated |
90 | // until the iterator is no longer in use. | 94 | // until the iterator is no longer in use. |
91 | func (s Set) Iterator() *Iterator { | 95 | func (s Set) Iterator() *Iterator { |
92 | // Sort the bucketIds to ensure that we always traverse in a | 96 | vals := s.Values() |
93 | // consistent order. | ||
94 | bucketIds := make([]int, 0, len(s.vals)) | ||
95 | for id := range s.vals { | ||
96 | bucketIds = append(bucketIds, id) | ||
97 | } | ||
98 | sort.Ints(bucketIds) | ||
99 | 97 | ||
100 | return &Iterator{ | 98 | return &Iterator{ |
101 | bucketIds: bucketIds, | 99 | vals: vals, |
102 | vals: s.vals, | 100 | idx: -1, |
103 | bucketIdx: -1, | ||
104 | } | 101 | } |
105 | } | 102 | } |
106 | 103 | ||
@@ -113,16 +110,30 @@ func (s Set) EachValue(cb func(interface{})) { | |||
113 | } | 110 | } |
114 | } | 111 | } |
115 | 112 | ||
116 | // Values returns a slice of all of the values in the set in no particular | 113 | // Values returns a slice of all the values in the set. If the set rules have |
117 | // order. This is just a wrapper around EachValue that accumulates the results | 114 | // an order then the result is in that order. If no order is provided or if |
118 | // in a slice for caller convenience. | 115 | // it is not a total order then the result order is undefined, but consistent |
119 | // | 116 | // for a particular set value within a specific release of cty. |
120 | // The returned slice will be nil if there are no values in the set. | ||
121 | func (s Set) Values() []interface{} { | 117 | func (s Set) Values() []interface{} { |
122 | var ret []interface{} | 118 | var ret []interface{} |
123 | s.EachValue(func(v interface{}) { | 119 | // Sort the bucketIds to ensure that we always traverse in a |
124 | ret = append(ret, v) | 120 | // consistent order. |
125 | }) | 121 | bucketIDs := make([]int, 0, len(s.vals)) |
122 | for id := range s.vals { | ||
123 | bucketIDs = append(bucketIDs, id) | ||
124 | } | ||
125 | sort.Ints(bucketIDs) | ||
126 | |||
127 | for _, bucketID := range bucketIDs { | ||
128 | ret = append(ret, s.vals[bucketID]...) | ||
129 | } | ||
130 | |||
131 | if orderRules, ok := s.rules.(OrderedRules); ok { | ||
132 | sort.SliceStable(ret, func(i, j int) bool { | ||
133 | return orderRules.Less(ret[i], ret[j]) | ||
134 | }) | ||
135 | } | ||
136 | |||
126 | return ret | 137 | return ret |
127 | } | 138 | } |
128 | 139 | ||
diff --git a/vendor/github.com/zclconf/go-cty/cty/set/rules.go b/vendor/github.com/zclconf/go-cty/cty/set/rules.go index 7200184..51f744b 100644 --- a/vendor/github.com/zclconf/go-cty/cty/set/rules.go +++ b/vendor/github.com/zclconf/go-cty/cty/set/rules.go | |||
@@ -23,3 +23,21 @@ type Rules interface { | |||
23 | // be equivalent. | 23 | // be equivalent. |
24 | Equivalent(interface{}, interface{}) bool | 24 | Equivalent(interface{}, interface{}) bool |
25 | } | 25 | } |
26 | |||
27 | // OrderedRules is an extension of Rules that can apply a partial order to | ||
28 | // element values. When a set's Rules implements OrderedRules an iterator | ||
29 | // over the set will return items in the order described by the rules. | ||
30 | // | ||
31 | // If the given order is not a total order (that is, some pairs of non-equivalent | ||
32 | // elements do not have a defined order) then the resulting iteration order | ||
33 | // is undefined but consistent for a particular version of cty. The exact | ||
34 | // order in that case is not part of the contract and is subject to change | ||
35 | // between versions. | ||
36 | type OrderedRules interface { | ||
37 | Rules | ||
38 | |||
39 | // Less returns true if and only if the first argument should sort before | ||
40 | // the second argument. If the second argument should sort before the first | ||
41 | // or if there is no defined order for the values, return false. | ||
42 | Less(interface{}, interface{}) bool | ||
43 | } | ||