-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptional.go
109 lines (92 loc) · 1.81 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
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
package directusapi
import (
"encoding/json"
"reflect"
)
type Optional[T any] struct {
value T
op operation
}
func UnsetOptional[T any]() Optional[T] {
return Optional[T]{
op: unset,
}
}
func SetOptional[T any](val T) Optional[T] {
return Optional[T]{
value: val,
op: set,
}
}
func (o Optional[T]) ValueOrZero() T {
if o.op != set {
var zeroval T
return zeroval
}
return o.value
}
func (o Optional[T]) ValueMust() T {
if o.op != set {
panic("value is not set")
}
return o.value
}
func (o Optional[T]) IsSet() bool {
return o.op == set
}
func (o Optional[T]) MarshalJSON() ([]byte, error) {
switch o.op {
case set:
return json.Marshal(o.value)
case unset:
return []byte(`null`), nil
default:
// https://github.com/golang/go/issues/11939
return json.Marshal(nil)
}
}
func (o *Optional[T]) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
o.op = unset
return nil
}
if err := json.Unmarshal(data, &o.value); err != nil {
return err
}
o.op = set
// in case of noop the UnmarhsalJSON is not called
return nil
}
func (o Optional[T]) getOp() operation {
// this is hack for reflection pkg
return o.op
}
func (o Optional[T]) fields(prefix string) []string {
var optVal T
f := reflect.TypeOf(optVal)
if f.Kind() == reflect.Struct {
var t Time
isTime := f.ConvertibleTo(reflect.TypeOf(t))
isOptional := f.Implements(reflect.TypeOf(new(isOpt)).Elem())
if isOptional {
panic("optional of optional is not supported")
}
if isTime {
return []string{prefix}
}
return iterateFields(f, prefix)
}
return []string{prefix}
}
// 1. don't touch the value
// => zero value of Optional[T]
// 2. unset the value (null)
// => UnsetOptional[int]()
// 3. set the value
// => SetOptional(3)
type operation uint8
const (
noop operation = iota
unset
set
)