-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional.go
54 lines (47 loc) · 1.36 KB
/
optional.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
package nulls
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
// Optional holds a nullable value. This can be used instead of Nullable or
// NullableInto if database support is not required.
type Optional[T any] struct {
// V is the actual value when Valid.
V T `exhaustruct:"optional"`
// Valid describes whether the Nullable does not hold a NULL value.
Valid bool
}
// NewOptional creates a new valid Optional with the given value.
func NewOptional[T any](v T) Optional[T] {
return Optional[T]{
V: v,
Valid: true,
}
}
// MarshalJSON as value. If not vot valid, a NULL-value is returned.
func (n Optional[T]) MarshalJSON() ([]byte, error) {
if !n.Valid {
return json.Marshal(nil)
}
return json.Marshal(n.V)
}
// UnmarshalJSON as value or sets Valid or false if null.
func (n *Optional[T]) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
n.Valid = false
return nil
}
n.Valid = true
return json.Unmarshal(data, &n.V)
}
// Scan returns an error as this is currently not supported on Optional. Use
// Nullable or NullableInto instead.
func (n *Optional[T]) Scan(_ any) error {
return fmt.Errorf("cannot scan optional")
}
// Value returns an error as this is currently not supported on Optional. Use
// Nullable or NullableInto instead.
func (n Optional[T]) Value() (driver.Value, error) {
return nil, fmt.Errorf("cannot value optional")
}