Skip to content

Commit cdbff76

Browse files
committed
Allow duplicate keys in sources
When same key appear multiple times in sources, casper will merge them in array. That way you can construct arrays in the command line.
1 parent 0a0e119 commit cdbff76

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

source/multi.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package source
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
// NewMultiSourcer create source that is a collection of value sources.
68
func NewMultiSourcer(vss ...Getter) (*Source, error) {
79
vars := map[string]interface{}{}
810

911
for _, s := range vss {
1012
for k, v := range s.Get() {
11-
if _, ok := vars[k]; ok {
12-
return nil, fmt.Errorf("duplicated key '%v'", k)
13+
if prev, ok := vars[k]; ok {
14+
switch p := prev.(type) {
15+
case interface{}:
16+
vars[k] = []interface{}{p, v}
17+
case []interface{}:
18+
vars[k] = append(p, v)
19+
default:
20+
return nil, fmt.Errorf("multy sources merging failed")
21+
}
22+
23+
continue
24+
1325
}
1426
vars[k] = v
1527
}

source/multi_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ func TestMultiSourcer(t *testing.T) {
5656
"key1": "var3",
5757
}),
5858
},
59-
nil,
60-
false,
59+
map[string]interface{}{
60+
"key1": []interface{}{
61+
"var1",
62+
"var3",
63+
},
64+
"key2": "var2",
65+
},
66+
true,
6167
},
6268
}
6369

0 commit comments

Comments
 (0)