forked from the-anna-project/annad
/
collection.go
135 lines (113 loc) · 4.41 KB
/
collection.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package config
import (
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/the-anna-project/annad/object/config/config"
"github.com/the-anna-project/annad/object/config/endpoint"
"github.com/the-anna-project/annad/object/config/endpoint/metric"
"github.com/the-anna-project/annad/object/config/endpoint/text"
"github.com/the-anna-project/annad/object/config/space"
spaceconnection "github.com/the-anna-project/annad/object/config/space/connection"
"github.com/the-anna-project/annad/object/config/space/dimension"
"github.com/the-anna-project/annad/object/config/space/peer"
"github.com/the-anna-project/annad/object/config/storage"
storageconnection "github.com/the-anna-project/annad/object/config/storage/connection"
"github.com/the-anna-project/annad/object/config/storage/feature"
"github.com/the-anna-project/annad/object/config/storage/general"
)
// NewCollection creates a new config collection. It provides configuration for
// the whole neural network.
func NewCollection() *Collection {
collection := &Collection{}
collection.SetConfig(config.New())
collection.SetEndpointCollection(endpoint.NewCollection())
collection.SetSpaceCollection(space.NewCollection())
collection.SetStorageCollection(storage.NewCollection())
collection.Endpoint().SetMetric(metric.New())
collection.Endpoint().SetText(text.New())
collection.Space().SetConnection(spaceconnection.New())
collection.Space().SetDimension(dimension.New())
collection.Space().SetPeer(peer.New())
collection.Storage().SetConnection(storageconnection.New())
collection.Storage().SetFeature(feature.New())
collection.Storage().SetGeneral(general.New())
return collection
}
// Collection represents the config collection.
type Collection struct {
// Settings.
endpointCollection *endpoint.Collection
config *config.Object
spaceCollection *space.Collection
storageCollection *storage.Collection
}
// Config returns the config file config of the config collection.
func (c *Collection) Config() *config.Object {
return c.config
}
// Endpoint returns the endpoint collection of the config collection.
func (c *Collection) Endpoint() *endpoint.Collection {
return c.endpointCollection
}
// Merge combines values of a flag-set with these of their corresponding
// environment and config file variables, in this order.
func (c *Collection) Merge(flagSet *pflag.FlagSet) error {
v := viper.New()
// Check the defined config file.
v.AddConfigPath(c.Config().Dir())
v.SetConfigName(c.Config().Name())
err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// In case there is no config file given we simply go ahead to check the
// process environment.
} else {
return maskAny(err)
}
}
// We merge the defined flags with their upper case counterparts from the
// environment .
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
v.BindPFlags(flagSet)
flagSet.VisitAll(func(flag *pflag.Flag) {
if flag.Changed {
// The current flag was set via the command line. We definitely want to use
// the set value. Therefore we do not merge anything into it.
return
}
if !v.IsSet(flag.Name) {
// There is neither configuration in the provided config file nor in the
// process environment. That means we cannot use it to merge it into any
// defined flag.
return
}
flag.Value.Set(v.GetString(flag.Name))
})
return nil
}
// SetConfig sets the config file config for the config collection.
func (c *Collection) SetConfig(config *config.Object) {
c.config = config
}
// SetEndpointCollection sets the endpoint collection for the config collection.
func (c *Collection) SetEndpointCollection(endpointCollection *endpoint.Collection) {
c.endpointCollection = endpointCollection
}
// SetSpaceCollection sets the space collection for the config collection.
func (c *Collection) SetSpaceCollection(spaceCollection *space.Collection) {
c.spaceCollection = spaceCollection
}
// SetStorageCollection sets the storage collection for the config collection.
func (c *Collection) SetStorageCollection(storageCollection *storage.Collection) {
c.storageCollection = storageCollection
}
// Space returns the space collection of the config collection.
func (c *Collection) Space() *space.Collection {
return c.spaceCollection
}
// Storage returns the storage collection of the config collection.
func (c *Collection) Storage() *storage.Collection {
return c.storageCollection
}