types/opt: de-weird the API a bit with new True and False consts

Updates #cleanup

Change-Id: I15d8d840877d43e2b884d42354b4eb156094df7d
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/17302/head
Brad Fitzpatrick 2 months ago committed by Brad Fitzpatrick
parent e766adf71f
commit afe909664b

@ -1785,7 +1785,7 @@ func TestC2NDebugNetmap(t *testing.T) {
tstest.Shard(t)
tstest.Parallel(t)
env := NewTestEnv(t, ConfigureControl(func(s *testcontrol.Server) {
s.CollectServices = "false"
s.CollectServices = opt.False
}))
var testNodes []*TestNode

@ -1101,7 +1101,7 @@ func (s *Server) MapResponse(req *tailcfg.MapRequest) (res *tailcfg.MapResponse,
Node: node,
DERPMap: s.DERPMap,
Domain: domain,
CollectServices: cmp.Or(s.CollectServices, "true"),
CollectServices: cmp.Or(s.CollectServices, opt.True),
PacketFilter: packetFilterWithIngress(s.PeerRelayGrants),
DNSConfig: dns,
ControlTime: &t,

@ -18,6 +18,22 @@ import (
// field without it being dropped.
type Bool string
const (
// True is the encoding of an explicit true.
True = Bool("true")
// False is the encoding of an explicit false.
False = Bool("false")
// ExplicitlyUnset is the encoding used by a null
// JSON value. It is a synonym for the empty string.
ExplicitlyUnset = Bool("unset")
// Empty means the Bool is unset and it's neither
// true nor false.
Empty = Bool("")
)
// NewBool constructs a new Bool value equal to b. The returned Bool is set,
// unless Set("") or Clear() methods are called.
func NewBool(b bool) Bool {
@ -50,16 +66,16 @@ func (b *Bool) Scan(src any) error {
switch src := src.(type) {
case bool:
if src {
*b = "true"
*b = True
} else {
*b = "false"
*b = False
}
return nil
case int64:
if src == 0 {
*b = "false"
*b = False
} else {
*b = "true"
*b = True
}
return nil
default:
@ -75,18 +91,18 @@ func (b Bool) EqualBool(v bool) bool {
}
var (
trueBytes = []byte("true")
falseBytes = []byte("false")
trueBytes = []byte(True)
falseBytes = []byte(False)
nullBytes = []byte("null")
)
func (b Bool) MarshalJSON() ([]byte, error) {
switch b {
case "true":
case True:
return trueBytes, nil
case "false":
case False:
return falseBytes, nil
case "", "unset":
case Empty, ExplicitlyUnset:
return nullBytes, nil
}
return nil, fmt.Errorf("invalid opt.Bool value %q", string(b))
@ -95,11 +111,11 @@ func (b Bool) MarshalJSON() ([]byte, error) {
func (b *Bool) UnmarshalJSON(j []byte) error {
switch string(j) {
case "true":
*b = "true"
*b = True
case "false":
*b = "false"
*b = False
case "null":
*b = "unset"
*b = ExplicitlyUnset
default:
return fmt.Errorf("invalid opt.Bool value %q", j)
}

Loading…
Cancel
Save