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 // UnmarshalJSON implements json.Unmarshaler.
43 // It supports number and null input.
44 // 0 will be considered a null Int.
45 // It also supports unmarshalling a sql.NullInt64.
46 func (i *Int) UnmarshalJSON(data []byte) error {
47 var err error
48 var v interface{}
49 if err = json.Unmarshal(data, &v); err != nil {
50 return err
51 }
52 switch v.(type) {
53 case float64:
54 // Unmarshal again, directly to int64, to avoid intermediate float64
55 err = json.Unmarshal(data, &i.Int64)
56 case map[string]interface{}:
57 err = json.Unmarshal(data, &i.NullInt64)
58 case nil:
59 i.Valid = false
60 return nil
61 default:
62 err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int", reflect.TypeOf(v).Name())
63 }
64 i.Valid = (err == nil) && (i.Int64 != 0)
65 return err
66 }
67
68 // UnmarshalText implements encoding.TextUnmarshaler.
69 // It will unmarshal to a null Int if the input is a blank, zero, or not an integer.
70 // It will return an error if the input is not an integer, blank, or "null".
71 func (i *Int) UnmarshalText(text []byte) error {
72 str := string(text)
73 if str == "" || str == "null" {
74 i.Valid = false
75 return nil
76 }
77 var err error
78 i.Int64, err = strconv.ParseInt(string(text), 10, 64)
79 i.Valid = (err == nil) && (i.Int64 != 0)
80 return err
81 }
82
83 // MarshalJSON implements json.Marshaler.
84 // It will encode 0 if this Int is null.
85 func (i Int) MarshalJSON() ([]byte, error) {
86 n := i.Int64
87 if !i.Valid {
88 n = 0
89 }
90 return []byte(strconv.FormatInt(n, 10)), nil
91 }
92
93 // MarshalText implements encoding.TextMarshaler.
94 // It will encode a zero if this Int is null.
95 func (i Int) MarshalText() ([]byte, error) {
96 n := i.Int64
97 if !i.Valid {
98 n = 0
99 }
100 return []byte(strconv.FormatInt(n, 10)), nil
101 }
102
103 // SetValid changes this Int's value and also sets it to be non-null.
104 func (i *Int) SetValid(n int64) {
105 i.Int64 = n
106 i.Valid = true
107 }
108
109 // Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.
110 func (i Int) Ptr() *int64 {
111 if !i.Valid {
112 return nil
113 }
114 return &i.Int64
115 }
116
117 // IsZero returns true for null or zero Ints, for future omitempty support (Go 1.4?)
118 func (i Int) IsZero() bool {
119 return !i.Valid || i.Int64 == 0
120 }