fs: enable configmap to be able to tell values set vs config file values #4996

This adds AddOverrideGetter and GetOverride methods to config map and
uses them in fs.ConfigMap.

This enables us to tell which values have been set and which are just
read from the config file or at their defaults.

This also deletes the unused AddGetters method in configmap.
This commit is contained in:
Nick Craig-Wood 2021-03-10 12:17:08 +00:00
parent 96207f342c
commit f111e0eaf8
3 changed files with 80 additions and 12 deletions

View file

@ -31,6 +31,7 @@ type Mapper interface {
type Map struct { type Map struct {
setters []Setter setters []Setter
getters []Getter getters []Getter
override []Getter
} }
// New returns an empty Map // New returns an empty Map
@ -44,9 +45,12 @@ func (c *Map) AddGetter(getter Getter) *Map {
return c return c
} }
// AddGetters appends multiple getters onto the end of the getters // AddOverrideGetter appends a getter onto the end of the getters
func (c *Map) AddGetters(getters ...Getter) *Map { //
c.getters = append(c.getters, getters...) // It also appends it onto the override getters for GetOverride
func (c *Map) AddOverrideGetter(getter Getter) *Map {
c.getters = append(c.getters, getter)
c.override = append(c.override, getter)
return c return c
} }
@ -56,11 +60,11 @@ func (c *Map) AddSetter(setter Setter) *Map {
return c return c
} }
// Get gets an item with the key passed in and return the value from // get gets an item with the key passed in and return the value from
// the first getter. If the item is found then it returns true, // the first getter. If the item is found then it returns true,
// otherwise false. // otherwise false.
func (c *Map) Get(key string) (value string, ok bool) { func (c *Map) get(key string, getters []Getter) (value string, ok bool) {
for _, do := range c.getters { for _, do := range getters {
value, ok = do.Get(key) value, ok = do.Get(key)
if ok { if ok {
return value, ok return value, ok
@ -69,6 +73,20 @@ func (c *Map) Get(key string) (value string, ok bool) {
return "", false return "", false
} }
// Get gets an item with the key passed in and return the value from
// the first getter. If the item is found then it returns true,
// otherwise false.
func (c *Map) Get(key string) (value string, ok bool) {
return c.get(key, c.getters)
}
// GetOverride gets an item with the key passed in and return the
// value from the first override getter. If the item is found then it
// returns true, otherwise false.
func (c *Map) GetOverride(key string) (value string, ok bool) {
return c.get(key, c.override)
}
// Set sets an item into all the stored setters. // Set sets an item into all the stored setters.
func (c *Map) Set(key, value string) { func (c *Map) Set(key, value string) {
for _, do := range c.setters { for _, do := range c.setters {

View file

@ -90,6 +90,56 @@ func TestConfigMapSet(t *testing.T) {
}, m2) }, m2)
} }
func TestConfigMapGetOverride(t *testing.T) {
m := New()
value, found := m.GetOverride("config1")
assert.Equal(t, "", value)
assert.Equal(t, false, found)
value, found = m.GetOverride("config2")
assert.Equal(t, "", value)
assert.Equal(t, false, found)
m1 := Simple{
"config1": "one",
}
m.AddOverrideGetter(m1)
value, found = m.GetOverride("config1")
assert.Equal(t, "one", value)
assert.Equal(t, true, found)
value, found = m.GetOverride("config2")
assert.Equal(t, "", value)
assert.Equal(t, false, found)
m2 := Simple{
"config1": "one2",
"config2": "two2",
}
m.AddGetter(m2)
value, found = m.GetOverride("config1")
assert.Equal(t, "one", value)
assert.Equal(t, true, found)
value, found = m.GetOverride("config2")
assert.Equal(t, "", value)
assert.Equal(t, false, found)
value, found = m.Get("config1")
assert.Equal(t, "one", value)
assert.Equal(t, true, found)
value, found = m.Get("config2")
assert.Equal(t, "two2", value)
assert.Equal(t, true, found)
}
func TestSimpleString(t *testing.T) { func TestSimpleString(t *testing.T) {
// Basic // Basic
assert.Equal(t, "", Simple(nil).String()) assert.Equal(t, "", Simple(nil).String())

View file

@ -1316,20 +1316,20 @@ func ConfigMap(fsInfo *RegInfo, configName string, connectionStringConfig config
// Config from connection string // Config from connection string
if len(connectionStringConfig) > 0 { if len(connectionStringConfig) > 0 {
config.AddGetter(connectionStringConfig) config.AddOverrideGetter(connectionStringConfig)
} }
// flag values // flag values
if fsInfo != nil { if fsInfo != nil {
config.AddGetter(&regInfoValues{fsInfo, false}) config.AddOverrideGetter(&regInfoValues{fsInfo, false})
} }
// remote specific environment vars // remote specific environment vars
config.AddGetter(configEnvVars(configName)) config.AddOverrideGetter(configEnvVars(configName))
// backend specific environment vars // backend specific environment vars
if fsInfo != nil { if fsInfo != nil {
config.AddGetter(optionEnvVars{fsInfo: fsInfo}) config.AddOverrideGetter(optionEnvVars{fsInfo: fsInfo})
} }
// config file // config file