refactor: move redis to its own file and add in defaults
This commit is contained in:
parent
960c2b01d9
commit
9b82da72ca
|
@ -1,7 +1,6 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
@ -13,19 +12,6 @@ type ClusterConfig struct {
|
|||
Etcd *EtcdConfig `mapstructure:"etcd"`
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Address string `mapstructure:"address"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
}
|
||||
|
||||
func (r *RedisConfig) Validate() error {
|
||||
if r.Address == "" {
|
||||
return errors.New("address is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func clusterConfigHook() mapstructure.DecodeHookFuncType {
|
||||
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
|
||||
if f.Kind() != reflect.Map || t != reflect.TypeOf(&ClusterConfig{}) {
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package config
|
||||
|
||||
import "errors"
|
||||
|
||||
var _ Validator = (*RedisConfig)(nil)
|
||||
var _ Defaults = (*RedisConfig)(nil)
|
||||
|
||||
type RedisConfig struct {
|
||||
Address string `mapstructure:"address"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
}
|
||||
|
||||
func (r *RedisConfig) Defaults() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"address": "localhost:6379",
|
||||
"db": 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RedisConfig) Validate() error {
|
||||
if r.Address == "" {
|
||||
return errors.New("address is required")
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue