Debian Codegraph @

bool.go

raw symbols SCIP
1 package null
2
3 import (
4 "database/sql"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "reflect"
9 )
10
11 // Bool is a nullable bool.
12 // It does not consider false values to be null.
13 // It will decode to null, not false, if null.
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 always be valid.
29 func BoolFrom(b bool) Bool {
30 return NewBool(b, true)
31 }
32
33 // BoolFromPtr creates a new Bool that will be null if f 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 // UnmarshalJSON implements json.Unmarshaler.
42 // It supports number and null input.
43 // 0 will not be considered a null Bool.
44 // It also supports unmarshalling a sql.NullBool.
45 func (b *Bool) UnmarshalJSON(data []byte) error {
46 var err error
47 var v interface{}
48 if err = json.Unmarshal(data, &v); err != nil {
49 return err
50 }
51 switch x := v.(type) {
52 case bool:
53 b.Bool = x
54 case map[string]interface{}:
55 err = json.Unmarshal(data, &b.NullBool)
56 case nil:
57 b.Valid = false
58 return nil
59 default:
60 err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", reflect.TypeOf(v).Name())
61 }
62 b.Valid = err == nil
63 return err
64 }
65
66 // UnmarshalText implements encoding.TextUnmarshaler.
67 // It will unmarshal to a null Bool if the input is a blank or not an integer.
68 // It will return an error if the input is not an integer, blank, or "null".
69 func (b *Bool) UnmarshalText(text []byte) error {
70 str := string(text)
71 switch str {
72 case "", "null":
73 b.Valid = false
74 return nil
75 case "true":
76 b.Bool = true
77 case "false":
78 b.Bool = false
79 default:
80 b.Valid = false
81 return errors.New("invalid input:" + str)
82 }
83 b.Valid = true
84 return nil
85 }
86
87 // MarshalJSON implements json.Marshaler.
88 // It will encode null if this Bool is null.
89 func (b Bool) MarshalJSON() ([]byte, error) {
90 if !b.Valid {
91 return []byte("null"), nil
92 }
93 if !b.Bool {
94 return []byte("false"), nil
95 }
96 return []byte("true"), nil
97 }
98
99 // MarshalText implements encoding.TextMarshaler.
100 // It will encode a blank string if this Bool is null.
101 func (b Bool) MarshalText() ([]byte, error) {
102 if !b.Valid {
103 return []byte{}, nil
104 }
105 if !b.Bool {
106 return []byte("false"), nil
107 }
108 return []byte("true"), nil
109 }
110
111 // SetValid changes this Bool's value and also sets it to be non-null.
112 func (b *Bool) SetValid(v bool) {
113 b.Bool = v
114 b.Valid = true
115 }
116
117 // Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null.
118 func (b Bool) Ptr() *bool {
119 if !b.Valid {
120 return nil
121 }
122 return &b.Bool
123 }
124
125 // IsZero returns true for invalid Bools, for future omitempty support (Go 1.4?)
126 // A non-null Bool with a 0 value will not be considered zero.
127 func (b Bool) IsZero() bool {
128 return !b.Valid
129 }