/
composite.go
47 lines (37 loc) · 1.1 KB
/
composite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package sources
import (
"fmt"
. "github.com/ahmetalpbalkan/go-linq"
"github.com/zeroturnaround/configo/flatmap"
)
type CompositeSource struct {
Sources []map[string]interface{} `json:"sources"`
}
func (compositeSource *CompositeSource) Get() (map[string]interface{}, error) {
resultEnv := make(map[string]interface{})
_, err := From(compositeSource.Sources).
Select(func(rawSource T) (T, error) {
loader, err := GetSource(rawSource.(map[string]interface{}))
if err != nil {
return nil, fmt.Errorf("Failed to parse source: %s", err)
}
return loader, nil
}).
// Resolve in parallel because some sources might use IO and will take some time
AsParallel().AsOrdered().
Select(func(loader T) (T, error) {
result, err := loader.(Source).Get()
if err != nil {
return nil, fmt.Errorf("Failed to resolve source: %s", err)
}
return result, nil
}).
AsSequential().
CountBy(func(partialConfig T) (bool, error) {
for key, value := range flatmap.Flatten(partialConfig.(map[string]interface{})) {
resultEnv[key] = value
}
return true, nil
})
return resultEnv, err
}