diff options
| author | Paul Buetow <paul@buetow.org> | 2023-07-02 20:10:38 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-07-02 20:10:38 +0300 |
| commit | 9e099d15bd31f80751b9b2ac38c31bf130528329 (patch) | |
| tree | 118d42839d53a237ea64658229ab9c9762c4f35a | |
| parent | 5978480b49c152e458055bb3a6b1b4ba9afa54e9 (diff) | |
refactor config to use functional options pattern
| -rw-r--r-- | internal/config/config.go | 46 | ||||
| -rw-r--r-- | internal/config/config_test.go | 6 | ||||
| -rw-r--r-- | internal/quorum/quorum_test.go | 20 | ||||
| -rw-r--r-- | internal/run.go | 2 | ||||
| -rw-r--r-- | internal/vote/vote_test.go | 6 |
5 files changed, 53 insertions, 27 deletions
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 { diff --git a/internal/quorum/quorum_test.go b/internal/quorum/quorum_test.go index 3ae87c6..5bf6834 100644 --- a/internal/quorum/quorum_test.go +++ b/internal/quorum/quorum_test.go @@ -13,9 +13,7 @@ var inOneHour = time.Now().Add(1 * time.Hour) func TestScore(t *testing.T) { t.Parallel() - conf, _ := config.NewFromConfig( - config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}}, - ) + conf, _ := config.New(config.WithNodes("foo:1234", "bar:4321", "baz:3444")) quo := New(conf) vote1, _ := vote.New(conf, []string{"foo", "bar"}) @@ -69,9 +67,7 @@ func TestTieScore(t *testing.T) { t.Run("First tie score test", func(t *testing.T) { // If it is a tie, the first particpant (here: "foo") will win. - conf, _ := config.NewFromConfig( - config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}}, - ) + conf, _ := config.New(config.WithNodes("foo:1234", "bar:4321", "baz:3444")) quo := New(conf) addVotes(conf, quo) @@ -92,9 +88,7 @@ func TestTieScore(t *testing.T) { t.Run("Second tie score test", func(t *testing.T) { // If it is a tie, the first particpant (here: "bar") will win. - conf, _ := config.NewFromConfig( - config.Config{Nodes: []string{"bar:1234", "foo:4321", "baz:3444"}}, - ) + conf, _ := config.New(config.WithNodes("bar:1234", "foo:4321", "baz:3444")) quo := New(conf) addVotes(conf, quo) @@ -115,9 +109,7 @@ func TestTieScore(t *testing.T) { } func TestExpire(t *testing.T) { - conf, _ := config.NewFromConfig( - config.Config{Nodes: []string{"foo:1234", "bar:4321", "bay:2212"}}, - ) + conf, _ := config.New(config.WithNodes("foo:1234", "bar:4321", "bay:2212")) quo := New(conf) vote1, _ := vote.New(conf, []string{"bar", "baz", "bay"}) @@ -149,9 +141,7 @@ func TestExpire(t *testing.T) { } func TestLiveNodes(t *testing.T) { - conf, _ := config.NewFromConfig( - config.Config{Nodes: []string{"foo:1234", "bay:4321"}}, - ) + conf, _ := config.New(config.WithNodes("foo:1234", "bay:4321")) quo := New(conf) vote1, _ := vote.New(conf, []string{"bar", "baz", "bay"}) diff --git a/internal/run.go b/internal/run.go index 961df14..7a51f6c 100644 --- a/internal/run.go +++ b/internal/run.go @@ -10,7 +10,7 @@ import ( ) func Run(ctx context.Context, configFile string, loopIntervalS int64) error { - conf, err := config.New(configFile) + conf, err := config.NewFromConfigFile(configFile) if err != nil { return err } diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go index a546a2b..f1221b2 100644 --- a/internal/vote/vote_test.go +++ b/internal/vote/vote_test.go @@ -10,7 +10,7 @@ import ( func TestVote(t *testing.T) { t.Parallel() - conf, _ := config.NewFromConfig(config.Config{MyID: "foo.zone"}) + conf, _ := config.New(config.WithMyID("foo.zone")) v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) if v.FromID != "foo.zone" { @@ -33,7 +33,7 @@ func TestVote(t *testing.T) { func TestVoteExpiry(t *testing.T) { t.Parallel() - conf, _ := config.NewFromConfig(config.Config{MyID: "foo.zone"}) + conf, _ := config.New(config.WithMyID("foo.zone")) v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) // Set expiry 1h into the future @@ -52,7 +52,7 @@ func TestVoteExpiry(t *testing.T) { func TestMarshalling(t *testing.T) { t.Parallel() - conf, _ := config.NewFromConfig(config.Config{MyID: "foo.zone"}) + conf, _ := config.New(config.WithMyID("foo.zone")) v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) jsonStr, err := v.ToJSON() |
