From c538fcde2b2fa510a239e28fb544b96859af129c Mon Sep 17 00:00:00 2001 From: Drayton Munster Date: Thu, 11 Mar 2021 22:07:34 -0500 Subject: [PATCH] Add AllowDuplicateShadowValues option --- ini.go | 2 ++ key.go | 17 ++++++++--------- key_test.go | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/ini.go b/ini.go index 23f0742..3ca84fd 100644 --- a/ini.go +++ b/ini.go @@ -125,6 +125,8 @@ type LoadOptions struct { ReaderBufferSize int // AllowNonUniqueSections indicates whether to allow sections with the same name multiple times. AllowNonUniqueSections bool + // AllowDuplicateShadowValues indicates whether values for shadowed keys should be deduplicated. + AllowDuplicateShadowValues bool } // DebugFunc is the type of function called to log parse events. diff --git a/key.go b/key.go index 8baafd9..0302c29 100644 --- a/key.go +++ b/key.go @@ -54,14 +54,16 @@ func (k *Key) addShadow(val string) error { return errors.New("cannot add shadow to auto-increment or boolean key") } - // Deduplicate shadows based on their values. - if k.value == val { - return nil - } - for i := range k.shadows { - if k.shadows[i].value == val { + if !k.s.f.options.AllowDuplicateShadowValues { + // Deduplicate shadows based on their values. + if k.value == val { return nil } + for i := range k.shadows { + if k.shadows[i].value == val { + return nil + } + } } shadow := newKey(k.s, k.name, val) @@ -781,10 +783,8 @@ func (k *Key) parseUint64s(strs []string, addInvalid, returnOnInvalid bool) ([]u return vals, err } - type Parser func(str string) (interface{}, error) - // parseTimesFormat transforms strings to times in given format. func (k *Key) parseTimesFormat(format string, strs []string, addInvalid, returnOnInvalid bool) ([]time.Time, error) { vals := make([]time.Time, 0, len(strs)) @@ -801,7 +801,6 @@ func (k *Key) parseTimesFormat(format string, strs []string, addInvalid, returnO return vals, err } - // doParse transforms strings to different types func (k *Key) doParse(strs []string, addInvalid, returnOnInvalid bool, parser Parser) ([]interface{}, error) { vals := make([]interface{}, 0, len(strs)) diff --git a/key_test.go b/key_test.go index 238f938..3d4c0e3 100644 --- a/key_test.go +++ b/key_test.go @@ -51,6 +51,29 @@ func TestKey_AddShadow(t *testing.T) { Convey("Add shadow to auto-increment key", func() { So(f.Section("notes").Key("#1").AddShadow("beta"), ShouldNotBeNil) }) + + Convey("Deduplicate an existing value", func() { + k := f.Section("").Key("NAME") + So(k.AddShadow("ini"), ShouldBeNil) + So(k.ValueWithShadows(), ShouldResemble, []string{"ini", "ini.v1"}) + }) + }) + + Convey("Allow duplicate shadowed values", t, func() { + f := ini.Empty(ini.LoadOptions{ + AllowShadows: true, + AllowDuplicateShadowValues: true, + }) + So(f, ShouldNotBeNil) + + k, err := f.Section("").NewKey("NAME", "ini") + So(err, ShouldBeNil) + So(k, ShouldNotBeNil) + + So(k.AddShadow("ini.v1"), ShouldBeNil) + So(k.AddShadow("ini"), ShouldBeNil) + So(k.AddShadow("ini"), ShouldBeNil) + So(k.ValueWithShadows(), ShouldResemble, []string{"ini", "ini.v1", "ini", "ini"}) }) Convey("Shadow is not allowed", t, func() {