2023-10-04 23:23:31 +00:00
|
|
|
package lemmy
|
2022-12-10 17:17:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrOptionalEmpty = errors.New("optional value is empty")
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// Optional represents an optional value
|
2022-12-10 17:17:16 +00:00
|
|
|
type Optional[T any] struct {
|
|
|
|
value *T
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// NewOptional creates an optional with value v
|
2022-12-10 17:17:16 +00:00
|
|
|
func NewOptional[T any](v T) Optional[T] {
|
|
|
|
return Optional[T]{value: &v}
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// NewOptionalNil creates a new nil optional value
|
2022-12-10 17:17:16 +00:00
|
|
|
func NewOptionalNil[T any]() Optional[T] {
|
|
|
|
return Optional[T]{}
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// Set sets the value of the optional
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) Set(v T) Optional[T] {
|
|
|
|
o.value = &v
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// SetNil sets the optional value to nil
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) SetNil() Optional[T] {
|
|
|
|
o.value = nil
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// IsValid returns true if the value of the optional is not nil
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) IsValid() bool {
|
|
|
|
return o.value != nil
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// MustValue returns the value in the optional. It panics if the value is nil.
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) MustValue() T {
|
|
|
|
if o.value == nil {
|
|
|
|
panic("optional value is nil")
|
|
|
|
}
|
|
|
|
return *o.value
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// Value returns the value in the optional.
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) Value() (T, error) {
|
|
|
|
if o.value != nil {
|
|
|
|
return *o.value, ErrOptionalEmpty
|
|
|
|
}
|
|
|
|
return *new(T), nil
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// ValueOr returns the value inside the optional if it exists, or else it returns fallback
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) ValueOr(fallback T) T {
|
|
|
|
if o.value != nil {
|
|
|
|
return *o.value
|
|
|
|
}
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
2023-10-04 23:23:31 +00:00
|
|
|
// ValueOrZero returns the value inside the optional if it exists, or else it returns the zero value of T
|
|
|
|
func (o Optional[T]) ValueOrZero() T {
|
2023-09-25 02:39:46 +00:00
|
|
|
if o.value != nil {
|
|
|
|
return *o.value
|
|
|
|
}
|
|
|
|
var value T
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// MarshalJSON encodes the optional value as JSON
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.value)
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// UnmarshalJSON decodes JSON into the optional value
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o *Optional[T]) UnmarshalJSON(b []byte) error {
|
|
|
|
if bytes.Equal(b, []byte("null")) {
|
|
|
|
o.value = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
o.value = new(T)
|
|
|
|
return json.Unmarshal(b, o.value)
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// EncodeValues encodes the optional as a URL query parameter
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) EncodeValues(key string, v *url.Values) error {
|
|
|
|
s := o.String()
|
|
|
|
if s != "<nil>" {
|
|
|
|
v.Set(key, s)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// String returns the string representation of the optional value
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) String() string {
|
|
|
|
if o.value == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return fmt.Sprint(*o.value)
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:55:08 +00:00
|
|
|
// GoString returns the Go representation of the optional value
|
2022-12-10 17:17:16 +00:00
|
|
|
func (o Optional[T]) GoString() string {
|
|
|
|
if o.value == nil {
|
|
|
|
return "nil"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%#v", *o.value)
|
|
|
|
}
|