-
Notifications
You must be signed in to change notification settings - Fork 222
/
null_int64.go
85 lines (76 loc) · 1.53 KB
/
null_int64.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
package xorm
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
type NullInt64 struct {
Int64 int64
Valid bool
}
func (ni NullInt64) Ptr() *int64 {
if !ni.Valid {
return nil
}
return &ni.Int64
}
func (ni NullInt64) ValueOrZero() int64 {
if !ni.Valid {
return 0
}
return ni.Int64
}
func (ni NullInt64) IsNil() bool {
return !ni.Valid
}
func (ni *NullInt64) UnmarshalJSON(data []byte) error {
var err error
var v interface{}
if err = json.Unmarshal(data, &v); err != nil {
return err
}
switch x := v.(type) {
case float64:
err = json.Unmarshal(data, &ni.Int64)
case string:
str := string(x)
if len(str) == 0 {
ni.Valid = false
return nil
}
ni.Int64, err = strconv.ParseInt(str, 10, 64)
case map[string]interface{}:
err = json.Unmarshal(data, &ni)
case nil:
ni.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type xorm.NullInt64", reflect.TypeOf(v).Name())
}
ni.Valid = err == nil
return err
}
func (ni *NullInt64) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
ni.Valid = false
return nil
}
var err error
ni.Int64, err = strconv.ParseInt(string(text), 10, 64)
ni.Valid = err == nil
return err
}
func (ni NullInt64) MarshalJSON() ([]byte, error) {
if !ni.Valid {
return []byte("null"), nil
}
return []byte(strconv.FormatInt(ni.Int64, 10)), nil
}
func (ni NullInt64) MarshalText() ([]byte, error) {
if !ni.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatInt(ni.Int64, 10)), nil
}