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