Skip to content

Commit 7f697fc

Browse files
use generic impl of uniquify
1 parent fc9f65b commit 7f697fc

File tree

2 files changed

+5
-28
lines changed

2 files changed

+5
-28
lines changed

internal/cli/config/add.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,10 @@ import (
2525
"github.com/spf13/cobra"
2626
)
2727

28-
// // TODO: When update to go 1.18 or later, convert to generic
29-
// // to allow uniquify() on any slice that supports
30-
// // `comparable`
31-
// // See https://gosamples.dev/generics-remove-duplicates-slice/
32-
// func uniquify[T comparable](s []T) []T {
33-
// // use a map, which enforces unique keys
34-
// inResult := make(map[T]bool)
35-
// var result []T
36-
// // loop through input slice **in order**,
37-
// // to ensure output retains that order
38-
// // (except that it removes duplicates)
39-
// for i := 0; i < len(s); i++ {
40-
// // attempt to use the element as a key
41-
// if _, ok := inResult[s[i]]; !ok {
42-
// // if key didn't exist in map,
43-
// // add to map and append to result
44-
// inResult[s[i]] = true
45-
// result = append(result, s[i])
46-
// }
47-
// }
48-
// return result
49-
// }
50-
51-
func uniquifyStringSlice(s []string) []string {
28+
func uniquify[T comparable](s []T) []T {
5229
// use a map, which enforces unique keys
53-
inResult := make(map[string]bool)
54-
var result []string
30+
inResult := make(map[T]bool)
31+
var result []T
5532
// loop through input slice **in order**,
5633
// to ensure output retains that order
5734
// (except that it removes duplicates)
@@ -96,7 +73,7 @@ func runAddCommand(cmd *cobra.Command, args []string) {
9673

9774
v := configuration.Settings.GetStringSlice(key)
9875
v = append(v, args[1:]...)
99-
v = uniquifyStringSlice(v)
76+
v = uniquify(v)
10077
configuration.Settings.Set(key, v)
10178

10279
if err := configuration.Settings.WriteConfig(); err != nil {

internal/cli/config/set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func runSetCommand(cmd *cobra.Command, args []string) {
5757
var value interface{}
5858
switch kind {
5959
case reflect.Slice:
60-
value = uniquifyStringSlice(args[1:])
60+
value = uniquify(args[1:])
6161
case reflect.String:
6262
value = args[1]
6363
case reflect.Bool:

0 commit comments

Comments
 (0)