-
Notifications
You must be signed in to change notification settings - Fork 2
/
option.go
181 lines (156 loc) · 3.06 KB
/
option.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
package gotez
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"github.com/ecadlabs/gotez/v2/encoding"
"github.com/ecadlabs/pretty"
)
type Option[T any] struct {
some bool
value T
}
func Some[T any](val T) Option[T] {
return Option[T]{
some: true,
value: val,
}
}
func None[T any]() Option[T] {
return Option[T]{
some: false,
}
}
func (op Option[T]) Unwrap() T {
if !op.some {
panic(fmt.Sprintf("called `Unwrap()` on a `None` value of type %T", op))
}
return op.value
}
func (op *Option[T]) UnwrapPtr() *T {
if !op.some {
panic(fmt.Sprintf("called `Unwrap()` on a `None` value of type %T", op))
}
return &op.value
}
func (op Option[T]) CheckUnwrap() (T, bool) {
if !op.some {
var null T
return null, false
}
return op.value, true
}
func (op *Option[T]) CheckUnwrapPtr() (*T, bool) {
if !op.some {
return nil, false
}
return &op.value, true
}
func (op Option[T]) UnwrapUnchecked() T {
return op.value
}
func (op Option[T]) IsSome() bool { return op.some }
func (op Option[T]) IsNone() bool { return !op.some }
func (op Option[T]) Or(val Option[T]) Option[T] {
if op.some {
return op
}
return val
}
func (op Option[T]) OrElse(f func() Option[T]) Option[T] {
if op.some {
return op
}
return f()
}
func (op Option[T]) UnwrapOr(def T) T {
if op.some {
return op.value
}
return def
}
func (op Option[T]) UnwrapOrElse(f func() T) T {
if op.some {
return op.value
}
return f()
}
func (op Option[T]) UnwrapOrZero() T {
if op.some {
return op.value
}
var t T
return t
}
func (op *Option[T]) DecodeTZ(data []byte, ctx *encoding.Context) (rest []byte, err error) {
if len(data) == 0 {
// tail entry
*op = Option[T]{}
return data, nil
}
*op = Option[T]{
some: data[0] != 0,
}
data = data[1:]
if op.some {
return encoding.Decode(data, &op.value, encoding.Ctx(ctx))
}
return data, nil
}
func (op *Option[T]) EncodeTZ(ctx *encoding.Context) ([]byte, error) {
var buf bytes.Buffer
if op.IsSome() {
buf.WriteByte(255)
val := op.Unwrap()
if err := encoding.Encode(&buf, &val, encoding.Ctx(ctx)); err != nil {
return nil, err
}
return buf.Bytes(), nil
} else {
return []byte{0}, nil
}
}
func (op *Option[T]) MarshalJSON() ([]byte, error) {
var v *T
if x, ok := op.CheckUnwrapPtr(); ok {
v = x
}
return json.Marshal(v)
}
func (op *Option[T]) UnmarshalJSON(data []byte) error {
var v *T
if err := json.Unmarshal(data, &v); err != nil {
return err
}
if v != nil {
*op = Some(*v)
} else {
*op = None[T]()
}
return nil
}
func (op Option[T]) GoString() string {
var tmp T
t := reflect.TypeOf(&tmp).Elem()
if op.some {
return fmt.Sprintf("Some[%v](%# v)", t, pretty.Formatter(op.value, pretty.OptStringer(true)))
}
return fmt.Sprintf("None[%v]", t)
}
type Option1[T any] struct {
Option[T]
}
func (op *Option1[T]) EncodeTZ(ctx *encoding.Context) ([]byte, error) {
var buf bytes.Buffer
if op.IsSome() {
buf.WriteByte(1)
val := op.Unwrap()
if err := encoding.Encode(&buf, &val, encoding.Ctx(ctx)); err != nil {
return nil, err
}
return buf.Bytes(), nil
} else {
return []byte{0}, nil
}
}