Debian Codegraph @

time.go

raw symbols SCIP
1 package null
2
3 import (
4 "database/sql/driver"
5 "encoding/json"
6 "fmt"
7 "reflect"
8 "time"
9 )
10
11 // Time is a nullable time.Time. It supports SQL and JSON serialization.
12 // It will marshal to null if null.
13 type Time struct {
14 Time time.Time
15 Valid bool
16 }
17
18 // Scan implements the Scanner interface.
19 func (t *Time) Scan(value interface{}) error {
20 var err error
21 switch x := value.(type) {
22 case time.Time:
23 t.Time = x
24 case nil:
25 t.Valid = false
26 return nil
27 default:
28 err = fmt.Errorf("null: cannot scan type %T into null.Time: %v", value, value)
29 }
30 t.Valid = err == nil
31 return err
32 }
33
34 // Value implements the driver Valuer interface.
35 func (t Time) Value() (driver.Value, error) {
36 if !t.Valid {
37 return nil, nil
38 }
39 return t.Time, nil
40 }
41
42 // NewTime creates a new Time.
43 func NewTime(t time.Time, valid bool) Time {
44 return Time{
45 Time: t,
46 Valid: valid,
47 }
48 }
49
50 // TimeFrom creates a new Time that will always be valid.
51 func TimeFrom(t time.Time) Time {
52 return NewTime(t, true)
53 }
54
55 // TimeFromPtr creates a new Time that will be null if t is nil.
56 func TimeFromPtr(t *time.Time) Time {
57 if t == nil {
58 return NewTime(time.Time{}, false)
59 }
60 return NewTime(*t, true)
61 }
62
63 // ValueOrZero returns the inner value if valid, otherwise zero.
64 func (t Time) ValueOrZero() time.Time {
65 if !t.Valid {
66 return time.Time{}
67 }
68 return t.Time
69 }
70
71 // MarshalJSON implements json.Marshaler.
72 // It will encode null if this time is null.
73 func (t Time) MarshalJSON() ([]byte, error) {
74 if !t.Valid {
75 return []byte("null"), nil
76 }
77 return t.Time.MarshalJSON()
78 }
79
80 // UnmarshalJSON implements json.Unmarshaler.
81 // It supports string, object (e.g. pq.NullTime and friends)
82 // and null input.
83 func (t *Time) UnmarshalJSON(data []byte) error {
84 var err error
85 var v interface{}
86 if err = json.Unmarshal(data, &v); err != nil {
87 return err
88 }
89 switch x := v.(type) {
90 case string:
91 err = t.Time.UnmarshalJSON(data)
92 case map[string]interface{}:
93 ti, tiOK := x["Time"].(string)
94 valid, validOK := x["Valid"].(bool)
95 if !tiOK || !validOK {
96 return fmt.Errorf(`json: unmarshalling object into Go value of type null.Time requires key "Time" to be of type string and key "Valid" to be of type bool; found %T and %T, respectively`, x["Time"], x["Valid"])
97 }
98 err = t.Time.UnmarshalText([]byte(ti))
99 t.Valid = valid
100 return err
101 case nil:
102 t.Valid = false
103 return nil
104 default:
105 err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name())
106 }
107 t.Valid = err == nil
108 return err
109 }
110
111 func (t Time) MarshalText() ([]byte, error) {
112 if !t.Valid {
113 return []byte("null"), nil
114 }
115 return t.Time.MarshalText()
116 }
117
118 func (t *Time) UnmarshalText(text []byte) error {
119 str := string(text)
120 if str == "" || str == "null" {
121 t.Valid = false
122 return nil
123 }
124 if err := t.Time.UnmarshalText(text); err != nil {
125 return err
126 }
127 t.Valid = true
128 return nil
129 }
130
131 // SetValid changes this Time's value and sets it to be non-null.
132 func (t *Time) SetValid(v time.Time) {
133 t.Time = v
134 t.Valid = true
135 }
136
137 // Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null.
138 func (t Time) Ptr() *time.Time {
139 if !t.Valid {
140 return nil
141 }
142 return &t.Time
143 }
144
145 // IsZero returns true for invalid Times, hopefully for future omitempty support.
146 // A non-null Time with a zero value will not be considered zero.
147 func (t Time) IsZero() bool {
148 return !t.Valid
149 }
150
151 // Equal returns true if both Time objects encode the same time or are both null.
152 // Two times can be equal even if they are in different locations.
153 // For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
154 func (t Time) Equal(other Time) bool {
155 return t.Valid == other.Valid && (!t.Valid || t.Time.Equal(other.Time))
156 }
157
158 // ExactEqual returns true if both Time objects are equal or both null.
159 // ExactEqual returns false for times that are in different locations or
160 // have a different monotonic clock reading.
161 func (t Time) ExactEqual(other Time) bool {
162 return t.Valid == other.Valid && (!t.Valid || t.Time == other.Time)
163 }