From 9e099d15bd31f80751b9b2ac38c31bf130528329 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 2 Jul 2023 20:10:38 +0300 Subject: refactor config to use functional options pattern --- internal/config/config.go | 46 +++++++++++++++++++++++++++++++++++++----- internal/config/config_test.go | 6 +++--- 2 files changed, 44 insertions(+), 8 deletions(-) (limited to 'internal/config') diff --git a/internal/config/config.go b/internal/config/config.go index cb144f3..988315a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -24,7 +24,7 @@ type Config struct { SMTPServer string `json:"SMTPServer,omitempty"` } -func New(configFile string) (Config, error) { +func NewFromConfigFile(configFile string) (Config, error) { var conf Config file, err := os.Open(configFile) @@ -43,14 +43,50 @@ func New(configFile string) (Config, error) { return conf, err } - return NewFromConfig(conf) + return conf.setDefaults() } -func NewFromConfig(conf Config) (Config, error) { - return conf.initialize() +func New(options ...func(*Config)) (Config, error) { + var conf Config + + for _, opt := range options { + opt(&conf) + } + + return conf.setDefaults() +} + +func WithStateDir(stateDir string) func(*Config) { + return func(conf *Config) { + conf.StateDir = stateDir + } +} + +func WithAddress(address string) func(*Config) { + return func(conf *Config) { + conf.Address = address + } +} + +func WithNodes(nodes ...string) func(*Config) { + return func(conf *Config) { + conf.Nodes = append(conf.Nodes, nodes...) + } +} + +func WithMyID(id string) func(*Config) { + return func(conf *Config) { + conf.MyID = id + } +} + +func WithRelaxedMode() func(*Config) { + return func(conf *Config) { + conf.RelaxedMode = true + } } -func (conf Config) initialize() (Config, error) { +func (conf Config) setDefaults() (Config, error) { if conf.LoopIntervalS == 0 { conf.LoopIntervalS = 10 } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 96d4998..e24c048 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -8,7 +8,7 @@ import ( func TestNodeNumber(t *testing.T) { t.Parallel() - conf, _ := NewFromConfig(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := New(WithNodes("localhost:1234", "hamburger:4321")) num := conf.NodeNumber("localhost") if num != 0 { @@ -23,7 +23,7 @@ func TestNodeNumber(t *testing.T) { func TestIsNode(t *testing.T) { t.Parallel() - conf, _ := NewFromConfig(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := New(WithNodes("localhost:1234", "hamburger:4321")) remoteAddr := "localhost:323232" if !conf.IsNode(remoteAddr) { @@ -38,7 +38,7 @@ func TestIsNode(t *testing.T) { func TestIsNodeWithLookup(t *testing.T) { t.Parallel() - conf, _ := NewFromConfig(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := New(WithNodes("localhost:1234", "hamburger:4321")) lookupIP := func(addr string) ([]net.IP, error) { switch addr { -- cgit v1.2.3