-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
88 lines (66 loc) · 1.48 KB
/
types.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
package resellerclub
import (
"encoding/json"
"time"
)
type Bool bool
type Int64 int64
type Float64 float64
type Time time.Time
func (v *Bool) UnmarshalJSON(data []byte) error {
var err error
var b bool
if len(data) > 1 && data[0] == '"' && data[len(data)-1] == '"' {
err = json.Unmarshal(data[1:len(data)-1], &b)
} else {
err = json.Unmarshal(data, &b)
}
*v = Bool(b)
return err
}
func (v *Int64) UnmarshalJSON(data []byte) error {
var err error
var i int64
if len(data) > 1 && data[0] == '"' && data[len(data)-1] == '"' {
err = json.Unmarshal(data[1:len(data)-1], &i)
} else {
err = json.Unmarshal(data, &i)
}
*v = Int64(i)
return err
}
func (v *Float64) UnmarshalJSON(data []byte) error {
var err error
var f float64
if len(data) > 1 && data[0] == '"' && data[len(data)-1] == '"' {
err = json.Unmarshal(data[1:len(data)-1], &f)
} else {
err = json.Unmarshal(data, &f)
}
*v = Float64(f)
return err
}
func (v *Time) UnmarshalJSON(data []byte) error {
var err error
var t time.Time
var i int64
if len(data) > 1 && data[0] == '"' && data[len(data)-1] == '"' {
err = json.Unmarshal(data[1:len(data)-1], &i)
if err != nil {
t, err = time.Parse("2006-01-02 15:04:05.999999999-07", string(data[1:len(data)-1]))
}
if err != nil {
t, err = time.Parse(time.RFC3339, string(data[1:len(data)-1]))
}
} else {
err = json.Unmarshal(data, &i)
}
if err != nil {
return err
}
if t.IsZero() {
t = time.Unix(i, 0)
}
*v = Time(t)
return nil
}