gas/config
config/configtest
import "github.com/gasmod/gas/config/configtest"Package configtest holds MockConfig, a config.ConfigProvider for tests of code that reads configuration, and StaticProvider, a config.Provider that loads fixed values.
A MockConfig records each call, and calls the function in the field named after the method, when it is set:
m := &configtest.MockConfig{ GetFn: func(key string) any { return "db" },}svc := NewService(m)// m.CallCount("Get") is how many times svc called GetNewMockConfigWithValues sets every field to the methods of a real config.Config holding the given values, so Get, Find, Bind and the rest behave as they do in an app:
m, err := configtest.NewMockConfigWithValues(map[string]any{ "database": map[string]any{"host": "db"},})Each method appends a Call with its name and arguments to Calls, then calls its field. With the field nil, it returns zero values: nil, false, or a nil error. LoadProviderContext records the provider but not the context.
The methods may be called from several goroutines at once, as Calls is appended to under a lock. MockConfig.CallCount and MockConfig.Reset take the lock too, but reading Calls directly does not, so read it once the calls are done. Setting a field while a method runs is a data race.
Values
Section titled “Values”NewMockConfigWithValues sets the values with config.Config.SetDefaults, so their keys are trimmed and lowercased, and two keys that become the same are an error. Nil values make an empty configuration. No provider loads, so the environment does not reach it. Any field may be replaced afterwards, and LoadProvider loads a provider into the configuration.
Static provider
Section titled “Static provider”A StaticProvider gives a real config.Config fixed values, for a test of code that builds its own Config from providers, such as an app’s function that loads its configuration:
c := config.New(config.WithProvider(configtest.NewStaticProvider(map[string]any{ "database": map[string]any{"host": "db"},})))if err := c.Load(); err != nil { return err}Each load returns the values, and the Config treats them as any
provider’s: their keys are trimmed and lowercased, two keys that become
the same fail the load, and a later provider overrides them. Errors name
the provider static. Nil values load nothing.
The provider keeps the map it is given rather than copying it, so do not change the map while a load may run.
API reference
Section titled “API reference”type Call
Section titled “type Call”type Call struct { Method string Args []any}Call is one call of a MockConfig method.
type MockConfig
Section titled “type MockConfig”type MockConfig struct { SetDefaultFn func(key string, value any) error SetDefaultsFn func(values any) error SetFn func(key string, value any) error BindFn func(dest any) error GetFn func(key string) any FindFn func(key string) (any, bool) ValuesFn func() map[string]any LoadProviderFn func(p config.Provider) error LoadProviderContextFn func(ctx context.Context, p config.Provider) error Calls []Call // contains filtered or unexported fields}MockConfig is a config.ConfigProvider that records its calls, as described in the package documentation. Each method calls the field named after it, when it is set.
func NewMockConfigWithValues
Section titled “func NewMockConfigWithValues”func NewMockConfigWithValues(values map[string]any) (*MockConfig, error)NewMockConfigWithValues returns a MockConfig whose fields are the methods of a config.Config holding values, as described in the package documentation.
func (*MockConfig) Bind
Section titled “func (*MockConfig) Bind”func (m *MockConfig) Bind(dest any) errorBind records the call and delegates to BindFn if set.
func (*MockConfig) CallCount
Section titled “func (*MockConfig) CallCount”func (m *MockConfig) CallCount(method string) intCallCount returns the number of times the given method was called.
func (*MockConfig) Find
Section titled “func (*MockConfig) Find”func (m *MockConfig) Find(key string) (value any, exist bool)Find records the call and delegates to FindFn if set.
func (*MockConfig) Get
Section titled “func (*MockConfig) Get”func (m *MockConfig) Get(key string) anyGet records the call and delegates to GetFn if set.
func (*MockConfig) LoadProvider
Section titled “func (*MockConfig) LoadProvider”func (m *MockConfig) LoadProvider(p config.Provider) errorLoadProvider records the call and delegates to LoadProviderFn if set.
func (*MockConfig) LoadProviderContext
Section titled “func (*MockConfig) LoadProviderContext”func (m *MockConfig) LoadProviderContext(ctx context.Context, p config.Provider) errorLoadProviderContext records the call, without ctx, and delegates to LoadProviderContextFn if set.
func (*MockConfig) Reset
Section titled “func (*MockConfig) Reset”func (m *MockConfig) Reset()Reset clears all recorded calls.
func (*MockConfig) Set
Section titled “func (*MockConfig) Set”func (m *MockConfig) Set(key string, value any) errorSet records the call and delegates to SetFn if set.
func (*MockConfig) SetDefault
Section titled “func (*MockConfig) SetDefault”func (m *MockConfig) SetDefault(key string, value any) errorSetDefault records the call and delegates to SetDefaultFn if set.
func (*MockConfig) SetDefaults
Section titled “func (*MockConfig) SetDefaults”func (m *MockConfig) SetDefaults(values any) errorSetDefaults records the call and delegates to SetDefaultsFn if set.
func (*MockConfig) Values
Section titled “func (*MockConfig) Values”func (m *MockConfig) Values() map[string]anyValues records the call and delegates to ValuesFn if set.
type StaticProvider
Section titled “type StaticProvider”type StaticProvider struct { // contains filtered or unexported fields}StaticProvider is a config.Provider that loads fixed values, as described in the package documentation.
func NewStaticProvider
Section titled “func NewStaticProvider”func NewStaticProvider(values map[string]any) *StaticProviderNewStaticProvider returns a StaticProvider that loads values. It keeps the map rather than copying it.
func (*StaticProvider) Load
Section titled “func (*StaticProvider) Load”func (p *StaticProvider) Load() (map[string]any, error)Load returns the values the provider was given.
func (*StaticProvider) Name
Section titled “func (*StaticProvider) Name”func (p *StaticProvider) Name() stringName returns “static”.