Documentation
¶
Overview ¶
Package either represents a value ether L or R.
Index ¶
- func Equal[L, R comparable](l, r Either[L, R]) bool
- func EqualEqualer[L interface{ ... }, R interface{ ... }](l, r Either[L, R]) bool
- func Transpose[T any](r Result[opt.Option[T]]) opt.Option[Result[T]]
- func TransposeLeft[L, R any](e Either[opt.Option[L], R]) opt.Option[Either[L, R]]
- func TransposeRight[L, R any](e Either[L, opt.Option[R]]) opt.Option[Either[L, R]]
- type Either
- func CloneCloner[L interface{ ... }, R interface{ ... }](e Either[L, R]) Either[L, R]
- func FlattenEither[L, R any](e Either[Either[L, R], Either[L, R]]) Either[L, R]
- func FlattenLeft[L, R any](e Either[Either[L, R], R]) Either[L, R]
- func FlattenRight[L, R any](e Either[L, Either[L, R]]) Either[L, R]
- func Left[L, R any](l L) Either[L, R]
- func Right[L, R any](r R) Either[L, R]
- func (e Either[L, R]) CloneFunc(cloneL func(L) L, cloneR func(R) R) Either[L, R]
- func (e Either[L, R]) EqualFunc(other Either[L, R], cmpL func(i, j L) bool, cmpR func(i, j R) bool) bool
- func (e Either[L, R]) ExpectLeft(msg string) L
- func (e Either[L, R]) ExpectRight(msg string) R
- func (e Either[L, R]) Flip() Either[R, L]
- func (e Either[L, R]) Fold[U any](onLeft func(l L) U, onRight func(r R) U) U
- func (e Either[L, R]) InspectLeft(fn func(l L)) Either[L, R]
- func (e Either[L, R]) InspectRight(fn func(r R)) Either[L, R]
- func (e Either[L, R]) IsLeft() bool
- func (e Either[L, R]) IsLeftAnd(fn func(l L) bool) bool
- func (e Either[L, R]) IsRight() bool
- func (e Either[L, R]) IsRightAnd(fn func(r R) bool) bool
- func (e Either[L, R]) IterLeft() iter.Seq[L]
- func (e Either[L, R]) IterRight() iter.Seq[R]
- func (e Either[L, R]) Left() opt.Option[L]
- func (e Either[L, R]) LeftAnd[U any](u Either[U, R]) Either[U, R]
- func (e Either[L, R]) LeftAndThen[U any](fn func(l L) Either[U, R]) Either[U, R]
- func (e Either[L, R]) LeftOr(l L) L
- func (e Either[L, R]) LeftOrElse(fn func(r R) L) L
- func (e Either[L, R]) LogValue() slog.Value
- func (e Either[L, R]) Map[L2, R2 any](onLeft func(l L) L2, onRight func(r R) R2) Either[L2, R2]
- func (e Either[L, R]) MapLeft[U any](fn func(l L) U) Either[U, R]
- func (e Either[L, R]) MapLeftOr[U any](defaultValue U, fn func(l L) U) U
- func (e Either[L, R]) MapRight[U any](fn func(r R) U) Either[L, U]
- func (e Either[L, R]) MapRightOr[U any](defaultValue U, fn func(r R) U) U
- func (e Either[L, R]) MarshalJSON() ([]byte, error)
- func (e Either[L, R]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (e Either[L, R]) MarshalXML(enc *xml.Encoder, start xml.StartElement) error
- func (e Either[L, R]) Right() opt.Option[R]
- func (e Either[L, R]) RightAnd[U any](u Either[L, U]) Either[L, U]
- func (e Either[L, R]) RightAndThen[U any](fn func(r R) Either[L, U]) Either[L, U]
- func (e Either[L, R]) RightOr(r R) R
- func (e Either[L, R]) RightOrElse(fn func(l L) R) R
- func (e *Either[L, R]) UnmarshalJSON(data []byte) error
- func (e *Either[L, R]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (e *Either[L, R]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (e Either[L, R]) Unpack() (l L, r R)
- func (e Either[L, R]) UnwrapLeft() L
- func (e Either[L, R]) UnwrapRight() R
- type Result
- func (r Result[T]) And[U any](u Result[U]) Result[U]
- func (r Result[T]) AndThen[U any](fn func(t T) Result[U]) Result[U]
- func (r Result[T]) Either() Either[T, error]
- func (r Result[T]) Err() error
- func (r Result[T]) Expect(msg string) T
- func (r Result[T]) ExpectErr(msg string) error
- func (r Result[T]) Inspect(fn func(t T)) Result[T]
- func (r Result[T]) InspectErr(fn func(err error)) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsErrAnd(fn func(err error) bool) bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) IsOkAnd(fn func(t T) bool) bool
- func (r Result[T]) Iter() iter.Seq[T]
- func (r Result[T]) LogValue() slog.Value
- func (r Result[T]) Map[U any](fn func(t T) U) Result[U]
- func (r Result[T]) MapErr(fn func(err error) error) Result[T]
- func (r Result[T]) MapOr[U any](defaultValue U, fn func(t T) U) U
- func (r Result[T]) MapOrElse[U any](defaultFn func(err error) U, fn func(t T) U) U
- func (r Result[T]) MarshalJSON() ([]byte, error)
- func (r Result[T]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (r Result[T]) MarshalXML(enc *xml.Encoder, start xml.StartElement) error
- func (r Result[T]) Ok() opt.Option[T]
- func (r Result[T]) Or(u Result[T]) Result[T]
- func (r Result[T]) OrElse(fn func(err error) Result[T]) Result[T]
- func (r *Result[T]) UnmarshalJSON(data []byte) error
- func (r *Result[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (r *Result[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (r Result[T]) Unwrap() T
- func (r Result[T]) UnwrapErr() error
- func (r Result[T]) UnwrapOr(t T) T
- func (r Result[T]) UnwrapOrDefault() T
- func (r Result[T]) UnwrapOrElse(fn func(err error) T) T
- func (r Result[T]) Value() T
- type UntaggedEither
- func (u UntaggedEither[L, R]) MarshalJSON() ([]byte, error)
- func (u UntaggedEither[L, R]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (u UntaggedEither[L, R]) MarshalXML(enc *xml.Encoder, start xml.StartElement) error
- func (u *UntaggedEither[L, R]) UnmarshalJSON(data []byte) error
- func (u *UntaggedEither[L, R]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (u *UntaggedEither[L, R]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- type UntaggedResult
- func (u UntaggedResult[T]) MarshalJSON() ([]byte, error)
- func (u UntaggedResult[T]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (u UntaggedResult[T]) MarshalXML(enc *xml.Encoder, start xml.StartElement) error
- func (u *UntaggedResult[T]) UnmarshalJSON(data []byte) error
- func (u *UntaggedResult[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (u *UntaggedResult[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
func Equal[L, R comparable](l, r Either[L, R]) bool
Equal tests equality of l and r then returns true if they are equal, false otherwise.
func EqualEqualer ¶
func EqualEqualer[L interface{ Equal(l L) bool }, R interface{ Equal(r R) bool }](l, r Either[L, R]) bool
EqualEqualer tests equality of l and r by calling the Equal method implemented on their held values.
func Transpose ¶
Transpose converts a Result[opt.Option[T]] into an opt.Option[Result[T]]. An ok wrapping none maps to none. An ok wrapping some maps to some wrapping ok, and an err maps to some wrapping err.
func TransposeLeft ¶
TransposeLeft converts an Either[opt.Option[L], R] into an opt.Option[Either[L, R]]. A left none maps to none. A left some maps to some wrapping left, and a right maps to some wrapping right.
Types ¶
type Either ¶
type Either[L, R any] struct { // contains filtered or unexported fields }
func CloneCloner ¶
CloneCloner clones e by calling the Clone method on its held value.
For custom clone functions, use Either.CloneFunc. An Either[L, R] whose L and R only need a shallow copy can be copied by plain assignment.
func FlattenEither ¶
FlattenEither converts an Either[Either[L, R], Either[L, R]] into an Either[L, R], returning the inner either of whichever side e holds.
func FlattenLeft ¶
FlattenLeft converts a nested Either[Either[L, R], R] into an Either[L, R]. The result is the inner either if e is left, Right[L] holding e's right value otherwise.
func FlattenRight ¶
FlattenRight converts a nested Either[L, Either[L, R]] into an Either[L, R]. The result is the inner either if e is right, Left[L, R] holding e's left value otherwise.
func (Either[L, R]) CloneFunc ¶
CloneFunc clones e using the cloneL or cloneR function for the held value.
func (Either[L, R]) EqualFunc ¶
func (e Either[L, R]) EqualFunc(other Either[L, R], cmpL func(i, j L) bool, cmpR func(i, j R) bool) bool
EqualFunc tests e and other if both are left or right. If their state does not match, it returns false immediately. Otherwise it tests equality of the held values by cmpL or cmpR respectively.
If L and R are just comparable types, use Equal. If L and R are implementors of interface { Equal(t T) bool }, e.g time.Time, use EqualEqualer.
func (Either[L, R]) ExpectLeft ¶
ExpectLeft returns e's left value if e is left. Otherwise it panics with msg.
func (Either[L, R]) ExpectRight ¶
ExpectRight returns e's right value if e is right. Otherwise it panics with msg.
func (Either[L, R]) Flip ¶
Flip converts Either[L, R] into Either[R, L], a left into a right and vice versa.
func (Either[L, R]) Fold ¶
Fold applies onLeft to a left value or onRight to a right value, squashing both cases into a single U.
func (Either[L, R]) InspectLeft ¶
InspectLeft calls fn with e's left value if e is left, then returns e unchanged.
func (Either[L, R]) InspectRight ¶
InspectRight calls fn with e's right value if e is right, then returns e unchanged.
func (Either[L, R]) IsLeft ¶
IsLeft returns true if e holds an L value. Note that the zero value of Either[L, R] is left holding the zero value of L.
func (Either[L, R]) IsLeftAnd ¶
IsLeftAnd returns true if e is left and calling fn with e's left value returns true. Otherwise it returns false.
func (Either[L, R]) IsRightAnd ¶
IsRightAnd returns true if e is right and calling fn with e's right value returns true. Otherwise it returns false.
func (Either[L, R]) IterLeft ¶
IterLeft returns an iterator over the left value. If e is left, the iterator yields the left value, otherwise nothing.
func (Either[L, R]) IterRight ¶
IterRight returns an iterator over the right value. If e is right, the iterator yields the right value, otherwise nothing.
func (Either[L, R]) Left ¶
Left converts e into opt.Option[L]. The option is some wrapping the left value if e is left, none otherwise.
func (Either[L, R]) LeftAnd ¶
LeftAnd returns u if e is left, otherwise Right[U] holding e's right value.
func (Either[L, R]) LeftAndThen ¶
LeftAndThen calls fn with e's left value if e is left, otherwise returns Right[U] holding e's right value.
func (Either[L, R]) LeftOr ¶
func (e Either[L, R]) LeftOr(l L) L
LeftOr returns e's left value if e is left, otherwise l.
func (Either[L, R]) LeftOrElse ¶
func (e Either[L, R]) LeftOrElse(fn func(r R) L) L
LeftOrElse returns e's left value if e is left, otherwise the result of calling fn with e's right value.
func (Either[L, R]) MapLeftOr ¶
MapLeftOr returns e's left value applied by fn if e is left. Otherwise it returns defaultValue.
func (Either[L, R]) MapRightOr ¶
MapRightOr returns e's right value applied by fn if e is right. Otherwise it returns defaultValue.
func (Either[L, R]) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
It encodes e as a JSON object with a single "left" or "right" key tagging the held value. For the untagged representation, use UntaggedEither.
func (Either[L, R]) MarshalJSONTo ¶
MarshalJSONTo implements encoding/json/v2.MarshalerTo.
It encodes e as a JSON object with a single "left" or "right" key tagging the held value. For the untagged representation, use UntaggedEither.
func (Either[L, R]) MarshalXML ¶
MarshalXML implements xml.Marshaler.
It encodes the held value wrapped in a <left> or <right> element tagging the side, e.g. <v><left>5</left></v>.
func (Either[L, R]) Right ¶
Right converts e into opt.Option[R]. The option is some wrapping the right value if e is right, none otherwise.
func (Either[L, R]) RightAnd ¶
RightAnd returns u if e is right, otherwise Left[L] holding e's left value.
func (Either[L, R]) RightAndThen ¶
RightAndThen calls fn with e's right value if e is right, otherwise returns Left[L] holding e's left value.
func (Either[L, R]) RightOr ¶
func (e Either[L, R]) RightOr(r R) R
RightOr returns e's right value if e is right, otherwise r.
func (Either[L, R]) RightOrElse ¶
func (e Either[L, R]) RightOrElse(fn func(l L) R) R
RightOrElse returns e's right value if e is right, otherwise the result of calling fn with e's left value.
func (*Either[L, R]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
It decodes a JSON object with a single "left" or "right" key, storing the tagged value on the corresponding side.
func (*Either[L, R]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements encoding/json/v2.UnmarshalerFrom.
It decodes a JSON object with a single "left" or "right" key, storing the tagged value on the corresponding side.
func (*Either[L, R]) UnmarshalXML ¶
UnmarshalXML implements xml.Unmarshaler.
It decodes an element containing a single <left> or <right> child element, storing the tagged value on the corresponding side.
func (Either[L, R]) Unpack ¶
func (e Either[L, R]) Unpack() (l L, r R)
Unpack returns both internal values of e. The value for the side e does not hold is the zero value of its type.
func (Either[L, R]) UnwrapLeft ¶
func (e Either[L, R]) UnwrapLeft() L
UnwrapLeft returns e's left value if e is left. Otherwise it panics.
func (Either[L, R]) UnwrapRight ¶
func (e Either[L, R]) UnwrapRight() R
UnwrapRight returns e's right value if e is right. Otherwise it panics.
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
func Flatten ¶
Flatten converts a nested Result[Result[T]] into a Result[T]. The result is the inner result if r is ok, Err[T] holding r's error otherwise.
func FromResult ¶
FromResult converts the conventional (t T, err error) pair into a Result[T]. The result is err if err is non nil, ok wrapping t otherwise.
func (Result[T]) AndThen ¶
AndThen calls fn with r's value if r is ok, otherwise returns Err[U] holding r's error.
func (Result[T]) InspectErr ¶
InspectErr calls fn with r's error if r is err, then returns r unchanged.
func (Result[T]) IsErrAnd ¶
IsErrAnd returns true if r is err and calling fn with r's error returns true. Otherwise it returns false.
func (Result[T]) IsOkAnd ¶
IsOkAnd returns true if r is ok and calling fn with r's value returns true. Otherwise it returns false.
func (Result[T]) Iter ¶
Iter returns an iterator over the internal value. If r is ok, the iterator yields the value, otherwise nothing.
func (Result[T]) LogValue ¶
LogValue implements slog.LogValuer, delegating to the internal either value.
func (Result[T]) Map ¶
Map returns Ok[U] whose inner value is r's value mapped by fn if r is ok. Otherwise it returns Err[U] holding r's error.
func (Result[T]) MapErr ¶
MapErr returns Err[T] whose error is r's error mapped by fn if r is err. Otherwise it returns r unchanged.
func (Result[T]) MapOr ¶
MapOr returns r's value applied by fn if r is ok. Otherwise it returns defaultValue.
func (Result[T]) MapOrElse ¶
MapOrElse returns r's value applied by fn if r is ok. Otherwise it returns the result of calling defaultFn with r's error.
func (Result[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
It encodes r as a JSON object with a single "ok" or "err" key tagging the held value. The error is encoded as its message string, or the JSON null if it is nil. For the untagged representation, use UntaggedResult.
func (Result[T]) MarshalJSONTo ¶
MarshalJSONTo implements encoding/json/v2.MarshalerTo.
It encodes r as a JSON object with a single "ok" or "err" key tagging the held value. The error is encoded as its message string, or the JSON null if it is nil. For the untagged representation, use UntaggedResult.
func (Result[T]) MarshalXML ¶
MarshalXML implements xml.Marshaler.
It encodes the held value wrapped in an <ok> or <err> element tagging the side. The error is encoded as its message string, an empty one if it is nil.
func (Result[T]) Ok ¶
Ok converts r into opt.Option[T]. The option is some wrapping the value if r is ok, none otherwise.
func (Result[T]) OrElse ¶
OrElse returns r if r is ok, otherwise calls fn with r's error and returns the result.
func (*Result[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
It decodes a JSON object with a single "ok" or "err" key. The "err" value must be a string or null; since the concrete error type is not recoverable, the error is reconstructed from the message with errors.New.
func (*Result[T]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements encoding/json/v2.UnmarshalerFrom.
It decodes a JSON object with a single "ok" or "err" key. The "err" value must be a string or null; since the concrete error type is not recoverable, the error is reconstructed from the message with errors.New.
func (*Result[T]) UnmarshalXML ¶
UnmarshalXML implements xml.Unmarshaler.
It decodes an element containing a single <ok> or <err> child element. Since the concrete error type is not recoverable, the error is reconstructed from the message with errors.New.
func (Result[T]) Unwrap ¶
func (r Result[T]) Unwrap() T
Unwrap returns r's value if r is ok. Otherwise it panics with the held error.
func (Result[T]) UnwrapOr ¶
func (r Result[T]) UnwrapOr(t T) T
UnwrapOr returns r's value if r is ok, otherwise t.
func (Result[T]) UnwrapOrDefault ¶
func (r Result[T]) UnwrapOrDefault() T
UnwrapOrDefault returns r's value if r is ok, otherwise the zero value of T.
func (Result[T]) UnwrapOrElse ¶
UnwrapOrElse returns r's value if r is ok, otherwise the result of calling fn with r's error.
type UntaggedEither ¶
UntaggedEither[L, R] adapts Either[L, R] to the untagged JSON representation: the held value is encoded directly, without the "left" or "right" tag object.
Since the encoding is untagged, unmarshaling first attempts to decode the value into L, then falls back to R; the first success wins. If L and R overlap (e.g. both decode from a JSON number), the value always lands on the left.
func (UntaggedEither[L, R]) MarshalJSON ¶
func (u UntaggedEither[L, R]) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (UntaggedEither[L, R]) MarshalJSONTo ¶
func (u UntaggedEither[L, R]) MarshalJSONTo(enc *jsontext.Encoder) error
MarshalJSONTo implements encoding/json/v2.MarshalerTo.
func (UntaggedEither[L, R]) MarshalXML ¶
func (u UntaggedEither[L, R]) MarshalXML(enc *xml.Encoder, start xml.StartElement) error
MarshalXML implements xml.Marshaler.
It encodes the held value directly into the start element, without the tagging child element.
func (*UntaggedEither[L, R]) UnmarshalJSON ¶
func (u *UntaggedEither[L, R]) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
func (*UntaggedEither[L, R]) UnmarshalJSONFrom ¶
func (u *UntaggedEither[L, R]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
UnmarshalJSONFrom implements encoding/json/v2.UnmarshalerFrom.
func (*UntaggedEither[L, R]) UnmarshalXML ¶
func (u *UntaggedEither[L, R]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML implements xml.Unmarshaler.
It records the element's tokens and replays them, first attempting to decode the element into L, then falling back to R; the first success wins.
Mind that XML decoding is lenient and the sides overlap more often than in JSON: decoding into a struct virtually never fails, and empty content decodes into primitives as their zero value. In such cases the value lands on the left; use a strict xml.Unmarshaler implementor as L to make the fallback reachable.
type UntaggedResult ¶
UntaggedResult[T] adapts Result[T] to the untagged JSON representation: the held value is encoded directly, without the "ok" or "err" tag object. The error is encoded as its message string, or the JSON null if it is nil.
Since the encoding is untagged, unmarshaling first attempts to decode the value into T, then falls back to an error message string reconstructed with errors.New; the first success wins. If T also decodes from a JSON string, the value always lands on ok.
func (UntaggedResult[T]) MarshalJSON ¶
func (u UntaggedResult[T]) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (UntaggedResult[T]) MarshalJSONTo ¶
func (u UntaggedResult[T]) MarshalJSONTo(enc *jsontext.Encoder) error
MarshalJSONTo implements encoding/json/v2.MarshalerTo.
func (UntaggedResult[T]) MarshalXML ¶
func (u UntaggedResult[T]) MarshalXML(enc *xml.Encoder, start xml.StartElement) error
MarshalXML implements xml.Marshaler.
It encodes the held value directly into the start element, without the tagging child element. The error is encoded as its message string, an empty one if it is nil.
func (*UntaggedResult[T]) UnmarshalJSON ¶
func (u *UntaggedResult[T]) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
func (*UntaggedResult[T]) UnmarshalJSONFrom ¶
func (u *UntaggedResult[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
UnmarshalJSONFrom implements encoding/json/v2.UnmarshalerFrom.
func (*UntaggedResult[T]) UnmarshalXML ¶
func (u *UntaggedResult[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML implements xml.Unmarshaler.
It records the element's tokens and replays them, first attempting to decode the element into T, then falling back to an error message string reconstructed with errors.New; the first success wins.
Mind that XML decoding is lenient and the sides overlap more often than in JSON: decoding into a struct virtually never fails, and empty content decodes into primitives as their zero value. In such cases the value lands on ok; use a strict xml.Unmarshaler implementor as T to make the fallback reachable.