Skip to content

Commit

Permalink
Allow duplicate keys in sources
Browse files Browse the repository at this point in the history
When same key appear multiple times in sources, casper will merge them
in array. That way you can construct arrays in the command line.
  • Loading branch information
VMitov committed Apr 5, 2018
1 parent 0a0e119 commit cdbff76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 15 additions & 3 deletions source/multi.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package source

import "fmt"
import (
"fmt"
)

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

for _, s := range vss {
for k, v := range s.Get() {
if _, ok := vars[k]; ok {
return nil, fmt.Errorf("duplicated key '%v'", k)
if prev, ok := vars[k]; ok {
switch p := prev.(type) {
case interface{}:
vars[k] = []interface{}{p, v}
case []interface{}:
vars[k] = append(p, v)
default:
return nil, fmt.Errorf("multy sources merging failed")
}

continue

}
vars[k] = v
}
Expand Down
10 changes: 8 additions & 2 deletions source/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ func TestMultiSourcer(t *testing.T) {
"key1": "var3",
}),
},
nil,
false,
map[string]interface{}{
"key1": []interface{}{
"var1",
"var3",
},
"key2": "var2",
},
true,
},
}

Expand Down

0 comments on commit cdbff76

Please sign in to comment.