-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_nullable.go
52 lines (45 loc) · 1.32 KB
/
json_nullable.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
package nulls
import (
"database/sql/driver"
"encoding/json"
"errors"
)
// JSONNullable holds a nullable value. Keep in mind, that T must be
// (un)marshallable. However, it cannot be used as sql.Scanner or driver.Valuer.
type JSONNullable[T any] struct {
// V is the actual value when Valid.
V T `exhaustruct:"optional"`
// Valid describes whether the JSONNullable does not hold a NULL value.
Valid bool
}
// NewJSONNullable creates a new valid JSONNullable with the given value.
func NewJSONNullable[T any](v T) JSONNullable[T] {
return JSONNullable[T]{
V: v,
Valid: true,
}
}
// MarshalJSON as value. If not vot valid, a NULL-value is returned.
func (n JSONNullable[T]) MarshalJSON() ([]byte, error) {
if !n.Valid {
return json.Marshal(nil)
}
return json.Marshal(n.V)
}
// UnmarshalJSON as value ro sets Valid o false if null.
func (n *JSONNullable[T]) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
n.Valid = false
return nil
}
n.Valid = true
return json.Unmarshal(data, &n.V)
}
// Scan to value or not valid if nil.
func (n *JSONNullable[T]) Scan(src any) error {
return errors.New("unsupported operation")
}
// Value returns the value for satisfying the driver.Valuer interface.
func (n JSONNullable[T]) Value() (driver.Value, error) {
return nil, errors.New("unsupported operation")
}