diff options
| author | Paul Buetow <paul@buetow.org> | 2023-06-29 09:28:35 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-06-29 09:28:35 +0300 |
| commit | 16602d176cb82da0f9980b95ac0ef6eec47953a2 (patch) | |
| tree | 26ba82dc33a61b6de77e866d29cd719c31668ac1 /internal | |
| parent | 84e5a4ae93eed6397a6cbf6091a0f5b3eacc78e2 (diff) | |
refactor config initialization
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/config/config.go | 18 | ||||
| -rw-r--r-- | internal/config/config_test.go | 6 | ||||
| -rw-r--r-- | internal/quorum/quorum_test.go | 10 | ||||
| -rw-r--r-- | internal/vote/vote_test.go | 6 |
4 files changed, 16 insertions, 24 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index ec4ea67..2f8c62b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -21,19 +21,7 @@ type Config struct { nodeNumberCache map[string]int } -func New(arg any) (Config, error) { - switch arg := arg.(type) { - case string: - return newFromConfigFile(arg) - case Config: - // Used to initialize a custom config from unit tests. - return arg.initialize() - default: - panic("unable to initialize config") - } -} - -func newFromConfigFile(configFile string) (Config, error) { +func New(configFile string) (Config, error) { var conf Config file, err := os.Open(configFile) @@ -52,6 +40,10 @@ func newFromConfigFile(configFile string) (Config, error) { return conf, err } + return NewFromConfig(conf) +} + +func NewFromConfig(conf Config) (Config, error) { return conf.initialize() } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index af403ec..96d4998 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, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := NewFromConfig(Config{Nodes: []string{"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, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := NewFromConfig(Config{Nodes: []string{"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, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := NewFromConfig(Config{Nodes: []string{"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 b88b4fb..fd51fb6 100644 --- a/internal/quorum/quorum_test.go +++ b/internal/quorum/quorum_test.go @@ -13,7 +13,7 @@ var inOneHour = time.Now().Add(1 * time.Hour) func TestScore(t *testing.T) { t.Parallel() - conf, _ := config.New( + conf, _ := config.NewFromConfig( config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}}, ) quo := New(conf) @@ -69,7 +69,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.New( + conf, _ := config.NewFromConfig( config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}}, ) quo := New(conf) @@ -92,7 +92,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.New( + conf, _ := config.NewFromConfig( config.Config{Nodes: []string{"bar:1234", "foo:4321", "baz:3444"}}, ) quo := New(conf) @@ -115,7 +115,7 @@ func TestTieScore(t *testing.T) { } func TestExpire(t *testing.T) { - conf, _ := config.New( + conf, _ := config.NewFromConfig( config.Config{Nodes: []string{"foo:1234", "bar:4321", "bay:2212"}}, ) quo := New(conf) @@ -149,7 +149,7 @@ func TestExpire(t *testing.T) { } func TestLiveNodes(t *testing.T) { - conf, _ := config.New( + conf, _ := config.NewFromConfig( config.Config{Nodes: []string{"foo:1234", "bay:4321"}}, ) quo := New(conf) diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go index 25860c7..a546a2b 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.New(config.Config{MyID: "foo.zone"}) + conf, _ := config.NewFromConfig(config.Config{MyID: "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.New(config.Config{MyID: "foo.zone"}) + conf, _ := config.NewFromConfig(config.Config{MyID: "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.New(config.Config{MyID: "foo.zone"}) + conf, _ := config.NewFromConfig(config.Config{MyID: "foo.zone"}) v, _ := New(conf, []string{"foo", "bar", "baz", "bay"}) jsonStr, err := v.ToJSON() |
