-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnull_time.go
155 lines (133 loc) · 3.38 KB
/
null_time.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
package typ
import (
"database/sql/driver"
"encoding/json"
"time"
)
// TimeCommon represents a time.Time that may be null.
type TimeCommon struct {
P *time.Time
Error error
}
// Set saves value into current struct
func (n *TimeCommon) Set(value time.Time) {
n.P = &value
}
// V returns value of underlying type if it was set, otherwise default value
func (n TimeCommon) V() time.Time {
if n.P == nil {
return time.Time{}
}
return *n.P
}
// Present determines whether a value has been set
func (n TimeCommon) Present() bool {
return n.P != nil
}
// Valid determines whether a value has been valid
func (n TimeCommon) Valid() bool {
return n.Err() == nil
}
// Value implements the sql driver Valuer interface.
func (n TimeCommon) Value() (driver.Value, error) {
return n.V(), nil
}
// Scan implements the sql Scanner interface.
func (n *TimeCommon) Scan(value interface{}) error {
n.Error = nil
var tv time.Time
tv, ok := value.(time.Time)
if !ok {
n.Error = ErrInvalidArgument
}
n.P = &tv
return n.Err()
}
// UnmarshalJSON implements the json Unmarshaler interface.
func (n *TimeCommon) UnmarshalJSON(b []byte) error {
v := n.V()
n.Error = v.UnmarshalJSON(b)
return n.Err()
}
// MarshalJSON implements the json Marshaler interface.
func (n TimeCommon) MarshalJSON() ([]byte, error) {
return n.V().MarshalJSON()
}
// Typ returns new instance with himself value.
// If current value is invalid, nil *Type returned
func (n TimeCommon) Typ(options ...Option) *Type {
if n.Err() != nil {
return NewType(nil, n.Err())
}
return NewType(n.V(), n.Err(), options...)
}
// Err returns underlying error.
func (n TimeCommon) Err() error {
return n.Error
}
// TimeAccessor accessor of time.Time type.
type TimeAccessor interface {
Common
V() time.Time
Set(value time.Time)
Clone() TimeAccessor
}
// NullTime represents a time.Time that may be null.
type NullTime struct {
TimeCommon
}
// Value implements the sql driver Valuer interface.
func (n NullTime) Value() (driver.Value, error) {
if n.Err() != nil || !n.Present() {
return nil, n.Err()
}
return n.TimeCommon.Value()
}
// MarshalJSON implements the json Marshaler interface.
func (n NullTime) MarshalJSON() ([]byte, error) {
if n.Err() != nil || !n.Present() {
return json.Marshal(nil)
}
return n.TimeCommon.MarshalJSON()
}
// Clone returns new instance of NullTime with preserved value & error
func (n NullTime) Clone() TimeAccessor {
nv := &NullTime{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NTime returns NullTime under TimeAccessor from time.Time
func NTime(value time.Time) TimeAccessor {
return &NullTime{TimeCommon{P: &value}}
}
// NotNullTime represents a time.Time that may be null.
type NotNullTime struct {
TimeCommon
}
// Clone returns new instance of NotNullTime with preserved value & error
func (n NotNullTime) Clone() TimeAccessor {
nv := &NotNullTime{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NNTime returns NullTime under TimeAccessor from time.Time
func NNTime(value time.Time) TimeAccessor {
return &NotNullTime{TimeCommon{P: &value}}
}
// TimeSlice returns slice of time.Time with filled values from slice of TimeAccessor
func TimeSlice(null []TimeAccessor, valid bool) []time.Time {
slice := make([]time.Time, 0, len(null))
for _, v := range null {
if valid && v.Err() != nil {
continue
}
slice = append(slice, v.V())
}
return slice
}