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:
Joe Tsai 2025-02-11 10:23:36 -08:00 committed by GitHub
parent 27f8e2e31d
commit 8b347060f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View File

@ -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 {

View File

@ -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)