Debian Codegraph @

zero/int.go

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