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 // MarshalJSON implements json.Marshaler.
64 // It will encode null if this time is null.
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 // UnmarshalJSON implements json.Unmarshaler.
73 // It supports string, object (e.g. pq.NullTime and friends)
74 // and null input.
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 // SetValid changes this Time's value and sets it to be non-null.
124 func (t *Time) SetValid(v time.Time) {
125 t.Time = v
126 t.Valid = true
127 }
128
129 // Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null.
130 func (t Time) Ptr() *time.Time {
131 if !t.Valid {
132 return nil
133 }
134 return &t.Time
135 }