bool.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package opt defines optional types.
  4. package opt
  5. import (
  6. "fmt"
  7. "strconv"
  8. )
  9. // Bool represents an optional boolean to be JSON-encoded. The string
  10. // is either "true", "false", or the empty string to mean unset.
  11. //
  12. // As a special case, the underlying string may also be the string
  13. // "unset" as as a synonym for the empty string. This lets the
  14. // explicit unset value be exchanged over an encoding/json "omitempty"
  15. // field without it being dropped.
  16. type Bool string
  17. const (
  18. // True is the encoding of an explicit true.
  19. True = Bool("true")
  20. // False is the encoding of an explicit false.
  21. False = Bool("false")
  22. // ExplicitlyUnset is the encoding used by a null
  23. // JSON value. It is a synonym for the empty string.
  24. ExplicitlyUnset = Bool("unset")
  25. // Empty means the Bool is unset and it's neither
  26. // true nor false.
  27. Empty = Bool("")
  28. )
  29. // NewBool constructs a new Bool value equal to b. The returned Bool is set,
  30. // unless Set("") or Clear() methods are called.
  31. func NewBool(b bool) Bool {
  32. return Bool(strconv.FormatBool(b))
  33. }
  34. func (b *Bool) Set(v bool) {
  35. *b = Bool(strconv.FormatBool(v))
  36. }
  37. func (b *Bool) Clear() { *b = "" }
  38. func (b Bool) Get() (v bool, ok bool) {
  39. switch b {
  40. case "true":
  41. return true, true
  42. case "false":
  43. return false, true
  44. default:
  45. return false, false
  46. }
  47. }
  48. // Scan implements database/sql.Scanner.
  49. func (b *Bool) Scan(src any) error {
  50. if src == nil {
  51. *b = ""
  52. return nil
  53. }
  54. switch src := src.(type) {
  55. case bool:
  56. if src {
  57. *b = True
  58. } else {
  59. *b = False
  60. }
  61. return nil
  62. case int64:
  63. if src == 0 {
  64. *b = False
  65. } else {
  66. *b = True
  67. }
  68. return nil
  69. default:
  70. return fmt.Errorf("opt.Bool.Scan: invalid type %T: %v", src, src)
  71. }
  72. }
  73. // Normalized returns the normalized form of b, mapping "unset" to ""
  74. // and leaving other values unchanged.
  75. func (b Bool) Normalized() Bool {
  76. switch b {
  77. case ExplicitlyUnset:
  78. return Empty
  79. default:
  80. return b
  81. }
  82. }
  83. // EqualBool reports whether b is equal to v.
  84. // If b is empty or not a valid bool, it reports false.
  85. func (b Bool) EqualBool(v bool) bool {
  86. p, ok := b.Get()
  87. return ok && p == v
  88. }
  89. var (
  90. trueBytes = []byte(True)
  91. falseBytes = []byte(False)
  92. nullBytes = []byte("null")
  93. )
  94. func (b Bool) MarshalJSON() ([]byte, error) {
  95. switch b {
  96. case True:
  97. return trueBytes, nil
  98. case False:
  99. return falseBytes, nil
  100. case Empty, ExplicitlyUnset:
  101. return nullBytes, nil
  102. }
  103. return nil, fmt.Errorf("invalid opt.Bool value %q", string(b))
  104. }
  105. func (b *Bool) UnmarshalJSON(j []byte) error {
  106. switch string(j) {
  107. case "true":
  108. *b = True
  109. case "false":
  110. *b = False
  111. case "null":
  112. *b = ExplicitlyUnset
  113. default:
  114. return fmt.Errorf("invalid opt.Bool value %q", j)
  115. }
  116. return nil
  117. }
  118. // BoolFlag is a wrapper for Bool that implements [flag.Value].
  119. type BoolFlag struct {
  120. *Bool
  121. }
  122. // Set the value of b, using any value supported by [strconv.ParseBool].
  123. func (b *BoolFlag) Set(s string) error {
  124. v, err := strconv.ParseBool(s)
  125. if err != nil {
  126. return err
  127. }
  128. b.Bool.Set(v)
  129. return nil
  130. }
  131. // String returns "true" or "false" if the value is set, or an empty string otherwise.
  132. func (b *BoolFlag) String() string {
  133. if b == nil || b.Bool == nil {
  134. return ""
  135. }
  136. if v, ok := b.Bool.Get(); ok {
  137. return strconv.FormatBool(v)
  138. }
  139. return ""
  140. }