forked from kernle32dll/nullable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
48 lines (38 loc) · 957 Bytes
/
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
package nullable
import (
"bytes"
"encoding/json"
"time"
"github.com/go-openapi/strfmt"
)
// Time represents a time that may be null or not
// present in json at all.
type Time struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid time
Value time.Time
}
// Returns nil if not present or valid. Otherwise it will
// return a pointer to the value.
func (t *Time) Ptr() *time.Time {
if t.Present && t.Valid {
return &t.Value
}
return nil
}
// UnmarshalJSON implements json.Marshaler interface.
func (t *Time) UnmarshalJSON(data []byte) error {
t.Present = true
if bytes.Equal(data, null) {
return nil
}
if err := json.Unmarshal(data, &t.Value); err != nil {
return err
}
t.Valid = true
return nil
}
// Validate implements runtime.Validateable interface for go-swagger generation.
func (t *Time) Validate(formats strfmt.Registry) error {
return nil
}