From fc9942baf60c7751a58cdeaf2a53bc6c0b242e4a Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Tue, 29 Oct 2019 14:44:59 -0700 Subject: [PATCH] Move default values to model package --- pkg/config/config.go | 30 +++++++++++++----------------- pkg/config/config_test.go | 4 +++- pkg/model/defaults.go | 13 +++++++++++++ 3 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 pkg/model/defaults.go diff --git a/pkg/config/config.go b/pkg/config/config.go index 00cc80b..47f8aff 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,13 +9,6 @@ import ( "github.com/mxpv/podsync/pkg/model" ) -const ( - DefaultFormat = model.FormatVideo - DefaultQuality = model.QualityHigh - DefaultPageSize = 50 - DefaultUpdatePeriod = 24 * time.Hour -) - // Feed is a configuration for a feed type Feed struct { // URL is a full URL of the field @@ -73,30 +66,33 @@ func LoadConfig(path string) (*Config, error) { return nil, errors.Wrap(err, "failed to load config file") } - // Apply defaults - if config.Server.Hostname == "" { - config.Server.Hostname = "http://localhost" + config.applyDefaults() + + return &config, nil +} + +func (c *Config) applyDefaults() { + if c.Server.Hostname == "" { + c.Server.Hostname = model.DefaultHostname } - for _, feed := range config.Feeds { + for _, feed := range c.Feeds { if feed.UpdatePeriod.Duration == 0 { - feed.UpdatePeriod.Duration = DefaultUpdatePeriod + feed.UpdatePeriod.Duration = model.DefaultUpdatePeriod } if feed.Quality == "" { - feed.Quality = DefaultQuality + feed.Quality = model.DefaultQuality } if feed.Format == "" { - feed.Format = DefaultFormat + feed.Format = model.DefaultFormat } if feed.PageSize == 0 { - feed.PageSize = DefaultPageSize + feed.PageSize = model.DefaultPageSize } } - - return &config, nil } type Duration struct { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 7dbb117..582fe19 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -8,6 +8,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/mxpv/podsync/pkg/model" ) func TestLoadConfig(t *testing.T) { @@ -79,7 +81,7 @@ func TestApplyDefaults(t *testing.T) { feed, ok := config.Feeds["A"] require.True(t, ok) - assert.EqualValues(t, feed.UpdatePeriod, Duration{DefaultUpdatePeriod}) + assert.EqualValues(t, feed.UpdatePeriod, Duration{model.DefaultUpdatePeriod}) assert.EqualValues(t, feed.PageSize, 50) assert.EqualValues(t, feed.Quality, "high") assert.EqualValues(t, feed.Format, "video") diff --git a/pkg/model/defaults.go b/pkg/model/defaults.go new file mode 100644 index 0000000..34bbaf5 --- /dev/null +++ b/pkg/model/defaults.go @@ -0,0 +1,13 @@ +package model + +import ( + "time" +) + +const ( + DefaultHostname = "http://localhost" + DefaultFormat = FormatVideo + DefaultQuality = QualityHigh + DefaultPageSize = 50 + DefaultUpdatePeriod = 24 * time.Hour +)