-
Notifications
You must be signed in to change notification settings - Fork 0
/
und.go
279 lines (240 loc) · 6.81 KB
/
und.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package und
import (
"database/sql"
"encoding/json"
"encoding/xml"
"log/slog"
"github.com/ngicks/und/option"
"github.com/ngicks/und/validate"
)
var (
_ json.Marshaler = Und[any]{}
_ json.Unmarshaler = (*Und[any])(nil)
_ xml.Marshaler = Und[any]{}
_ xml.Unmarshaler = (*Und[any])(nil)
_ slog.LogValuer = Und[any]{}
)
var (
_ validate.UndValidator = Und[any]{}
_ validate.UndChecker = Und[any]{}
)
// Und[T] is a type that can express T (a value of type T), *null* (exists but empty), or *undefined* (absent, unspecified).
//
// Und[T] implements json.Unmarshaler so that it can be unmarshaled from all of those type.
//
// Und[T] implements IsZero.
// For Go 1.24 or later version, *undefined* Und[T] struct fields are omitted by [json.Marshal] (or similar functions)
// if `json:",omitzero"` option is attached to those fields.
// For Go 1.23 or older version, instead you can use the sliceund variant with `json:",omitempty"` option.
type Und[T any] struct {
opt option.Option[option.Option[T]]
}
// Defined returns a defined Und[T] whose internal value is t.
func Defined[T any](t T) Und[T] {
return Und[T]{
opt: option.Some(option.Some(t)),
}
}
// Null returns a null Und[T].
func Null[T any]() Und[T] {
return Und[T]{
opt: option.Some(option.None[T]()),
}
}
// Undefined returns an undefined Und[T].
func Undefined[T any]() Und[T] {
return Und[T]{}
}
// FromPointer converts *T into Und[T].
// If v is nil, it returns an undefined Und.
// Otherwise, it returns Defined[T] whose value is the dereferenced v.
//
// If you need to keep t as pointer, use [WrapPointer] instead.
func FromPointer[T any](v *T) Und[T] {
if v == nil {
return Undefined[T]()
}
return Defined(*v)
}
// WrapPointer converts *T into Und[*T].
// The und value is defined if t is non nil, undefined otherwise.
//
// If you want t to be dereferenced, use [FromPointer] instead.
func WrapPointer[T any](t *T) Und[*T] {
if t == nil {
return Undefined[*T]()
}
return Defined(t)
}
// FromOptions converts opt into an Und[T].
// opt is retained by the returned value.
func FromOption[T any](opt option.Option[option.Option[T]]) Und[T] {
return Und[T]{opt: opt}
}
// FromSqlNull converts a valid sql.Null[T] to a defined Und[T]
// and invalid one into a null Und[T].
func FromSqlNull[T any](v sql.Null[T]) Und[T] {
if !v.Valid {
return Null[T]()
}
return Defined(v.V)
}
// IsZero is an alias for IsUndefined.
func (u Und[T]) IsZero() bool {
return u.IsUndefined()
}
// IsDefined returns true if u is a defined value, otherwise false.
func (u Und[T]) IsDefined() bool {
return u.opt.IsSome() && u.opt.Value().IsSome()
}
// IsNull returns true if u is a null value, otherwise false.
func (u Und[T]) IsNull() bool {
return u.opt.IsSome() && u.opt.Value().IsNone()
}
// IsUndefined returns true if u is an undefined value, otherwise false.
func (u Und[T]) IsUndefined() bool {
return u.opt.IsNone()
}
// EqualFunc reports whether two Und values are equal.
// EqualFunc checks state of both. If both state does not match, it returns false.
// If both are *defined* state, then it checks equality of their value by cmp,
// then returns true if they are equal.
func (u Und[T]) EqualFunc(t Und[T], cmp func(i, j T) bool) bool {
return u.opt.EqualFunc(
t.opt,
func(i, j option.Option[T]) bool {
return i.EqualFunc(j, cmp)
},
)
}
// Equal tests equality of l and r then returns true if they are equal, false otherwise.
// For those types that are comparable but need special tests, e.g. time.Time, you should use [Und.EqualFunc] instead.
//
// This only sits here only to keep consistency to sliceund, elastic, sliceund/elastic.
// You can simply test their equality by only doing l == r.
func Equal[T comparable](l, r Und[T]) bool {
return l.EqualFunc(r, func(i, j T) bool { return i == j })
}
// CloneFunc clones u using the cloneT functions.
func (u Und[T]) CloneFunc(cloneT func(T) T) Und[T] {
return u.Map(func(o option.Option[option.Option[T]]) option.Option[option.Option[T]] {
return o.CloneFunc(func(o option.Option[T]) option.Option[T] {
return o.CloneFunc(cloneT)
})
})
}
// Clone clones u.
//
// It just returns u; this only sits here only for consistency to sliceund, elastic, sliceund/elastic.
func Clone[T comparable](u Und[T]) Und[T] {
return u
}
func (u Und[T]) UndValidate() error {
return u.opt.Value().UndValidate()
}
func (u Und[T]) UndCheck() error {
return u.opt.UndCheck()
}
// Value returns an internal value.
func (u Und[T]) Value() T {
if u.IsDefined() {
return u.opt.Value().Value()
}
var zero T
return zero
}
// Pointer returns u's internal value as a pointer.
// The value is copied by assignment before returned from Pointer.
func (u Und[T]) Pointer() *T {
if !u.IsDefined() {
return nil
}
return u.opt.Value().Pointer()
}
// DoublePointer returns nil if u is undefined, &(*T)(nil) if null, the internal value if defined.
func (u Und[T]) DoublePointer() **T {
switch {
case u.IsUndefined():
return nil
case u.IsNull():
var t *T
return &t
default:
t := u.opt.Value().Value()
tt := &t
return &tt
}
}
// Unwrap returns u's internal value.
func (u Und[T]) Unwrap() option.Option[option.Option[T]] {
return u.opt
}
// Map returns a new Und[T] whose internal value is u's mapped by f.
func (u Und[T]) Map(f func(option.Option[option.Option[T]]) option.Option[option.Option[T]]) Und[T] {
return Und[T]{opt: f(u.opt)}
}
// MarshalJSON implements json.Marshaler.
func (u Und[T]) MarshalJSON() ([]byte, error) {
if !u.IsDefined() {
return []byte(`null`), nil
}
return json.Marshal(u.opt.Value().Value())
}
// UnmarshalJSON implements json.Unmarshaler.
func (u *Und[T]) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
*u = Null[T]()
return nil
}
var t T
err := json.Unmarshal(data, &t)
if err != nil {
return err
}
*u = Defined(t)
return nil
}
// MarshalXML implements xml.Marshaler.
func (o Und[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return o.opt.Value().MarshalXML(e, start)
}
// UnmarshalXML implements xml.Unmarshaler.
func (o *Und[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var t T
err := d.DecodeElement(&t, &start)
if err != nil {
return err
}
*o = Defined(t)
return nil
}
// LogValue implements slog.LogValuer.
func (u Und[T]) LogValue() slog.Value {
return u.opt.Value().LogValue()
}
// SqlNull converts o into sql.Null[T].
func (u Und[T]) SqlNull() sql.Null[T] {
return u.opt.Value().SqlNull()
}
// State returns u's value state.
func (u Und[T]) State() State {
switch {
case u.IsUndefined():
return StateUndefined
case u.IsNull():
return StateNull
default:
return StateDefined
}
}
// Map returns a new Und value whose internal value is mapped by f.
func Map[T, U any](u Und[T], f func(t T) U) Und[U] {
switch {
case u.IsUndefined():
return Undefined[U]()
case u.IsNull():
return Null[U]()
default:
return Defined(f(u.Value()))
}
}