| 1 |
package zero |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql/driver" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"reflect" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type Time struct { |
| 15 |
Time time.Time |
| 16 |
Valid bool |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func (t *Time) Scan(value interface{}) error { |
| 21 |
var err error |
| 22 |
switch x := value.(type) { |
| 23 |
case time.Time: |
| 24 |
t.Time = x |
| 25 |
case nil: |
| 26 |
t.Valid = false |
| 27 |
return nil |
| 28 |
default: |
| 29 |
err = fmt.Errorf("null: cannot scan type %T into null.Time: %v", value, value) |
| 30 |
} |
| 31 |
t.Valid = err == nil |
| 32 |
return err |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func (t Time) Value() (driver.Value, error) { |
| 37 |
if !t.Valid { |
| 38 |
return nil, nil |
| 39 |
} |
| 40 |
return t.Time, nil |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func NewTime(t time.Time, valid bool) Time { |
| 45 |
return Time{ |
| 46 |
Time: t, |
| 47 |
Valid: valid, |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func TimeFrom(t time.Time) Time { |
| 54 |
return NewTime(t, !t.IsZero()) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
func TimeFromPtr(t *time.Time) Time { |
| 60 |
if t == nil { |
| 61 |
return NewTime(time.Time{}, false) |
| 62 |
} |
| 63 |
return TimeFrom(*t) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (t Time) ValueOrZero() time.Time { |
| 68 |
if !t.Valid { |
| 69 |
return time.Time{} |
| 70 |
} |
| 71 |
return t.Time |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (t Time) MarshalJSON() ([]byte, error) { |
| 78 |
if !t.Valid { |
| 79 |
return (time.Time{}).MarshalJSON() |
| 80 |
} |
| 81 |
return t.Time.MarshalJSON() |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func (t *Time) UnmarshalJSON(data []byte) error { |
| 88 |
var err error |
| 89 |
var v interface{} |
| 90 |
if err = json.Unmarshal(data, &v); err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
switch x := v.(type) { |
| 94 |
case string: |
| 95 |
var ti time.Time |
| 96 |
if err = ti.UnmarshalJSON(data); err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
*t = TimeFrom(ti) |
| 100 |
return nil |
| 101 |
case map[string]interface{}: |
| 102 |
ti, tiOK := x["Time"].(string) |
| 103 |
valid, validOK := x["Valid"].(bool) |
| 104 |
if !tiOK || !validOK { |
| 105 |
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"]) |
| 106 |
} |
| 107 |
err = t.Time.UnmarshalText([]byte(ti)) |
| 108 |
t.Valid = valid |
| 109 |
return err |
| 110 |
case nil: |
| 111 |
t.Valid = false |
| 112 |
return nil |
| 113 |
default: |
| 114 |
return fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name()) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func (t Time) MarshalText() ([]byte, error) { |
| 119 |
ti := t.Time |
| 120 |
if !t.Valid { |
| 121 |
ti = time.Time{} |
| 122 |
} |
| 123 |
return ti.MarshalText() |
| 124 |
} |
| 125 |
|
| 126 |
func (t *Time) UnmarshalText(text []byte) error { |
| 127 |
str := string(text) |
| 128 |
if str == "" || str == "null" { |
| 129 |
t.Valid = false |
| 130 |
return nil |
| 131 |
} |
| 132 |
if err := t.Time.UnmarshalText(text); err != nil { |
| 133 |
return err |
| 134 |
} |
| 135 |
t.Valid = true |
| 136 |
return nil |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
func (t *Time) SetValid(v time.Time) { |
| 142 |
t.Time = v |
| 143 |
t.Valid = true |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
func (t Time) Ptr() *time.Time { |
| 149 |
if !t.Valid { |
| 150 |
return nil |
| 151 |
} |
| 152 |
return &t.Time |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
func (t Time) IsZero() bool { |
| 157 |
return !t.Valid || t.Time.IsZero() |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
func (t Time) Equal(other Time) bool { |
| 164 |
return t.ValueOrZero().Equal(other.ValueOrZero()) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
func (t Time) ExactEqual(other Time) bool { |
| 171 |
return t.ValueOrZero() == other.ValueOrZero() |
| 172 |
} |