forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathint32.go
169 lines (147 loc) · 3.39 KB
/
int32.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package null
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"github.com/volatiletech/null/convert"
"github.com/volatiletech/sqlboiler/randomize"
)
// Int32 is an nullable int32.
type Int32 struct {
Int32 int32
Valid bool
}
// NewInt32 creates a new Int32
func NewInt32(i int32, valid bool) Int32 {
return Int32{
Int32: i,
Valid: valid,
}
}
// Int32From creates a new Int32 that will always be valid.
func Int32From(i int32) Int32 {
return NewInt32(i, true)
}
// Int32FromPtr creates a new Int32 that be null if i is nil.
func Int32FromPtr(i *int32) Int32 {
if i == nil {
return NewInt32(0, false)
}
return NewInt32(*i, true)
}
// UnmarshalJSON implements json.Unmarshaler.
func (i *Int32) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, NullBytes) {
i.Valid = false
i.Int32 = 0
return nil
}
var err error
var v interface{}
if err = json.Unmarshal(data, &v); err != nil {
return err
}
var r int64
switch x := v.(type) {
case float64:
// Unmarshal again, directly to uint64, to avoid intermediate float64
err = json.Unmarshal(data, &r)
case string:
str := string(x)
if len(str) == 0 {
i.Valid = false
return nil
}
r, err = strconv.ParseInt(str, 10, 32)
case nil:
i.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Int32", reflect.TypeOf(v).Name())
}
if r > math.MaxInt32 {
return fmt.Errorf("json: %d overflows max int32 value", r)
}
i.Int32 = int32(r)
i.Valid = (err == nil) && (i.Int32 != 0)
return err
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int32) UnmarshalText(text []byte) error {
if text == nil || len(text) == 0 {
i.Valid = false
return nil
}
var err error
res, err := strconv.ParseInt(string(text), 10, 32)
i.Valid = err == nil
if i.Valid {
i.Int32 = int32(res)
}
return err
}
// MarshalJSON implements json.Marshaler.
func (i Int32) MarshalJSON() ([]byte, error) {
if !i.Valid {
return NullBytes, nil
}
return []byte(strconv.FormatInt(int64(i.Int32), 10)), nil
}
// MarshalText implements encoding.TextMarshaler.
func (i Int32) MarshalText() ([]byte, error) {
if !i.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatInt(int64(i.Int32), 10)), nil
}
// SetValid changes this Int32's value and also sets it to be non-null.
func (i *Int32) SetValid(n int32) {
i.Int32 = n
i.Valid = true
}
// Ptr returns a pointer to this Int32's value, or a nil pointer if this Int32 is null.
func (i Int32) Ptr() *int32 {
if !i.Valid {
return nil
}
return &i.Int32
}
// IsZero returns true for invalid Int32's, for future omitempty support (Go 1.4?)
func (i Int32) IsZero() bool {
return !i.Valid
}
// Scan implements the Scanner interface.
func (i *Int32) Scan(value interface{}) error {
if value == nil {
i.Int32, i.Valid = 0, false
return nil
}
i.Valid = true
return convert.ConvertAssign(&i.Int32, value)
}
// Value implements the driver Valuer interface.
func (i Int32) Value() (driver.Value, error) {
if !i.Valid {
return nil, nil
}
return int64(i.Int32), nil
}
// Randomize for sqlboiler
func (i *Int32) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
if shouldBeNull {
i.Int32 = 0
i.Valid = false
} else {
val, ok := randomize.MediumInt(nextInt, fieldType)
if ok {
i.Int32 = val
} else {
i.Int32 = int32(nextInt() % math.MaxInt32)
}
i.Valid = true
}
}