From f111e0eaf810beb7d404ec3df271407f40ef2d4a Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 10 Mar 2021 12:17:08 +0000 Subject: [PATCH] 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. --- fs/config/configmap/configmap.go | 34 +++++++++++++----- fs/config/configmap/configmap_test.go | 50 +++++++++++++++++++++++++++ fs/fs.go | 8 ++--- 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/fs/config/configmap/configmap.go b/fs/config/configmap/configmap.go index fa323e1c1..9cd3891f3 100644 --- a/fs/config/configmap/configmap.go +++ b/fs/config/configmap/configmap.go @@ -29,8 +29,9 @@ type Mapper interface { // Map provides a wrapper around multiple Setter and // Getter interfaces. type Map struct { - setters []Setter - getters []Getter + setters []Setter + getters []Getter + override []Getter } // New returns an empty Map @@ -44,9 +45,12 @@ func (c *Map) AddGetter(getter Getter) *Map { return c } -// AddGetters appends multiple getters onto the end of the getters -func (c *Map) AddGetters(getters ...Getter) *Map { - c.getters = append(c.getters, getters...) +// AddOverrideGetter appends a getter onto the end of the 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 } @@ -56,11 +60,11 @@ func (c *Map) AddSetter(setter Setter) *Map { 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, // otherwise false. -func (c *Map) Get(key string) (value string, ok bool) { - for _, do := range c.getters { +func (c *Map) get(key string, getters []Getter) (value string, ok bool) { + for _, do := range getters { value, ok = do.Get(key) if ok { return value, ok @@ -69,6 +73,20 @@ func (c *Map) Get(key string) (value string, ok bool) { 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. func (c *Map) Set(key, value string) { for _, do := range c.setters { diff --git a/fs/config/configmap/configmap_test.go b/fs/config/configmap/configmap_test.go index 90b70a4bf..fc38060b2 100644 --- a/fs/config/configmap/configmap_test.go +++ b/fs/config/configmap/configmap_test.go @@ -90,6 +90,56 @@ func TestConfigMapSet(t *testing.T) { }, 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) { // Basic assert.Equal(t, "", Simple(nil).String()) diff --git a/fs/fs.go b/fs/fs.go index 92425c416..02ec37db5 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -1316,20 +1316,20 @@ func ConfigMap(fsInfo *RegInfo, configName string, connectionStringConfig config // Config from connection string if len(connectionStringConfig) > 0 { - config.AddGetter(connectionStringConfig) + config.AddOverrideGetter(connectionStringConfig) } // flag values if fsInfo != nil { - config.AddGetter(®InfoValues{fsInfo, false}) + config.AddOverrideGetter(®InfoValues{fsInfo, false}) } // remote specific environment vars - config.AddGetter(configEnvVars(configName)) + config.AddOverrideGetter(configEnvVars(configName)) // backend specific environment vars if fsInfo != nil { - config.AddGetter(optionEnvVars{fsInfo: fsInfo}) + config.AddOverrideGetter(optionEnvVars{fsInfo: fsInfo}) } // config file