forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
bool.go
48 lines (43 loc) · 1.09 KB
/
bool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2014, Greg Roseberry
// All rights reserved.
package null
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
)
// Bool is a nullable bool. It does not consider false values
// to be null. It will decode to null, not false, if null.
type Bool struct {
sql.NullBool
}
// UnmarshalJSON implements json.Unmarshaler. It supports
// number and null input. 0 will not be considered a null
// Bool. It also supports unmarshalling a sql.NullBool.
func (b *Bool) UnmarshalJSON(data []byte) error {
var v interface{}
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
switch x := v.(type) {
case bool:
b.Bool = x
case map[string]interface{}:
err = json.Unmarshal(data, &b.NullBool)
case nil:
b.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", reflect.TypeOf(v).Name())
}
b.Valid = err == nil
return err
}
// IsZero returns true for invalid Bools, for future omitempty
// support (Go 1.4?). A non-null Bool with a 0 value will not be
// considered zero.
func (b Bool) IsZero() bool {
return !b.Valid
}