From 9b82da72ca26eaecc77c0697aa73c6ac9c68a729 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 28 Feb 2024 09:14:11 -0500 Subject: [PATCH] refactor: move redis to its own file and add in defaults --- config/cluster.go | 14 -------------- config/redis.go | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 config/redis.go diff --git a/config/cluster.go b/config/cluster.go index 3e590c6..8b8455f 100644 --- a/config/cluster.go +++ b/config/cluster.go @@ -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{}) { diff --git a/config/redis.go b/config/redis.go new file mode 100644 index 0000000..2531aef --- /dev/null +++ b/config/redis.go @@ -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 +}