Debian Codegraph @

int.go

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