result

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error = errors.Error

type Optional added in v0.5.0

type Optional interface {
	IsNone() bool
}

type Result

type Result[T any, E Optional] struct {
	// contains filtered or unexported fields
}

result_ of an operation.

Designed to replace the return pattern of (value, error). result_ is either a value or an error. Ideally, the internal err would be a non-interface type, but kept as error for backwards compatibility. The internal err is designed to be a singular error and not a linked list of errors. This removes a ton of complexity and uncertainty in the error chain and error usage/lifecycle.

Example
package main

import (
	"context"
	"fmt"

	"github.com/wspowell/errors"
	"github.com/wspowell/errors/result"
)

type Result[T any, E result.Optional] struct {
	result.Result[T, E]
}

func Ok[T any, E result.Optional](value T) Result[T, E] {
	return Result[T, E]{result.Ok[T, E](value)}
}

func Err[T any, E result.Optional](err E) Result[T, E] {
	return Result[T, E]{result.Err[T](err)}
}

func main() {
	exampleFn := func(ctx context.Context, success bool) Result[int, Error] {
		if success {
			return Ok[int, Error](1)
		}

		return Err[int](errors.New(ctx, errErrorFailure))
	}

	ctx := context.Background()

	// Success result.
	fmt.Println(exampleFn(ctx, true).IsOk())
	fmt.Println(exampleFn(ctx, true).Error().Error())
	fmt.Println(exampleFn(ctx, true).Value())
	fmt.Println(exampleFn(ctx, true).ValueOr(2))
	fmt.Println(exampleFn(ctx, true).ValueOrPanic())

	// Error result.
	fmt.Println(exampleFn(ctx, false).IsOk())
	fmt.Println(exampleFn(ctx, false).Error().Error())
	fmt.Println(exampleFn(ctx, false).Value())
	fmt.Println(exampleFn(ctx, false).ValueOr(2))
	//fmt.Println(exampleFn(ctx, false).ValueOrPanic()) // Will panic

}
Output:

true

1
1
1
false
failure
0
2

func Err

func Err[T any, E Optional](err E) Result[T, E]

Err result. Used upon failure.

func Ok

func Ok[T any, E Optional](result T) Result[T, E]

Ok result. Used upon success.

func Then

func Then[T any, S any, E Optional](ctx context.Context, self Result[T, E], f func(context.Context, T) Result[S, E]) Result[S, E]

func (Result[T, E]) Error

func (self Result[T, E]) Error() E

Error of the result.

The Error value can never be `nil`, so it is recommended to use Result.IsOk(). Another alternative is to call Result.Error().IsNone().

func (Result[T, E]) IsOk

func (self Result[T, E]) IsOk() bool

IsOk then return true, false otherwise.

func (Result[T, E]) Result

func (self Result[T, E]) Result() (T, E)

Result decomposes into the basic (T, error) return value.

Useful when decomposing into variables for custom evaluation.

func (Result[T, E]) Value

func (self Result[T, E]) Value() T

Value of the Ok result.

Note: If called on an error result, this will be the zero value of T.

func (Result[T, E]) ValueOr

func (self Result[T, E]) ValueOr(defaultValue T) T

ValueOr default value if not an Ok result.

func (Result[T, E]) ValueOrPanic

func (self Result[T, E]) ValueOrPanic() T

ValueOrPanic if not an Ok result.

It is recommended to only call this during app initialization. Otherwise, use Result.ValueOr().

type When

type When[T any, S any, E Optional] Result[T, E]

func (When[T, S, E]) Then

func (self When[T, S, E]) Then(ctx context.Context, fn func(context.Context, T) Result[S, E]) Result[S, E]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL