types/bool: add Int (#14984)
Add Int which converts a bool into an integer. Updates tailscale/corp#22024 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
parent
27f8e2e31d
commit
8b347060f8
@ -1,9 +1,18 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Package bools contains the [Compare] and [Select] functions.
|
||||
// Package bools contains the [Int], [Compare], and [Select] functions.
|
||||
package bools
|
||||
|
||||
// Int returns 1 for true and 0 for false.
|
||||
func Int(v bool) int {
|
||||
if v {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Compare compares two boolean values as if false is ordered before true.
|
||||
func Compare[T ~bool](x, y T) int {
|
||||
switch {
|
||||
|
@ -5,6 +5,15 @@
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInt(t *testing.T) {
|
||||
if got := Int(true); got != 1 {
|
||||
t.Errorf("Int(true) = %v, want 1", got)
|
||||
}
|
||||
if got := Int(false); got != 0 {
|
||||
t.Errorf("Int(false) = %v, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompare(t *testing.T) {
|
||||
if got := Compare(false, false); got != 0 {
|
||||
t.Errorf("Compare(false, false) = %v, want 0", got)
|
||||
|
Loading…
Reference in New Issue
Block a user