2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-27 17:47:04 +00:00
|
|
|
|
|
|
|
// Package opt defines optional types.
|
|
|
|
package opt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2022-08-09 19:49:02 +00:00
|
|
|
// Bool represents an optional boolean to be JSON-encoded. The string
|
2022-09-25 18:29:55 +00:00
|
|
|
// is either "true", "false", or the empty string to mean unset.
|
2022-08-09 19:49:02 +00:00
|
|
|
//
|
|
|
|
// As a special case, the underlying string may also be the string
|
|
|
|
// "unset" as as a synonym for the empty string. This lets the
|
|
|
|
// explicit unset value be exchanged over an encoding/json "omitempty"
|
|
|
|
// field without it being dropped.
|
2020-02-27 17:47:04 +00:00
|
|
|
type Bool string
|
|
|
|
|
2023-12-18 22:57:03 +00:00
|
|
|
// 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 {
|
|
|
|
return Bool(strconv.FormatBool(b))
|
|
|
|
}
|
|
|
|
|
2020-02-27 17:47:04 +00:00
|
|
|
func (b *Bool) Set(v bool) {
|
|
|
|
*b = Bool(strconv.FormatBool(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bool) Clear() { *b = "" }
|
|
|
|
|
|
|
|
func (b Bool) Get() (v bool, ok bool) {
|
2022-08-09 19:49:02 +00:00
|
|
|
switch b {
|
|
|
|
case "true":
|
|
|
|
return true, true
|
|
|
|
case "false":
|
|
|
|
return false, true
|
|
|
|
default:
|
|
|
|
return false, false
|
2020-02-27 17:47:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-03 17:07:12 +00:00
|
|
|
|
|
|
|
// Scan implements database/sql.Scanner.
|
2022-03-16 23:27:57 +00:00
|
|
|
func (b *Bool) Scan(src any) error {
|
2021-10-03 17:07:12 +00:00
|
|
|
if src == nil {
|
|
|
|
*b = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch src := src.(type) {
|
|
|
|
case bool:
|
|
|
|
if src {
|
|
|
|
*b = "true"
|
|
|
|
} else {
|
|
|
|
*b = "false"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case int64:
|
|
|
|
if src == 0 {
|
|
|
|
*b = "false"
|
|
|
|
} else {
|
|
|
|
*b = "true"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("opt.Bool.Scan: invalid type %T: %v", src, src)
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 17:47:04 +00:00
|
|
|
|
2020-06-26 02:07:38 +00:00
|
|
|
// EqualBool reports whether b is equal to v.
|
|
|
|
// If b is empty or not a valid bool, it reports false.
|
|
|
|
func (b Bool) EqualBool(v bool) bool {
|
|
|
|
p, ok := b.Get()
|
|
|
|
return ok && p == v
|
|
|
|
}
|
|
|
|
|
2020-02-27 17:47:04 +00:00
|
|
|
var (
|
|
|
|
trueBytes = []byte("true")
|
|
|
|
falseBytes = []byte("false")
|
|
|
|
nullBytes = []byte("null")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (b Bool) MarshalJSON() ([]byte, error) {
|
|
|
|
switch b {
|
|
|
|
case "true":
|
|
|
|
return trueBytes, nil
|
|
|
|
case "false":
|
|
|
|
return falseBytes, nil
|
2022-08-09 19:49:02 +00:00
|
|
|
case "", "unset":
|
2020-02-27 17:47:04 +00:00
|
|
|
return nullBytes, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid opt.Bool value %q", string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bool) UnmarshalJSON(j []byte) error {
|
2023-08-29 20:12:49 +00:00
|
|
|
switch string(j) {
|
|
|
|
case "true":
|
2020-02-27 17:47:04 +00:00
|
|
|
*b = "true"
|
2023-08-29 20:12:49 +00:00
|
|
|
case "false":
|
2020-02-27 17:47:04 +00:00
|
|
|
*b = "false"
|
2023-08-29 20:12:49 +00:00
|
|
|
case "null":
|
2022-08-09 19:49:02 +00:00
|
|
|
*b = "unset"
|
2023-08-29 20:12:49 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid opt.Bool value %q", j)
|
2020-02-27 17:47:04 +00:00
|
|
|
}
|
2023-08-29 20:12:49 +00:00
|
|
|
return nil
|
2020-02-27 17:47:04 +00:00
|
|
|
}
|