Documentation
¶
Overview ¶
Package result defines interfaces and structs for encapsulating a value or an error.
Without Result, code must continually handle errors in the middle of business logic. With Result, the algorithm is more clear and error handling is abstracted.
Three constructors are provided for creating a Result; Ok which creates a Result encapsulating a value, Error which creates a Result encapsulating an error, and New which creates a Result from a value and an error. New is useful for converting return values from functions that return both a value and an error into a Result. See those functions for more information.
Usage:
Without Result:
func getPostCount() (int, error) { resp, err := http.Get("https://api.project.com/posts") if err != nil { return 0, errors.New("failed to fetch posts: cause %v", e) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return 0, err } posts := []*Post{} err = json.Unmarshal(body, &posts) if err != nil { return 0, err } return len(posts), nil }
Using Result:
func getPostCount() result.Result[int] { resp := fetch("https://api.project.com/posts") body := result.FlatMap(readAll, resp) posts := result.FlatMap(marshal, body) return result.MapNoError(count, posts) } func fetch(url string) result.Result[*http.Response] { resp := result.New(http.Get(url)) return result.ErrorMap(addErrorContext, resp) } func addErrorContext(err error) error { return errors.New("failed to fetch posts: cause %v", e) } func readAll(r *http.Response) result.Result[[]byte] { defer r.Body.Close() return result.New(io.ReadAll(r.Body)) } func marshal(content []byte) result.Result[[]*Post] { posts := []*Post{} err := json.Unmarshal(content, &posts) return result.New(posts, err) } func count(posts []*Post) int { return len(posts) }
Index ¶
- func CallErrorOnlyFunc(functionResult Result[func() error]) error
- func CallFunc(functionResult Result[func()])
- func MapErrorOnly[T any](f MapperErrorOnly[T], r Result[T]) error
- func MapErrorOnly2[T1, T2 any](f MapperErrorOnly2[T1, T2], r1 Result[T1], r2 Result[T2]) error
- func MapErrorOnly3[T1, T2, T3 any](f MapperErrorOnly3[T1, T2, T3], r1 Result[T1], r2 Result[T2], r3 Result[T3]) error
- func MapErrorOnly4[T1, T2, T3, T4 any](f MapperErrorOnly4[T1, T2, T3, T4], r1 Result[T1], r2 Result[T2], ...) error
- func MapErrorOnly5[T1, T2, T3, T4, T5 any](f MapperErrorOnly5[T1, T2, T3, T4, T5], r1 Result[T1], r2 Result[T2], ...) error
- type ErrorMapper
- type FlatMapper
- type FlatMapper2
- type FlatMapper3
- type FlatMapper4
- type FlatMapper5
- type Getter
- type Mapper
- type Mapper2
- type Mapper3
- type Mapper4
- type Mapper5
- type MapperErrorOnly
- type MapperErrorOnly2
- type MapperErrorOnly3
- type MapperErrorOnly4
- type MapperErrorOnly5
- type MapperNoError
- type MapperNoError2
- type MapperNoError3
- type MapperNoError4
- type MapperNoError5
- type Result
- func As[T, S any](src Result[S]) Result[T]
- func CallFlatFunc[T any](functionResult Result[func() Result[T]]) Result[T]
- func Error[T any](e error) Result[T]
- func ErrorMap[T any](f ErrorMapper, r Result[T]) Result[T]
- func FlatMap[T, R any](f FlatMapper[T, R], r Result[T]) Result[R]
- func FlatMap2[T1, T2, R any](f FlatMapper2[T1, T2, R], r1 Result[T1], r2 Result[T2]) Result[R]
- func FlatMap3[T1, T2, T3, R any](f FlatMapper3[T1, T2, T3, R], r1 Result[T1], r2 Result[T2], r3 Result[T3]) Result[R]
- func FlatMap4[T1, T2, T3, T4, R any](f FlatMapper4[T1, T2, T3, T4, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], ...) Result[R]
- func FlatMap5[T1, T2, T3, T4, T5, R any](f FlatMapper5[T1, T2, T3, T4, T5, R], r1 Result[T1], r2 Result[T2], ...) Result[R]
- func FromMaybe[T any](value maybe.Maybe[T], err error) Result[T]
- func Map[T, R any](f Mapper[T, R], r Result[T]) Result[R]
- func Map2[T1, T2, R any](f Mapper2[T1, T2, R], r1 Result[T1], r2 Result[T2]) Result[R]
- func Map3[T1, T2, T3, R any](f Mapper3[T1, T2, T3, R], r1 Result[T1], r2 Result[T2], r3 Result[T3]) Result[R]
- func Map4[T1, T2, T3, T4, R any](f Mapper4[T1, T2, T3, T4, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], ...) Result[R]
- func Map5[T1, T2, T3, T4, T5, R any](f Mapper5[T1, T2, T3, T4, T5, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], ...) Result[R]
- func MapNoError[T, R any](f MapperNoError[T, R], r Result[T]) Result[R]
- func MapNoError2[T1, T2, R any](f MapperNoError2[T1, T2, R], r1 Result[T1], r2 Result[T2]) Result[R]
- func MapNoError3[T1, T2, T3, R any](f MapperNoError3[T1, T2, T3, R], r1 Result[T1], r2 Result[T2], r3 Result[T3]) Result[R]
- func MapNoError4[T1, T2, T3, T4, R any](f MapperNoError4[T1, T2, T3, T4, R], r1 Result[T1], r2 Result[T2], ...) Result[R]
- func MapNoError5[T1, T2, T3, T4, T5, R any](f MapperNoError5[T1, T2, T3, T4, T5, R], r1 Result[T1], r2 Result[T2], ...) Result[R]
- func New[T any](value T, err error) Result[T]
- func Ok[T any](value T) Result[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallErrorOnlyFunc ¶
CallErrorOnlyFunc is a helper function that will call an error returning function that is encapsulated in a Result. This is useful when deferring a call to a function that was returned in a Result. If the Result contains an error, that error is returned. Otherwise, the value returned by the encapsulated function is returned.
func CallFunc ¶
func CallFunc(functionResult Result[func()])
CallFunc is a helper function that will call a function with no return value that is encapsulated in a Result. This is useful when deferring a call to a function that was returned in a Result.
func MapErrorOnly ¶
func MapErrorOnly[T any]( f MapperErrorOnly[T], r Result[T], ) error
MapErrorOnly applies the given MapperErrorOnly to the Result. If the Result encapsulates a value then that value is passed to the MapperErrorOnly and the error is returned. If the Result encapsulates an error then that error is returned.
func MapErrorOnly2 ¶
func MapErrorOnly2[T1, T2 any]( f MapperErrorOnly2[T1, T2], r1 Result[T1], r2 Result[T2], ) error
MapErrorOnly2 applies the given MapperErrorOnly2 to the Result. If the Result encapsulates a value then that value is passed to the MapperErrorOnly2 and the error is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is returned.
func MapErrorOnly3 ¶
func MapErrorOnly3[T1, T2, T3 any]( f MapperErrorOnly3[T1, T2, T3], r1 Result[T1], r2 Result[T2], r3 Result[T3], ) error
MapErrorOnly3 applies the given MapperErrorOnly3 to the Result. If the Result encapsulates a value then that value is passed to the MapperErrorOnly3 and the error is returned. If any Result encapsulates an error then the error of the first argument that has an error (from left to right) is returned.
func MapErrorOnly4 ¶
func MapErrorOnly4[T1, T2, T3, T4 any]( f MapperErrorOnly4[T1, T2, T3, T4], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], ) error
MapErrorOnly4 applies the given MapperErrorOnly4 to the Result. If the Result encapsulates a value then that value is passed to the MapperErrorOnly4 and the error is returned. If any Result encapsulates an error then the error of the first argument that has an error (from left to right) is returned.
func MapErrorOnly5 ¶
func MapErrorOnly5[T1, T2, T3, T4, T5 any]( f MapperErrorOnly5[T1, T2, T3, T4, T5], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], r5 Result[T5], ) error
MapErrorOnly5 applies the given MapperErrorOnly5 to the Result. If the Result encapsulates a value then that value is passed to the MapperErrorOnly5 and the error is returned. If any Result encapsulates an error then the error of the first argument that has an error (from left to right) is returned.
Types ¶
type ErrorMapper ¶
ErrorMapper defines a function that takes an error and returns an error. This can be used to convert between errors or add additional context to an error.
type FlatMapper ¶
FlatMapper defines a function with one argument that maps from the argument's type to a Result.
func MakeFlatMapper ¶
func MakeFlatMapper[T, R any]( f Mapper[T, R], ) FlatMapper[T, R]
MakeFlatMapper takes a Mapper and returns a FlatMapper that can be used with FlatMap.
func MakeFlatMapperNoError ¶
func MakeFlatMapperNoError[T, R any]( f MapperNoError[T, R], ) FlatMapper[T, R]
MakeFlatMapperNoError takes a MapperNoError and returns a FlatMapper that can be used with FlatMap.
type FlatMapper2 ¶
FlatMapper2 defines a function with one argument that maps from the argument's type to a Result.
func MakeFlatMapper2 ¶
func MakeFlatMapper2[T1, T2, R any]( f Mapper2[T1, T2, R], ) FlatMapper2[T1, T2, R]
MakeFlatMapper2 takes a Mapper2 and returns a FlatMapper2 that can be used with FlatMap2.
func MakeFlatMapperNoError2 ¶
func MakeFlatMapperNoError2[T1, T2, R any]( f MapperNoError2[T1, T2, R], ) FlatMapper2[T1, T2, R]
MakeFlatMapperNoError2 takes a MapperNoError2 and returns a FlatMapper2 that can be used with FlatMap2.
type FlatMapper3 ¶
FlatMapper3 defines a function with one argument that maps from the argument's type to a Result.
func MakeFlatMapper3 ¶
func MakeFlatMapper3[T1, T2, T3, R any]( f Mapper3[T1, T2, T3, R], ) FlatMapper3[T1, T2, T3, R]
MakeFlatMapper3 takes a Mapper3 and returns a FlatMapper3 that can be used with FlatMap3.
func MakeFlatMapperNoError3 ¶
func MakeFlatMapperNoError3[T1, T2, T3, R any]( f MapperNoError3[T1, T2, T3, R], ) FlatMapper3[T1, T2, T3, R]
MakeFlatMapperNoError3 takes a MapperNoErrors3 and returns a FlatMapper3 that can be used with FlatMap3.
type FlatMapper4 ¶
FlatMapper4 defines a function with one argument that maps from the argument's type to a Result.
func MakeFlatMapper4 ¶
func MakeFlatMapper4[T1, T2, T3, T4, R any]( f Mapper4[T1, T2, T3, T4, R], ) FlatMapper4[T1, T2, T3, T4, R]
MakeFlatMapper4 takes a Mapper4 and returns a FlatMapper4 that can be used with FlatMap4.
func MakeFlatMapperNoError4 ¶
func MakeFlatMapperNoError4[T1, T2, T3, T4, R any]( f MapperNoError4[T1, T2, T3, T4, R], ) FlatMapper4[T1, T2, T3, T4, R]
MakeFlatMapperNoError4 takes a MapperNoError4 and returns a FlatMapper4 that can be used with FlatMap4.
type FlatMapper5 ¶
FlatMapper5 defines a function with one argument that maps from the argument's type to a Result.
func MakeFlatMapper5 ¶
func MakeFlatMapper5[T1, T2, T3, T4, T5, R any]( f Mapper5[T1, T2, T3, T4, T5, R], ) FlatMapper5[T1, T2, T3, T4, T5, R]
MakeFlatMapper5 takes a Mapper5 and returns a FlatMapper5 that can be used with FlatMap5.
func MakeFlatMapperNoError5 ¶
func MakeFlatMapperNoError5[T1, T2, T3, T4, T5, R any]( f MapperNoError5[T1, T2, T3, T4, T5, R], ) FlatMapper5[T1, T2, T3, T4, T5, R]
MakeFlatMapperNoError5 takes a MapperNoError5 and returns a FlatMapper5 that can be used with FlatMap5.
type Getter ¶
type Getter[T any] func() T
Getter defines a function that gets the default value for a Result that encapsulates an error.
type Mapper ¶
Mapper defines a function with one argument of any type that returns a single value and an error.
type Mapper2 ¶
Mapper2 defines a function with two arguments of any type that returns a single value and an error.
type Mapper3 ¶
Mapper3 defines a function with three arguments of any type that returns a single value and an error.
type Mapper4 ¶
Mapper4 defines a function with four arguments of any type that returns a single value and an error.
type Mapper5 ¶
Mapper5 defines a function with five arguments of any type that returns a single value and an error.
type MapperErrorOnly ¶
MapperErrorOnly defines a function with one argument of any type that returns an error.
type MapperErrorOnly2 ¶
MapperErrorOnly2 defines a function with two arguments of any type that returns an error.
type MapperErrorOnly3 ¶
MapperErrorOnly3 defines a function with three arguments of any type that returns an error.
type MapperErrorOnly4 ¶
MapperErrorOnly4 defines a function with four arguments of any type that returns an error.
type MapperErrorOnly5 ¶
MapperErrorOnly5 defines a function with five arguments of any type that returns an error.
type MapperNoError ¶
type MapperNoError[T, R any] func( T, ) R
MapperNoError defines a function with one argument of any type that returns a single value.
type MapperNoError2 ¶
type MapperNoError2[T1, T2, R any] func( T1, T2, ) R
MapperNoError2 defines a function with two arguments of any type that returns a single value.
type MapperNoError3 ¶
type MapperNoError3[T1, T2, T3, R any] func( T1, T2, T3, ) R
MapperNoError3 defines a function with three arguments of any type that returns a single value.
type MapperNoError4 ¶
type MapperNoError4[T1, T2, T3, T4, R any] func( T1, T2, T3, T4, ) R
MapperNoError4 defines a function with four arguments of any type that returns a single value.
type MapperNoError5 ¶
type MapperNoError5[T1, T2, T3, T4, T5, R any] func( T1, T2, T3, T4, T5, ) R
MapperNoError5 defines a function with five arguments of any type that returns a single value.
type Result ¶
type Result[T any] interface { // Error returns nil if the Result encapsulates an error or the encapsulated // error if the Result encapsulates an error. Error() error // IsError returns false if the Result encapsulates a value and true if the // Result encapsulates an error. IsError() bool // MustGet returns the encapsulated value if the Result encapsulates a value // and panics if the Result encapsulates an error. MustGet() T // OrElse returns the encapsulated value if the Result encapsulates a value // and returns the given default if the Result encapsulates an error. OrElse(d T) T // OrElseGet returns the encapsulated value if the Result encapsulates a // value and returns the result of calling the given Getter function if the // Result encapsulates an error. OrElseGet(Getter[T]) T }
Result encapsulates a value or an error. An instance implementing this interface will either contain a value or an error. Not both.
func As ¶
As encapsulates the value in the given Result in a new Result of the target type. If the given Result encapsulates an error then an Error Result will be returned of the target type encapsulating the original error.
func CallFlatFunc ¶
CallFlatFunc is a helper function that will call a Result returning function that is encapsulated in a Result. This is useful when deferring a call to a function that was returned in a Result.
func ErrorMap ¶
func ErrorMap[T any](f ErrorMapper, r Result[T]) Result[T]
ErrorMap applies the given ErrorMapper to the Result. If the Result encapsulates a value then the Result is returned unchanged. If the Result encapsulates an error then the error is passed to the ErrorMapper and the returned error is propagated to a Result of the return type.
func FlatMap ¶
func FlatMap[T, R any]( f FlatMapper[T, R], r Result[T], ) Result[R]
FlatMap applies the given FlatMapper to the Result. If the Result encapsulates a value then that value is passed to the FlatMapper and the FlatMapper's return value is returned. If the Result encapsulates an error then the error is propagated to a Result of the return type.
func FlatMap2 ¶
func FlatMap2[T1, T2, R any]( f FlatMapper2[T1, T2, R], r1 Result[T1], r2 Result[T2], ) Result[R]
FlatMap2 applies the given FlatMapper2 to the given Results. If both Results encapsulate a value then those values are passed to the FlatMapper2 and the FlatMapper2's return value is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func FlatMap3 ¶
func FlatMap3[T1, T2, T3, R any]( f FlatMapper3[T1, T2, T3, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], ) Result[R]
FlatMap3 applies the given FlatMapper3 to the given Results. If all Results encapsulate a value then those values are passed to the FlatMapper3 and the FlatMapper3's return value is returned. If any Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func FlatMap4 ¶
func FlatMap4[T1, T2, T3, T4, R any]( f FlatMapper4[T1, T2, T3, T4, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], ) Result[R]
FlatMap4 applies the given FlatMapper4 to the given Results. If all Results encapsulate a value then those values are passed to the FlatMapper4 and the FlatMapper4's return value is returned. If any Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func FlatMap5 ¶
func FlatMap5[T1, T2, T3, T4, T5, R any]( f FlatMapper5[T1, T2, T3, T4, T5, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], r5 Result[T5], ) Result[R]
FlatMap5 applies the given FlatMapper5 to the given Results. If all Results encapsulate a value then those values are passed to the FlatMapper5 and the FlatMapper5's return value is returned. If any Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func FromMaybe ¶
FromMaybe converts the given maybe.Maybe to a Result. If the maybe.Maybe is maybe.Nothing then the Result will have the given error. Otherwise, the Result will have the value of the maybe.Just.
func Map ¶
Map applies the given Mapper to the Result. If the Result encapsulates a value then that value is passed to the Mapper and a Result of the return type is returned. If the Result encapsulates an error then the error is propagated to a Result of the return type.
func Map2 ¶
Map2 applies the given Mapper2 to the given Results. If both Results encapsulate values then those values are passed to the Mapper2 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func Map3 ¶
func Map3[T1, T2, T3, R any]( f Mapper3[T1, T2, T3, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], ) Result[R]
Map3 applies the given Mapper3 to the given Results. If all Results encapsulate values then those values are passed to the Mapper3 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func Map4 ¶
func Map4[T1, T2, T3, T4, R any]( f Mapper4[T1, T2, T3, T4, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], ) Result[R]
Map4 applies the given Mapper4 to the given Results. If all Results encapsulate values then those values are passed to the Mapper4 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func Map5 ¶
func Map5[T1, T2, T3, T4, T5, R any]( f Mapper5[T1, T2, T3, T4, T5, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], r5 Result[T5], ) Result[R]
Map5 applies the given Mapper5 to the given Results. If all Results encapsulate values then those values are passed to the Mapper5 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func MapNoError ¶
func MapNoError[T, R any]( f MapperNoError[T, R], r Result[T], ) Result[R]
MapNoError applies the given MapperNoError to the Result. If the Result encapsulates a value then that value is passed to the MapperNoError and a Result of the return type is returned. If the Result encapsulates an error then the error is propagated to a Result of the return type.
func MapNoError2 ¶
func MapNoError2[T1, T2, R any]( f MapperNoError2[T1, T2, R], r1 Result[T1], r2 Result[T2], ) Result[R]
MapNoError2 applies the given MapperNoError2 to the given Results. If both Results encapsulate values then those values are passed to the MapperNoError2 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func MapNoError3 ¶
func MapNoError3[T1, T2, T3, R any]( f MapperNoError3[T1, T2, T3, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], ) Result[R]
MapNoError3 applies the given MapperNoError3 to the given Results. If all Results encapsulate values then those values are passed to the MapperNoError3 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func MapNoError4 ¶
func MapNoError4[T1, T2, T3, T4, R any]( f MapperNoError4[T1, T2, T3, T4, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], ) Result[R]
MapNoError4 applies the given MapperNoError4 to the given Results. If all Results encapsulate values then those values are passed to the MapperNoError4 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.
func MapNoError5 ¶
func MapNoError5[T1, T2, T3, T4, T5, R any]( f MapperNoError5[T1, T2, T3, T4, T5, R], r1 Result[T1], r2 Result[T2], r3 Result[T3], r4 Result[T4], r5 Result[T5], ) Result[R]
MapNoError5 applies the given MapperNoError5 to the given Results. If all Results encapsulate values then those values are passed to the MapperNoError5 and a Result of the return type is returned. If either Result encapsulates an error then the error of the first argument that has an error (from left to right) is propagated to a Result of the return type.