@@ -19,6 +19,7 @@ package customresourcestate
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "math"
22
23
"sort"
23
24
"strconv"
24
25
"strings"
@@ -222,9 +223,39 @@ func (e compiledEach) Values(obj map[string]interface{}) (result []eachValue, er
222
223
addPathLabels (v , e .LabelFromPath , value .Labels )
223
224
result = append (result , * value )
224
225
}
226
+ // return results in a consistent order (simplifies testing)
227
+ sort .Slice (result , func (i , j int ) bool {
228
+ return less (result [i ].Labels , result [j ].Labels )
229
+ })
225
230
return result , errors
226
231
}
227
232
233
+ // less compares two maps of labels by keys and values
234
+ func less (a , b map [string ]string ) bool {
235
+ var aKeys , bKeys sort.StringSlice
236
+ for k := range a {
237
+ aKeys = append (aKeys , k )
238
+ }
239
+ for k := range b {
240
+ bKeys = append (bKeys , k )
241
+ }
242
+ aKeys .Sort ()
243
+ bKeys .Sort ()
244
+ for i := 0 ; i < int (math .Min (float64 (len (aKeys )), float64 (len (bKeys )))); i ++ {
245
+ if aKeys [i ] != bKeys [i ] {
246
+ return aKeys [i ] < bKeys [i ]
247
+ }
248
+
249
+ va := a [aKeys [i ]]
250
+ vb := b [bKeys [i ]]
251
+ if va == vb {
252
+ continue
253
+ }
254
+ return va < vb
255
+ }
256
+ return len (aKeys ) < len (bKeys )
257
+ }
258
+
228
259
func (e compiledEach ) value (it interface {}) (* eachValue , error ) {
229
260
labels := make (map [string ]string )
230
261
value , err := getNum (e .ValueFrom .Get (it ))
0 commit comments