Return bool from Optional[T].Value() instead of an error

This commit is contained in:
Elara 2023-10-04 16:44:07 -07:00
parent 7e2677c495
commit 8a4f704788

View File

@ -3,13 +3,10 @@ package lemmy
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/url" "net/url"
) )
var ErrOptionalEmpty = errors.New("optional value is empty")
// Optional represents an optional value // Optional represents an optional value
type Optional[T any] struct { type Optional[T any] struct {
value *T value *T
@ -51,11 +48,11 @@ func (o Optional[T]) MustValue() T {
} }
// Value returns the value in the optional. // Value returns the value in the optional.
func (o Optional[T]) Value() (T, error) { func (o Optional[T]) Value() (T, bool) {
if o.value != nil { if o.value != nil {
return *o.value, ErrOptionalEmpty return *o.value, true
} }
return *new(T), nil return *new(T), false
} }
// ValueOr returns the value inside the optional if it exists, or else it returns fallback // ValueOr returns the value inside the optional if it exists, or else it returns fallback