-
Notifications
You must be signed in to change notification settings - Fork 4
/
int32.go
55 lines (44 loc) · 1.09 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
package null
import (
"database/sql"
"encoding/json"
)
// Int32 is a nullable int32 type based on sql.NullInt32, that supports parsing to/from JSON.
type Int32 struct {
sql.NullInt32
}
// NewInt32 returns a new nullable Int32 object.
// This is equivalent to `null.Int32{sql.NullInt32{Int32: i, Valid: valid}}`.
func NewInt32(i int32, valid bool) Int32 {
return Int32{sql.NullInt32{Int32: i, Valid: valid}}
}
// MarshalJSON implements the encoding json interface.
func (i Int32) MarshalJSON() ([]byte, error) {
if i.Valid {
return json.Marshal(i.Int32)
}
return json.Marshal(nil)
}
// UnmarshalJSON implements the encoding json interface.
func (i *Int32) UnmarshalJSON(data []byte) error {
var value *int32
if err := json.Unmarshal(data, &value); err != nil {
return err
}
if value != nil {
i.SetValid(*value)
} else {
i.SetNil()
}
return nil
}
// SetValid sets the value and valid to true.
func (i *Int32) SetValid(value int32) {
i.Int32 = value
i.Valid = true
}
// SetNil sets the value to default and valid to false.
func (i *Int32) SetNil() {
i.Int32 = 0
i.Valid = false
}