| 1 |
package null |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql/driver" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"reflect" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
type Time struct { |
| 14 |
Time time.Time |
| 15 |
Valid bool |
| 16 |
} |
| 17 |
|
| 18 |
|
| 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 |
|
| 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 |
|
| 43 |
func NewTime(t time.Time, valid bool) Time { |
| 44 |
return Time{ |
| 45 |
Time: t, |
| 46 |
Valid: valid, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func TimeFrom(t time.Time) Time { |
| 52 |
return NewTime(t, true) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 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 |
|
| 64 |
|
| 65 |
func (t Time) MarshalJSON() ([]byte, error) { |
| 66 |
if !t.Valid { |
| 67 |
return []byte("null"), nil |
| 68 |
} |
| 69 |
return t.Time.MarshalJSON() |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
func (t *Time) UnmarshalJSON(data []byte) error { |
| 76 |
var err error |
| 77 |
var v interface{} |
| 78 |
if err = json.Unmarshal(data, &v); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
switch x := v.(type) { |
| 82 |
case string: |
| 83 |
err = t.Time.UnmarshalJSON(data) |
| 84 |
case map[string]interface{}: |
| 85 |
ti, tiOK := x["Time"].(string) |
| 86 |
valid, validOK := x["Valid"].(bool) |
| 87 |
if !tiOK || !validOK { |
| 88 |
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"]) |
| 89 |
} |
| 90 |
err = t.Time.UnmarshalText([]byte(ti)) |
| 91 |
t.Valid = valid |
| 92 |
return err |
| 93 |
case nil: |
| 94 |
t.Valid = false |
| 95 |
return nil |
| 96 |
default: |
| 97 |
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name()) |
| 98 |
} |
| 99 |
t.Valid = err == nil |
| 100 |
return err |
| 101 |
} |
| 102 |
|
| 103 |
func (t Time) MarshalText() ([]byte, error) { |
| 104 |
if !t.Valid { |
| 105 |
return []byte("null"), nil |
| 106 |
} |
| 107 |
return t.Time.MarshalText() |
| 108 |
} |
| 109 |
|
| 110 |
func (t *Time) UnmarshalText(text []byte) error { |
| 111 |
str := string(text) |
| 112 |
if str == "" || str == "null" { |
| 113 |
t.Valid = false |
| 114 |
return nil |
| 115 |
} |
| 116 |
if err := t.Time.UnmarshalText(text); err != nil { |
| 117 |
return err |
| 118 |
} |
| 119 |
t.Valid = true |
| 120 |
return nil |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (t *Time) SetValid(v time.Time) { |
| 125 |
t.Time = v |
| 126 |
t.Valid = true |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func (t Time) Ptr() *time.Time { |
| 131 |
if !t.Valid { |
| 132 |
return nil |
| 133 |
} |
| 134 |
return &t.Time |
| 135 |
} |