-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathint.go
122 lines (110 loc) · 2.59 KB
/
int.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
package gomu
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
// Int is a nullable int.
type Int struct {
Int64 int64
Null bool
Valid bool
}
// NewInt creates a new Int.
func NewInt(i int64, n bool, valid bool) Int {
return Int{
Int64: i,
Null: n,
Valid: valid,
}
}
// IntFrom creates a new Int that will never be blank.
func IntFrom(i int64) Int {
return NewInt(i, false, true)
}
// IntFromPtr creates a new Int that be null if i is nil.
func IntFromPtr(i *int64) Int {
if i == nil {
return NewInt(0, true, true)
}
return NewInt(*i, false, true)
}
// UnmarshalJSON implements json.Unmarshaler.
func (i *Int) UnmarshalJSON(data []byte) (err error) {
var v interface{}
if err = json.Unmarshal(data, &v); err != nil {
return err
}
switch v.(type) {
case float64:
err = json.Unmarshal(data, &i.Int64)
case nil:
i.Null = true
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type gomu.Int", reflect.TypeOf(v).Name())
}
i.Valid = err == nil
return
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int) UnmarshalText(text []byte) (err error) {
if text == nil {
return
}
str := string(text)
if str == "" || str == "null" {
i.Null = true
i.Valid = true
return
}
i.Int64, err = strconv.ParseInt(str, 10, 64)
i.Valid = err == nil
return
}
// MarshalJSON implements json.Marshaler.
func (i Int) MarshalJSON() ([]byte, error) {
if i.Null || !i.Valid {
return []byte("null"), nil
}
return []byte(strconv.FormatInt(i.Int64, 10)), nil
}
// MarshalText implements encoding.TextMarshaler.
func (i Int) MarshalText() ([]byte, error) {
if !i.Valid {
return nil, nil
}
if i.Null {
return []byte("null"), nil
}
return []byte(strconv.FormatInt(i.Int64, 10)), nil
}
// SetValid changes this Int value and also sets Valid to be true.
func (i *Int) SetValid(n int64) {
i.Int64 = n
i.Null = false
i.Valid = true
}
// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null or not valid.
func (i Int) Ptr() *int64 {
if i.Null || !i.Valid {
return nil
}
return &i.Int64
}
// Scan implements database/sql.Scanner.
func (i *Int) Scan(value interface{}) (err error) {
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i.Int64 = rv.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i.Int64 = int64(rv.Uint())
case reflect.Float32, reflect.Float64:
i.Int64 = int64(rv.Float())
default:
err = fmt.Errorf("gomu: cannot scan type %T into gomu.Int: %v", value, value)
}
i.Valid = err == nil
return
}