Debian Codegraph @

zero/bool.go

raw symbols SCIP
1 package zero
2
3 import (
4 "database/sql"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "reflect"
9 )
10
11 // Bool is a nullable bool. False input is considered null.
12 // JSON marshals to false if null.
13 // Considered null to SQL unmarshaled from a false value.
14 type Bool struct {
15 sql.NullBool
16 }
17
18 // NewBool creates a new Bool
19 func NewBool(b bool, valid bool) Bool {
20 return Bool{
21 NullBool: sql.NullBool{
22 Bool: b,
23 Valid: valid,
24 },
25 }
26 }
27
28 // BoolFrom creates a new Bool that will be null if false.
29 func BoolFrom(b bool) Bool {
30 return NewBool(b, b)
31 }
32
33 // BoolFromPtr creates a new Bool that be null if b is nil.
34 func BoolFromPtr(b *bool) Bool {
35 if b == nil {
36 return NewBool(false, false)
37 }
38 return NewBool(*b, true)
39 }
40
41 // ValueOrZero returns the inner value if valid, otherwise false.
42 func (b Bool) ValueOrZero() bool {
43 return b.Valid && b.Bool
44 }
45
46 // UnmarshalJSON implements json.Unmarshaler.
47 // "false" will be considered a null Bool.
48 // It also supports unmarshalling a sql.NullBool.
49 func (b *Bool) UnmarshalJSON(data []byte) error {
50 var err error
51 var v interface{}
52 if err = json.Unmarshal(data, &v); err != nil {
53 return err
54 }
55 switch x := v.(type) {
56 case bool:
57 b.Bool = x
58 case map[string]interface{}:
59 err = json.Unmarshal(data, &b.NullBool)
60 case nil:
61 b.Valid = false
62 return nil
63 default:
64 err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Bool", reflect.TypeOf(v).Name())
65 }
66 b.Valid = (err == nil) && b.Bool
67 return err
68 }
69
70 // UnmarshalText implements encoding.TextUnmarshaler.
71 // It will unmarshal to a null Bool if the input is a false or not a bool.
72 // It will return an error if the input is not a float, blank, or "null".
73 func (b *Bool) UnmarshalText(text []byte) error {
74 str := string(text)
75 switch str {
76 case "", "null":
77 b.Valid = false
78 return nil
79 case "true":
80 b.Bool = true
81 case "false":
82 b.Bool = false
83 default:
84 b.Valid = false
85 return errors.New("invalid input:" + str)
86 }
87 b.Valid = b.Bool
88 return nil
89 }
90
91 // MarshalJSON implements json.Marshaler.
92 // It will encode null if this Bool is null.
93 func (b Bool) MarshalJSON() ([]byte, error) {
94 if !b.Valid || !b.Bool {
95 return []byte("false"), nil
96 }
97 return []byte("true"), nil
98 }
99
100 // MarshalText implements encoding.TextMarshaler.
101 // It will encode a zero if this Bool is null.
102 func (b Bool) MarshalText() ([]byte, error) {
103 if !b.Valid || !b.Bool {
104 return []byte("false"), nil
105 }
106 return []byte("true"), nil
107 }
108
109 // SetValid changes this Bool's value and also sets it to be non-null.
110 func (b *Bool) SetValid(v bool) {
111 b.Bool = v
112 b.Valid = true
113 }
114
115 // Ptr returns a poBooler to this Bool's value, or a nil poBooler if this Bool is null.
116 func (b Bool) Ptr() *bool {
117 if !b.Valid {
118 return nil
119 }
120 return &b.Bool
121 }
122
123 // IsZero returns true for null or zero Bools, for future omitempty support (Go 1.4?)
124 func (b Bool) IsZero() bool {
125 return !b.Valid || !b.Bool
126 }
127
128 // Equal returns true if both booleans are true and valid, or if both booleans are either false or invalid.
129 func (b Bool) Equal(other Bool) bool {
130 return b.ValueOrZero() == other.ValueOrZero()
131 }