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

fix(config): ordering of list option values not preserved #805

Merged
merged 1 commit into from
Jul 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions internal/cmd/config/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"fmt"
"os"
"slices"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -52,8 +51,7 @@ func runAdd(s state.State, cmd *cobra.Command, args []string) error {
case []string:
before := util.AnyToStringSlice(val)
newVal := append(before, values...)
slices.Sort(newVal)
newVal = slices.Compact(newVal)
newVal = util.RemoveDuplicates(newVal)
val = newVal
added = util.ToAnySlice(util.SliceDiff[[]string](newVal, before))
default:
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/config/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ active_context = "test_context"
args: []string{"--global", "array-option", "c", "b", "c", "a", "a"},
config: testConfig,
expErr: "Warning: some values were already present or duplicate\n",
expOut: `Added '[a b c]' to 'array-option' globally
expOut: `Added '[c b a]' to 'array-option' globally
active_context = "test_context"

[preferences]
array_option = ["a", "b", "c"]
array_option = ["c", "b", "a"]
debug = true
poll_interval = "1.234s"

Expand Down
39 changes: 26 additions & 13 deletions internal/cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"reflect"
"slices"
"sort"
"strings"
"text/template"
Expand Down Expand Up @@ -237,24 +236,38 @@ func FilterNil[T any](values []T) []T {
return filtered
}

// SliceDiff returns the difference between the two passed slices. The returned slice contains all elements that are present in a but not in b.
// Note that it does not preserve order.
// SliceDiff returns the difference between the two passed slices. The returned slice contains all elements that
// are present in a but not in b. The order of a is preserved.
func SliceDiff[S ~[]E, E cmp.Ordered](a, b []E) []E {
m := make(map[E]struct{})
for _, x := range a {
m[x] = struct{}{}
}
for _, x := range b {
delete(m, x)
}
var diff S
for x := range m {
diff = append(diff, x)
seen := make(map[E]struct{})
for _, v := range b {
seen[v] = struct{}{}
}
for _, v := range a {
if _, ok := seen[v]; ok {
continue
}
diff = append(diff, v)
}
slices.Sort(diff)
return diff
}

// RemoveDuplicates removes duplicates from the passed slice while preserving the order of the elements.
// The first occurrence of an element is kept, all following occurrences are removed.
func RemoveDuplicates[S ~[]E, E cmp.Ordered](values S) S {
var unique S
seen := make(map[E]struct{})
for _, v := range values {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
unique = append(unique, v)
}
return unique
}

func AnyToAnySlice(a any) []any {
val := reflect.ValueOf(a)
if val.Kind() != reflect.Slice {
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,12 @@ func TestToStringSliceDelimited(t *testing.T) {
assert.Equal(t, []string{"a", "b", "c"}, util.ToStringSliceDelimited("a,b,c"))
assert.Equal(t, []string{"0", "1", "2"}, util.ToStringSliceDelimited([]int{0, 1, 2}))
}

func TestRemoveDuplicates(t *testing.T) {
assert.Equal(t, []string{"a", "b", "c"}, util.RemoveDuplicates([]string{"a", "b", "c"}))
assert.Equal(t, []string{"a", "b", "c"}, util.RemoveDuplicates([]string{"a", "b", "c", "a", "b", "c"}))
assert.Equal(t, []string{"a", "b", "c"}, util.RemoveDuplicates([]string{"a", "b", "c", "c", "b", "a"}))
assert.Equal(t, []string{"c", "b", "a"}, util.RemoveDuplicates([]string{"c", "b", "a", "a", "b", "c"}))
assert.Equal(t, []string{"a"}, util.RemoveDuplicates([]string{"a", "a", "a", "a", "a"}))
assert.Equal(t, []int{1, 2, 3, 4, 5}, util.RemoveDuplicates([]int{1, 2, 1, 1, 3, 2, 1, 4, 3, 2, 5, 4, 3, 2, 1}))
}
7 changes: 1 addition & 6 deletions internal/state/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -288,12 +287,8 @@ func (o *Option[T]) Parse(values []string) (any, error) {
if err != nil {
return nil, fmt.Errorf("invalid duration value: %s", value)
}

case []string:
newVal := values[:]
slices.Sort(newVal)
newVal = slices.Compact(newVal)
val = newVal
val = util.RemoveDuplicates(values)
default:
return nil, fmt.Errorf("unsupported type %T", t)
}
Expand Down