util/slicesx: add AppendNonzero
Some checks are pending
checklocks / checklocks (push) Waiting to run
CodeQL / Analyze (go) (push) Waiting to run
Dockerfile build / deploy (push) Waiting to run
CI / race-root-integration (1/4) (push) Waiting to run
CI / race-root-integration (2/4) (push) Waiting to run
CI / race-root-integration (3/4) (push) Waiting to run
CI / race-root-integration (4/4) (push) Waiting to run
CI / test (-coverprofile=/tmp/coverage.out, amd64) (push) Waiting to run
CI / test (-race, amd64, 1/3) (push) Waiting to run
CI / test (-race, amd64, 2/3) (push) Waiting to run
CI / test (-race, amd64, 3/3) (push) Waiting to run
CI / test (386) (push) Waiting to run
CI / windows (push) Waiting to run
CI / privileged (push) Waiting to run
CI / vm (push) Waiting to run
CI / race-build (push) Waiting to run
CI / cross (386, linux) (push) Waiting to run
CI / cross (amd64, darwin) (push) Waiting to run
CI / cross (amd64, freebsd) (push) Waiting to run
CI / cross (amd64, openbsd) (push) Waiting to run
CI / cross (amd64, windows) (push) Waiting to run
CI / cross (arm, 5, linux) (push) Waiting to run
CI / cross (arm, 7, linux) (push) Waiting to run
CI / cross (arm64, darwin) (push) Waiting to run
CI / cross (arm64, linux) (push) Waiting to run
CI / cross (arm64, windows) (push) Waiting to run
CI / cross (loong64, linux) (push) Waiting to run
CI / ios (push) Waiting to run
CI / crossmin (amd64, illumos) (push) Waiting to run
CI / crossmin (amd64, plan9) (push) Waiting to run
CI / crossmin (amd64, solaris) (push) Waiting to run
CI / crossmin (ppc64, aix) (push) Waiting to run
CI / android (push) Waiting to run
CI / wasm (push) Waiting to run
CI / tailscale_go (push) Waiting to run
CI / fuzz (push) Waiting to run
CI / depaware (push) Waiting to run
CI / go_generate (push) Waiting to run
CI / go_mod_tidy (push) Waiting to run
CI / licenses (push) Waiting to run
CI / staticcheck (386, windows) (push) Waiting to run
CI / staticcheck (amd64, darwin) (push) Waiting to run
CI / staticcheck (amd64, linux) (push) Waiting to run
CI / staticcheck (amd64, windows) (push) Waiting to run
CI / notify_slack (push) Blocked by required conditions
CI / check_mergeability (push) Blocked by required conditions
Some checks are pending
checklocks / checklocks (push) Waiting to run
CodeQL / Analyze (go) (push) Waiting to run
Dockerfile build / deploy (push) Waiting to run
CI / race-root-integration (1/4) (push) Waiting to run
CI / race-root-integration (2/4) (push) Waiting to run
CI / race-root-integration (3/4) (push) Waiting to run
CI / race-root-integration (4/4) (push) Waiting to run
CI / test (-coverprofile=/tmp/coverage.out, amd64) (push) Waiting to run
CI / test (-race, amd64, 1/3) (push) Waiting to run
CI / test (-race, amd64, 2/3) (push) Waiting to run
CI / test (-race, amd64, 3/3) (push) Waiting to run
CI / test (386) (push) Waiting to run
CI / windows (push) Waiting to run
CI / privileged (push) Waiting to run
CI / vm (push) Waiting to run
CI / race-build (push) Waiting to run
CI / cross (386, linux) (push) Waiting to run
CI / cross (amd64, darwin) (push) Waiting to run
CI / cross (amd64, freebsd) (push) Waiting to run
CI / cross (amd64, openbsd) (push) Waiting to run
CI / cross (amd64, windows) (push) Waiting to run
CI / cross (arm, 5, linux) (push) Waiting to run
CI / cross (arm, 7, linux) (push) Waiting to run
CI / cross (arm64, darwin) (push) Waiting to run
CI / cross (arm64, linux) (push) Waiting to run
CI / cross (arm64, windows) (push) Waiting to run
CI / cross (loong64, linux) (push) Waiting to run
CI / ios (push) Waiting to run
CI / crossmin (amd64, illumos) (push) Waiting to run
CI / crossmin (amd64, plan9) (push) Waiting to run
CI / crossmin (amd64, solaris) (push) Waiting to run
CI / crossmin (ppc64, aix) (push) Waiting to run
CI / android (push) Waiting to run
CI / wasm (push) Waiting to run
CI / tailscale_go (push) Waiting to run
CI / fuzz (push) Waiting to run
CI / depaware (push) Waiting to run
CI / go_generate (push) Waiting to run
CI / go_mod_tidy (push) Waiting to run
CI / licenses (push) Waiting to run
CI / staticcheck (386, windows) (push) Waiting to run
CI / staticcheck (amd64, darwin) (push) Waiting to run
CI / staticcheck (amd64, linux) (push) Waiting to run
CI / staticcheck (amd64, windows) (push) Waiting to run
CI / notify_slack (push) Blocked by required conditions
CI / check_mergeability (push) Blocked by required conditions
By request of @agottardo. Updates #cleanup Change-Id: I2f02314eb9533b1581e47b66b45b6fb8ac257bb7 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
17022ad0e9
commit
8b65598614
@ -242,7 +242,7 @@ change in the future.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func nonNilCmds(cmds ...*ffcli.Command) []*ffcli.Command {
|
func nonNilCmds(cmds ...*ffcli.Command) []*ffcli.Command {
|
||||||
return slicesx.Filter(cmds[:0], cmds, func(c *ffcli.Command) bool { return c != nil })
|
return slicesx.AppendNonzero(cmds[:0], cmds)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fatalf(format string, a ...any) {
|
func fatalf(format string, a ...any) {
|
||||||
|
@ -95,6 +95,17 @@ func Filter[S ~[]T, T any](dst, src S, fn func(T) bool) S {
|
|||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppendNonzero appends all non-zero elements of src to dst.
|
||||||
|
func AppendNonzero[S ~[]T, T comparable](dst, src S) S {
|
||||||
|
var zero T
|
||||||
|
for _, v := range src {
|
||||||
|
if v != zero {
|
||||||
|
dst = append(dst, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dst
|
||||||
|
}
|
||||||
|
|
||||||
// AppendMatching appends elements in ps to dst if f(x) is true.
|
// AppendMatching appends elements in ps to dst if f(x) is true.
|
||||||
func AppendMatching[T any](dst, ps []T, f func(T) bool) []T {
|
func AppendMatching[T any](dst, ps []T, f func(T) bool) []T {
|
||||||
for _, p := range ps {
|
for _, p := range ps {
|
||||||
|
@ -137,6 +137,19 @@ func TestFilterNoAllocations(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAppendNonzero(t *testing.T) {
|
||||||
|
v := []string{"one", "two", "", "four"}
|
||||||
|
got := AppendNonzero(nil, v)
|
||||||
|
want := []string{"one", "two", "four"}
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("got %v; want %v", got, want)
|
||||||
|
}
|
||||||
|
got = AppendNonzero(v[:0], v)
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("got %v; want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAppendMatching(t *testing.T) {
|
func TestAppendMatching(t *testing.T) {
|
||||||
v := []string{"one", "two", "three", "four"}
|
v := []string{"one", "two", "three", "four"}
|
||||||
got := AppendMatching(v[:0], v, func(s string) bool { return len(s) > 3 })
|
got := AppendMatching(v[:0], v, func(s string) bool { return len(s) > 3 })
|
||||||
|
@ -289,7 +289,7 @@ func newSettingMetric(key setting.Key, scope setting.Scope, suffix string, typ c
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newMetric(nameParts []string, typ clientmetric.Type) metric {
|
func newMetric(nameParts []string, typ clientmetric.Type) metric {
|
||||||
name := strings.Join(slicesx.Filter([]string{internal.OS(), "syspolicy"}, nameParts, isNonEmpty), "_")
|
name := strings.Join(slicesx.AppendNonzero([]string{internal.OS(), "syspolicy"}, nameParts), "_")
|
||||||
switch {
|
switch {
|
||||||
case !ShouldReport():
|
case !ShouldReport():
|
||||||
return &funcMetric{name: name, typ: typ}
|
return &funcMetric{name: name, typ: typ}
|
||||||
@ -304,8 +304,6 @@ func newMetric(nameParts []string, typ clientmetric.Type) metric {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isNonEmpty(s string) bool { return s != "" }
|
|
||||||
|
|
||||||
func metricScopeName(scope setting.Scope) string {
|
func metricScopeName(scope setting.Scope) string {
|
||||||
switch scope {
|
switch scope {
|
||||||
case setting.DeviceSetting:
|
case setting.DeviceSetting:
|
||||||
|
Reference in New Issue
Block a user