Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key: add AllowDuplicateShadowValues option #298

Merged
merged 2 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 8 additions & 6 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down