diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a5da6..b86cac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Removed - Removed mocks package. [#9](https://github.com/go-nacelle/config/pull/9) +- Removed `MustLoad` from `Config` interface. [#11](https://github.com/go-nacelle/config/pull/11) ### Changed diff --git a/config.go b/config.go index 34d6c68..cd3bb68 100644 --- a/config.go +++ b/config.go @@ -22,9 +22,6 @@ type Config interface { // the PostLoadConfig interface. PostLoad(interface{}) error - // MustInject calls Injects and panics on error. - MustLoad(interface{}, ...TagModifier) - // Assets returns a list of names of assets that compose the // underlying sourcer. This can be a list of matched files that are // read, or a token that denotes a fixed source. @@ -107,13 +104,6 @@ func (c *config) PostLoad(target interface{}) error { return nil } -// MustLoad calls Load and panics on error. -func (c *config) MustLoad(target interface{}, modifiers ...TagModifier) { - if err := c.Load(target, modifiers...); err != nil { - panic(err.Error()) - } -} - func (c *config) Assets() []string { return c.sourcer.Assets() } diff --git a/logging_config.go b/logging_config.go index cc0e069..a06a677 100644 --- a/logging_config.go +++ b/logging_config.go @@ -47,12 +47,6 @@ func (c *loggingConfig) Load(target interface{}, modifiers ...TagModifier) error return nil } -func (c *loggingConfig) MustLoad(target interface{}, modifiers ...TagModifier) { - if err := c.Load(target, modifiers...); err != nil { - panic(err.Error()) - } -} - func (c *loggingConfig) dumpSource() error { chunk := map[string]interface{}{} for key, value := range c.Config.Dump() { diff --git a/logging_config_test.go b/logging_config_test.go index b58ddf2..b278294 100644 --- a/logging_config_test.go +++ b/logging_config_test.go @@ -66,19 +66,3 @@ func TestLoggingConfigBadMaskTag(t *testing.T) { assert.EqualError(t, lc.Load(chunk), "failed to serialize config (field 'X' has an invalid mask tag)") } - -func TestLoggingConfigMustLoadLogs(t *testing.T) { - type C struct { - X string `env:"x"` - Y int `env:"y"` - Z []string `env:"w" display:"Q"` - } - - config := NewMockConfig() - logger := NewMockLogger() - lc := NewLoggingConfig(config, logger, nil) - chunk := &C{} - - lc.MustLoad(chunk) - mockassert.CalledOnce(t, logger.PrintfFunc) -} diff --git a/mock_config_test.go b/mock_config_test.go index b77ea9b..b6667aa 100644 --- a/mock_config_test.go +++ b/mock_config_test.go @@ -22,9 +22,6 @@ type MockConfig struct { // LoadFunc is an instance of a mock function object controlling the // behavior of the method Load. LoadFunc *ConfigLoadFunc - // MustLoadFunc is an instance of a mock function object controlling the - // behavior of the method MustLoad. - MustLoadFunc *ConfigMustLoadFunc // PostLoadFunc is an instance of a mock function object controlling the // behavior of the method PostLoad. PostLoadFunc *ConfigPostLoadFunc @@ -59,11 +56,6 @@ func NewMockConfig() *MockConfig { return nil }, }, - MustLoadFunc: &ConfigMustLoadFunc{ - defaultHook: func(interface{}, ...TagModifier) { - return - }, - }, PostLoadFunc: &ConfigPostLoadFunc{ defaultHook: func(interface{}) error { return nil @@ -91,9 +83,6 @@ func NewMockConfigFrom(i Config) *MockConfig { LoadFunc: &ConfigLoadFunc{ defaultHook: i.Load, }, - MustLoadFunc: &ConfigMustLoadFunc{ - defaultHook: i.MustLoad, - }, PostLoadFunc: &ConfigPostLoadFunc{ defaultHook: i.PostLoad, }, @@ -624,115 +613,6 @@ func (c ConfigLoadFuncCall) Results() []interface{} { return []interface{}{c.Result0} } -// ConfigMustLoadFunc describes the behavior when the MustLoad method of the -// parent MockConfig instance is invoked. -type ConfigMustLoadFunc struct { - defaultHook func(interface{}, ...TagModifier) - hooks []func(interface{}, ...TagModifier) - history []ConfigMustLoadFuncCall - mutex sync.Mutex -} - -// MustLoad delegates to the next hook function in the queue and stores the -// parameter and result values of this invocation. -func (m *MockConfig) MustLoad(v0 interface{}, v1 ...TagModifier) { - m.MustLoadFunc.nextHook()(v0, v1...) - m.MustLoadFunc.appendCall(ConfigMustLoadFuncCall{v0, v1}) - return -} - -// SetDefaultHook sets function that is called when the MustLoad method of -// the parent MockConfig instance is invoked and the hook queue is empty. -func (f *ConfigMustLoadFunc) SetDefaultHook(hook func(interface{}, ...TagModifier)) { - f.defaultHook = hook -} - -// PushHook adds a function to the end of hook queue. Each invocation of the -// MustLoad method of the parent MockConfig instance invokes the hook at the -// front of the queue and discards it. After the queue is empty, the default -// hook function is invoked for any future action. -func (f *ConfigMustLoadFunc) PushHook(hook func(interface{}, ...TagModifier)) { - f.mutex.Lock() - f.hooks = append(f.hooks, hook) - f.mutex.Unlock() -} - -// SetDefaultReturn calls SetDefaultDefaultHook with a function that returns -// the given values. -func (f *ConfigMustLoadFunc) SetDefaultReturn() { - f.SetDefaultHook(func(interface{}, ...TagModifier) { - return - }) -} - -// PushReturn calls PushDefaultHook with a function that returns the given -// values. -func (f *ConfigMustLoadFunc) PushReturn() { - f.PushHook(func(interface{}, ...TagModifier) { - return - }) -} - -func (f *ConfigMustLoadFunc) nextHook() func(interface{}, ...TagModifier) { - f.mutex.Lock() - defer f.mutex.Unlock() - - if len(f.hooks) == 0 { - return f.defaultHook - } - - hook := f.hooks[0] - f.hooks = f.hooks[1:] - return hook -} - -func (f *ConfigMustLoadFunc) appendCall(r0 ConfigMustLoadFuncCall) { - f.mutex.Lock() - f.history = append(f.history, r0) - f.mutex.Unlock() -} - -// History returns a sequence of ConfigMustLoadFuncCall objects describing -// the invocations of this function. -func (f *ConfigMustLoadFunc) History() []ConfigMustLoadFuncCall { - f.mutex.Lock() - history := make([]ConfigMustLoadFuncCall, len(f.history)) - copy(history, f.history) - f.mutex.Unlock() - - return history -} - -// ConfigMustLoadFuncCall is an object that describes an invocation of -// method MustLoad on an instance of MockConfig. -type ConfigMustLoadFuncCall struct { - // Arg0 is the value of the 1st argument passed to this method - // invocation. - Arg0 interface{} - // Arg1 is a slice containing the values of the variadic arguments - // passed to this method invocation. - Arg1 []TagModifier -} - -// Args returns an interface slice containing the arguments of this -// invocation. The variadic slice argument is flattened in this array such -// that one positional argument and three variadic arguments would result in -// a slice of four, not two. -func (c ConfigMustLoadFuncCall) Args() []interface{} { - trailing := []interface{}{} - for _, val := range c.Arg1 { - trailing = append(trailing, val) - } - - return append([]interface{}{c.Arg0}, trailing...) -} - -// Results returns an interface slice containing the results of this -// invocation. -func (c ConfigMustLoadFuncCall) Results() []interface{} { - return []interface{}{} -} - // ConfigPostLoadFunc describes the behavior when the PostLoad method of the // parent MockConfig instance is invoked. type ConfigPostLoadFunc struct {