File tree Expand file tree Collapse file tree 3 files changed +65
-3
lines changed Expand file tree Collapse file tree 3 files changed +65
-3
lines changed Original file line number Diff line number Diff line change 1+ package expand
2+
3+ import (
4+ "sort"
5+ "strings"
6+
7+ "github.com/doublerebel/bellows"
8+ )
9+
10+ func Expand (m map [string ]interface {}) map [string ]interface {} {
11+ return bellows .Expand (removeConflicts (m ))
12+ }
13+
14+ // It is possible for the map to contain conflicts:
15+ // {"a.b": 1, "a": 2}
16+ // What should the result be? We remove the less-specific key.
17+ // {"a.b": 1, "a": 2} -> {"a.b": 1, "a": 2}
18+ func removeConflicts (m map [string ]interface {}) map [string ]interface {} {
19+ var keys []string
20+ n := map [string ]interface {}{}
21+ for k , v := range m {
22+ keys = append (keys , k )
23+ n [k ] = v
24+ }
25+ sort .Strings (keys )
26+ for i := 0 ; i < len (keys )- 1 ; i ++ {
27+ k := keys [i ]
28+ // remove any parent that has a child
29+ if strings .HasPrefix (keys [i + 1 ]+ "." , k ) {
30+ delete (n , k )
31+ }
32+ }
33+ return n
34+ }
Original file line number Diff line number Diff line change 1+ package expand
2+
3+ import (
4+ "fmt"
5+ "testing"
6+
7+ "github.com/stretchr/testify/assert"
8+ )
9+
10+ func TestExpand (t * testing.T ) {
11+ for i := 0 ; i < 100 ; i ++ { // loop 100 times, because map ordering is not determisitic
12+ t .Run (fmt .Sprint (i ), func (t * testing.T ) {
13+ before := map [string ]interface {}{
14+ "a.b" : 1 ,
15+ "a" : 2 ,
16+ "ab" : 3 ,
17+ }
18+ after := Expand (before )
19+ assert .Len (t , before , 3 , "original map unchanged" )
20+ assert .Equal (t , map [string ]interface {}{
21+ "a" : map [string ]interface {}{
22+ "b" : 1 ,
23+ },
24+ "ab" : 3 ,
25+ }, after )
26+ })
27+ }
28+ }
Original file line number Diff line number Diff line change 44 "encoding/json"
55
66 "github.com/Masterminds/sprig"
7-
87 exprpkg "github.com/argoproj/pkg/expr"
9- "github.com/doublerebel/bellows"
8+
9+ "github.com/argoproj/argo-workflows/v3/util/expand"
1010)
1111
1212var sprigFuncMap = sprig .GenericFuncMap () // a singleton for better performance
@@ -17,7 +17,7 @@ func init() {
1717}
1818
1919func GetFuncMap (m map [string ]interface {}) map [string ]interface {} {
20- env := bellows .Expand (m )
20+ env := expand .Expand (m )
2121 for k , v := range exprpkg .GetExprEnvFunctionMap () {
2222 env [k ] = v
2323 }
You can’t perform that action at this time.
0 commit comments