mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-20 19:13:06 +00:00
✨ Added 'Option' type
This commit is contained in:
parent
7c7302c3c5
commit
9cfae3bdc1
53
utils/types.go
Normal file
53
utils/types.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Option[T any] struct {
|
||||||
|
value T
|
||||||
|
none bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Option[T]) IsNone() bool {
|
||||||
|
return o.none
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Option[T]) ValueOrErr() (*T, error) {
|
||||||
|
if o.IsNone() {
|
||||||
|
return nil, errors.New("Option is None")
|
||||||
|
}
|
||||||
|
return &o.value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Option[T]) ValueOr(def *T) *T {
|
||||||
|
if o.IsNone() {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return &o.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Option[T]) ValueOrPanic() *T {
|
||||||
|
if o.IsNone() {
|
||||||
|
panic("Option is None")
|
||||||
|
}
|
||||||
|
return &o.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Option[T]) UnmarshalJSON(data []byte) error {
|
||||||
|
if string(data) == "null" {
|
||||||
|
o.none = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
o.none = false
|
||||||
|
return json.Unmarshal(data, &o.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOption[T any](value T) Option[T] {
|
||||||
|
return Option[T]{value: value}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNoneOption[T any]() Option[T] {
|
||||||
|
return Option[T]{none: true}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user