From 9d5c5bf7ab82159d9f0cb6936d0475a270c24862 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sun, 4 Apr 2021 14:47:57 +0100 Subject: [PATCH] fs: add Options.NonDefault to read options which aren't at their default #5178 --- fs/fs.go | 17 +++++++++++++++++ fs/fs_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/fs/fs.go b/fs/fs.go index 4911a8c98..946a75ee0 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -140,6 +140,23 @@ func (os Options) Overridden(m *configmap.Map) configmap.Simple { return overridden } +// NonDefault discovers which config values aren't at their default +func (os Options) NonDefault(m configmap.Getter) configmap.Simple { + var nonDefault = configmap.Simple{} + for i := range os { + opt := &os[i] + value, isSet := m.Get(opt.Name) + if !isSet { + continue + } + defaultValue := fmt.Sprint(opt.Default) + if value != defaultValue { + nonDefault.Set(opt.Name, value) + } + } + return nonDefault +} + // OptionVisibility controls whether the options are visible in the // configurator or the command line. type OptionVisibility byte diff --git a/fs/fs_test.go b/fs/fs_test.go index 4de8ba370..0ad09cf07 100644 --- a/fs/fs_test.go +++ b/fs/fs_test.go @@ -190,6 +190,43 @@ func TestOptionsGet(t *testing.T) { assert.Nil(t, opt) } +func TestOptionsOveridden(t *testing.T) { + m := configmap.New() + m1 := configmap.Simple{ + "nounc": "m1", + "copy_links": "m1", + } + m.AddGetter(m1, configmap.PriorityNormal) + m2 := configmap.Simple{ + "nounc": "m2", + "case_insensitive": "m2", + } + m.AddGetter(m2, configmap.PriorityConfig) + m3 := configmap.Simple{ + "nounc": "m3", + } + m.AddGetter(m3, configmap.PriorityDefault) + got := testOptions.Overridden(m) + assert.Equal(t, configmap.Simple{ + "copy_links": "m1", + "nounc": "m1", + }, got) +} + +func TestOptionsNonDefault(t *testing.T) { + m := configmap.Simple{} + got := testOptions.NonDefault(m) + assert.Equal(t, configmap.Simple{}, got) + + m["case_insensitive"] = "false" + got = testOptions.NonDefault(m) + assert.Equal(t, configmap.Simple{}, got) + + m["case_insensitive"] = "true" + got = testOptions.NonDefault(m) + assert.Equal(t, configmap.Simple{"case_insensitive": "true"}, got) +} + func TestOptionMarshalJSON(t *testing.T) { out, err := json.MarshalIndent(&caseInsensitiveOption, "", "") assert.NoError(t, err)