|
1 | 1 | package mo |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "database/sql/driver" |
| 6 | + "encoding/gob" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
4 | 9 | "fmt" |
5 | 10 | "reflect" |
| 11 | + "time" |
6 | 12 | ) |
7 | 13 |
|
8 | 14 | var optionNoSuchElement = fmt.Errorf("no such element") |
@@ -141,3 +147,115 @@ func (o Option[T]) FlatMap(mapper func(value T) Option[T]) Option[T] { |
141 | 147 |
|
142 | 148 | return None[T]() |
143 | 149 | } |
| 150 | + |
| 151 | +// MarshalJSON encodes Option into json. |
| 152 | +func (o Option[T]) MarshalJSON() ([]byte, error) { |
| 153 | + if o.isPresent { |
| 154 | + return json.Marshal(o.value) |
| 155 | + } |
| 156 | + return json.Marshal(nil) |
| 157 | +} |
| 158 | + |
| 159 | +// UnmarshalJSON decodes Option from json. |
| 160 | +func (o *Option[T]) UnmarshalJSON(b []byte) error { |
| 161 | + if bytes.Equal(b, []byte("null")) { |
| 162 | + o.isPresent = false |
| 163 | + return nil |
| 164 | + } |
| 165 | + |
| 166 | + err := json.Unmarshal(b, &o.value) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + o.isPresent = true |
| 172 | + time.Now() |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +// MarshalText implements the encoding.TextMarshaler interface. |
| 177 | +func (o Option[T]) MarshalText() ([]byte, error) { |
| 178 | + return json.Marshal(o) |
| 179 | +} |
| 180 | + |
| 181 | +// UnmarshalText implements the encoding.TextUnmarshaler interface. |
| 182 | +func (o *Option[T]) UnmarshalText(data []byte) error { |
| 183 | + return json.Unmarshal(data, o) |
| 184 | +} |
| 185 | + |
| 186 | +// BinaryMarshaler is the interface implemented by an object that can marshal itself into a binary form. |
| 187 | +func (o Option[T]) MarshalBinary() ([]byte, error) { |
| 188 | + if !o.isPresent { |
| 189 | + return []byte{0}, nil |
| 190 | + } |
| 191 | + |
| 192 | + var buf bytes.Buffer |
| 193 | + |
| 194 | + enc := gob.NewEncoder(&buf) |
| 195 | + if err := enc.Encode(o.value); err != nil { |
| 196 | + return []byte{}, err |
| 197 | + } |
| 198 | + |
| 199 | + return append([]byte{1}, buf.Bytes()...), nil |
| 200 | +} |
| 201 | + |
| 202 | +// BinaryUnmarshaler is the interface implemented by an object that can unmarshal a binary representation of itself. |
| 203 | +func (o *Option[T]) UnmarshalBinary(data []byte) error { |
| 204 | + if len(data) == 0 { |
| 205 | + return errors.New("Option[T].UnmarshalBinary: no data") |
| 206 | + } |
| 207 | + |
| 208 | + if data[0] == 0 { |
| 209 | + o.isPresent = false |
| 210 | + o.value = empty[T]() |
| 211 | + return nil |
| 212 | + } |
| 213 | + |
| 214 | + buf := bytes.NewBuffer(data[1:]) |
| 215 | + dec := gob.NewDecoder(buf) |
| 216 | + err := dec.Decode(&o.value) |
| 217 | + if err != nil { |
| 218 | + return err |
| 219 | + } |
| 220 | + |
| 221 | + o.isPresent = true |
| 222 | + return nil |
| 223 | +} |
| 224 | + |
| 225 | +// GobEncode implements the gob.GobEncoder interface. |
| 226 | +func (o Option[T]) GobEncode() ([]byte, error) { |
| 227 | + return o.MarshalBinary() |
| 228 | +} |
| 229 | + |
| 230 | +// GobDecode implements the gob.GobDecoder interface. |
| 231 | +func (o *Option[T]) GobDecode(data []byte) error { |
| 232 | + return o.UnmarshalBinary(data) |
| 233 | +} |
| 234 | + |
| 235 | +// Scan implements the SQL driver.Scanner interface. |
| 236 | +func (o *Option[T]) Scan(src any) error { |
| 237 | + if src == nil { |
| 238 | + o.isPresent = false |
| 239 | + o.value = empty[T]() |
| 240 | + return nil |
| 241 | + } |
| 242 | + |
| 243 | + if av, err := driver.DefaultParameterConverter.ConvertValue(src); err == nil { |
| 244 | + if v, ok := av.(T); ok { |
| 245 | + o.isPresent = true |
| 246 | + o.value = v |
| 247 | + return nil |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return fmt.Errorf("failed to scan Option[T]") |
| 252 | +} |
| 253 | + |
| 254 | +// Value implements the driver Valuer interface. |
| 255 | +func (o Option[T]) Value() (driver.Value, error) { |
| 256 | + if !o.isPresent { |
| 257 | + return nil, nil |
| 258 | + } |
| 259 | + |
| 260 | + return o.value, nil |
| 261 | +} |
0 commit comments