refactor: move redis to its own file and add in defaults

This commit is contained in:
Derrick Hammer 2024-02-28 09:14:11 -05:00
parent 960c2b01d9
commit 9b82da72ca
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 26 additions and 14 deletions

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"errors"
"reflect" "reflect"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -13,19 +12,6 @@ type ClusterConfig struct {
Etcd *EtcdConfig `mapstructure:"etcd"` 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 { func clusterConfigHook() mapstructure.DecodeHookFuncType {
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) { return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.Map || t != reflect.TypeOf(&ClusterConfig{}) { if f.Kind() != reflect.Map || t != reflect.TypeOf(&ClusterConfig{}) {

26
config/redis.go Normal file
View File

@ -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
}