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.Shard(t)
tstest.Parallel(t) tstest.Parallel(t)
env := NewTestEnv(t, ConfigureControl(func(s *testcontrol.Server) { env := NewTestEnv(t, ConfigureControl(func(s *testcontrol.Server) {
s.CollectServices = "false" s.CollectServices = opt.False
})) }))
var testNodes []*TestNode var testNodes []*TestNode

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

@ -18,6 +18,22 @@ import (
// field without it being dropped. // field without it being dropped.
type Bool string 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, // NewBool constructs a new Bool value equal to b. The returned Bool is set,
// unless Set("") or Clear() methods are called. // unless Set("") or Clear() methods are called.
func NewBool(b bool) Bool { func NewBool(b bool) Bool {
@ -50,16 +66,16 @@ func (b *Bool) Scan(src any) error {
switch src := src.(type) { switch src := src.(type) {
case bool: case bool:
if src { if src {
*b = "true" *b = True
} else { } else {
*b = "false" *b = False
} }
return nil return nil
case int64: case int64:
if src == 0 { if src == 0 {
*b = "false" *b = False
} else { } else {
*b = "true" *b = True
} }
return nil return nil
default: default:
@ -75,18 +91,18 @@ func (b Bool) EqualBool(v bool) bool {
} }
var ( var (
trueBytes = []byte("true") trueBytes = []byte(True)
falseBytes = []byte("false") falseBytes = []byte(False)
nullBytes = []byte("null") nullBytes = []byte("null")
) )
func (b Bool) MarshalJSON() ([]byte, error) { func (b Bool) MarshalJSON() ([]byte, error) {
switch b { switch b {
case "true": case True:
return trueBytes, nil return trueBytes, nil
case "false": case False:
return falseBytes, nil return falseBytes, nil
case "", "unset": case Empty, ExplicitlyUnset:
return nullBytes, nil return nullBytes, nil
} }
return nil, fmt.Errorf("invalid opt.Bool value %q", string(b)) 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 { func (b *Bool) UnmarshalJSON(j []byte) error {
switch string(j) { switch string(j) {
case "true": case "true":
*b = "true" *b = True
case "false": case "false":
*b = "false" *b = False
case "null": case "null":
*b = "unset" *b = ExplicitlyUnset
default: default:
return fmt.Errorf("invalid opt.Bool value %q", j) return fmt.Errorf("invalid opt.Bool value %q", j)
} }

Loading…
Cancel
Save