-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
github now sets reviewers/team-reviewers/assignees/labels insteads of
adding them
- Loading branch information
Showing
5 changed files
with
203 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cmd | ||
|
||
import "github.com/spf13/pflag" | ||
|
||
// stringSlice is a wrapped around *pflag.FlagSet.GetStringSlice to allow nil when the flag is not set | ||
func stringSlice(set *pflag.FlagSet, name string) ([]string, error) { | ||
if !set.Changed(name) { | ||
return nil, nil | ||
} | ||
return set.GetStringSlice(name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package scm | ||
|
||
// Diff two slices and get the added and removed items compared to s1 | ||
func Diff[T comparable](s1, s2 []T) (added, removed []T) { | ||
s1Lookup := map[T]struct{}{} | ||
for _, v := range s1 { | ||
s1Lookup[v] = struct{}{} | ||
} | ||
s2Lookup := map[T]struct{}{} | ||
for _, v := range s2 { | ||
s2Lookup[v] = struct{}{} | ||
} | ||
|
||
for _, v := range s2 { | ||
if _, ok := s1Lookup[v]; !ok { | ||
added = append(added, v) | ||
} | ||
} | ||
for _, v := range s1 { | ||
if _, ok := s2Lookup[v]; !ok { | ||
removed = append(removed, v) | ||
} | ||
} | ||
|
||
return added, removed | ||
} | ||
|
||
// Map runs a function for each value in a slice and returns a slice of all function returns | ||
func Map[T any, K any](vals []T, mapping func(T) K) []K { | ||
newVals := make([]K, len(vals)) | ||
for i, v := range vals { | ||
newVals[i] = mapping(v) | ||
} | ||
return newVals | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package scm | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestDiff(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
s1 []int | ||
s2 []int | ||
wantAdded []int | ||
wantRemoved []int | ||
}{ | ||
{ | ||
name: "same", | ||
s1: []int{1, 2, 3}, | ||
s2: []int{1, 2, 3}, | ||
wantAdded: nil, | ||
wantRemoved: nil, | ||
}, | ||
{ | ||
name: "empty s2", | ||
s1: []int{1, 2, 3}, | ||
s2: []int{}, | ||
wantAdded: nil, | ||
wantRemoved: []int{1, 2, 3}, | ||
}, | ||
{ | ||
name: "empty s1", | ||
s1: []int{}, | ||
s2: []int{1, 2, 3}, | ||
wantAdded: []int{1, 2, 3}, | ||
wantRemoved: nil, | ||
}, | ||
{ | ||
name: "some overlap", | ||
s1: []int{1, 2, 3}, | ||
s2: []int{3, 4, 5}, | ||
wantAdded: []int{4, 5}, | ||
wantRemoved: []int{1, 2}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotAdded, gotRemoved := Diff(tt.s1, tt.s2) | ||
if !reflect.DeepEqual(gotAdded, tt.wantAdded) { | ||
t.Errorf("Diff() gotAdded = %v, want %v", gotAdded, tt.wantAdded) | ||
} | ||
if !reflect.DeepEqual(gotRemoved, tt.wantRemoved) { | ||
t.Errorf("Diff() gotRemoved = %v, want %v", gotRemoved, tt.wantRemoved) | ||
} | ||
}) | ||
} | ||
} |