-
Notifications
You must be signed in to change notification settings - Fork 0
/
of.go
159 lines (128 loc) · 2.88 KB
/
of.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package nullable
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/google/uuid"
)
type Of[T bool | int | int16 | int32 | int64 | string | uuid.UUID | float64 | JSON] struct {
val *T
}
// IsNull returns true iff the value is nil
func (n *Of[T]) IsNull() bool {
return n == nil || n.val == nil
}
// GetValue implements the getter.
func (n *Of[T]) GetValue() *T {
if n == nil {
return nil
}
return n.val
}
// SetValue implements the setter.
func (n *Of[T]) SetValue(b T) {
if n == nil {
n = new(Of[T])
n.SetValue(b)
return
}
n.val = &b
}
// SetValueP implements the setter by pointer.
// If ref is not nil, calls SetValue(*ref)
// If ref is nil, calls SetNull()
func (n *Of[T]) SetValueP(ref *T) {
if n == nil {
n = new(Of[T])
}
if ref != nil {
n.SetValue(*ref)
} else {
n.SetNull()
}
}
// SetNull set to null.
func (n *Of[T]) SetNull() {
if n == nil {
panic("calling SetNull on nil receiver")
}
n.val = nil
}
// MarshalJSON implements the encoding json interface.
func (n *Of[T]) MarshalJSON() ([]byte, error) {
if n == nil {
b, _ := json.Marshal(nil)
return b, nil
}
return marshalJSON[T](n)
}
// UnmarshalJSON implements the decoding json interface.
func (n *Of[T]) UnmarshalJSON(data []byte) error {
if n == nil {
panic("calling UnmarshalJSON on nil receiver")
}
if n.val == nil && data != nil {
n.val = new(T)
}
if data == nil {
return nil
}
err := json.Unmarshal(data, n.val)
if err != nil {
return fmt.Errorf("nullable Unmarshal Error : %w", err)
}
return nil
}
// Value implements the driver.Valuer interface.
func (n *Of[T]) Value() (driver.Value, error) {
if n.IsNull() {
return nil, nil
}
switch value := any(n.val).(type) {
case *string, *int16, *int32, *int, *int64, *float64, *bool, *time.Time, *uuid.UUID:
return *n.val, nil
case JSON:
if value == nil {
return nil, nil
}
if valuer, ok := value.(driver.Valuer); ok {
v, err := valuer.Value()
if err != nil {
return nil, fmt.Errorf("custom valuer error on nullable : %w", err)
}
return v, nil
}
b, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("nullable database value error : %w", err)
}
return string(b), nil
}
return nil, fmt.Errorf("type %T is not supported for value %v", *n.val, *n.val)
}
// Scan implements the sql.Scanner interface.
// This method decodes a JSON-encoded value into the struct.
func (n *Of[T]) Scan(v any) error {
if n == nil {
return errors.New("calling Scan on nil receiver")
}
switch any(n.val).(type) {
case *string:
return n.scanString(v)
case *uuid.UUID:
return n.scanUUID(v)
case *int16, *int32, *int, *int64:
return n.scanInt(v)
case *float64:
return n.scanFloat(v)
case *bool:
return n.scanBool(v)
case *time.Time:
return n.scanTime(v)
case *JSON, JSON:
return n.scanJSON(v)
}
return fmt.Errorf("type %T is not handled as nullable", v)
}