diff options
author | Valery Piashchynski <[email protected]> | 2020-12-26 00:40:31 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-26 00:40:31 +0300 |
commit | 7a0dee1a416705c621edbf50e1f43fb39845348f (patch) | |
tree | 0007a6b8c8ac9e7d31b8a5f3f7f27669c860d261 /plugins/config | |
parent | 8526c03822e724bc2ebb64b6197085fea335b782 (diff) |
Huge tests refactoring. Reduce running time 2-3x times
Diffstat (limited to 'plugins/config')
-rw-r--r-- | plugins/config/interface.go | 22 | ||||
-rwxr-xr-x | plugins/config/plugin.go | 75 |
2 files changed, 97 insertions, 0 deletions
diff --git a/plugins/config/interface.go b/plugins/config/interface.go new file mode 100644 index 00000000..2a7c67ce --- /dev/null +++ b/plugins/config/interface.go @@ -0,0 +1,22 @@ +package config + +type Configurer interface { + // UnmarshalKey reads configuration section into configuration object. + // + // func (h *HttpService) Init(cp config.Configurer) error { + // h.config := &HttpConfig{} + // if err := configProvider.UnmarshalKey("http", h.config); err != nil { + // return err + // } + // } + UnmarshalKey(name string, out interface{}) error + + // Get used to get config section + Get(name string) interface{} + + // Overwrite used to overwrite particular values in the unmarshalled config + Overwrite(values map[string]interface{}) error + + // Has checks if config section exists. + Has(name string) bool +} diff --git a/plugins/config/plugin.go b/plugins/config/plugin.go new file mode 100755 index 00000000..1a170448 --- /dev/null +++ b/plugins/config/plugin.go @@ -0,0 +1,75 @@ +package config + +import ( + "bytes" + "strings" + + "github.com/spf13/viper" + "github.com/spiral/errors" +) + +type Viper struct { + viper *viper.Viper + Path string + Prefix string + Type string + ReadInCfg []byte +} + +// Inits config provider. +func (v *Viper) Init() error { + const op = errors.Op("viper plugin init") + v.viper = viper.New() + // If user provided []byte data with config, read it and ignore Path and Prefix + if v.ReadInCfg != nil && v.Type != "" { + v.viper.SetConfigType("yaml") + return v.viper.ReadConfig(bytes.NewBuffer(v.ReadInCfg)) + } + + // read in environment variables that match + v.viper.AutomaticEnv() + if v.Prefix == "" { + return errors.E(op, errors.Str("prefix should be set")) + } + + v.viper.SetEnvPrefix(v.Prefix) + if v.Path == "" { + return errors.E(op, errors.Str("path should be set")) + } + + v.viper.SetConfigFile(v.Path) + v.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + + return v.viper.ReadInConfig() +} + +// Overwrite overwrites existing config with provided values +func (v *Viper) Overwrite(values map[string]interface{}) error { + if len(values) != 0 { + for key, value := range values { + v.viper.Set(key, value) + } + } + + return nil +} + +// UnmarshalKey reads configuration section into configuration object. +func (v *Viper) UnmarshalKey(name string, out interface{}) error { + const op = errors.Op("unmarshal key") + err := v.viper.UnmarshalKey(name, &out) + if err != nil { + return errors.E(op, err) + } + return nil +} + +// Get raw config in a form of config section. +func (v *Viper) Get(name string) interface{} { + return v.viper.Get(name) +} + +// Has checks if config section exists. +func (v *Viper) Has(name string) bool { + return v.viper.IsSet(name) +} |