option

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2024 License: MIT Imports: 4 Imported by: 1

README

Option Type for Go

The Option package provides an Option type that represents encapsulation of an optional value; a value that may either contain a value (Some) or not contain a value (None), similar to Option in Rust.

Features

  • Some: Create an Option that contains a value.
  • None: Create an Option that represents no value.
  • Ok: Return the value if it exists.
  • Cause: Return the cause of None if it is due to an error.
  • Process: Transform the Option value with a function that might fail.
  • Map: Transform the Option value with a function that cannot fail.
  • Flatten: Nested Option values into a single Option.
  • Wrap: Wraps a value and an error into an Option.
  • WrapFn: Wraps a function that returns a value and an error into a function that returns an Option.

Installation

To use the Option package in your Go project, place the package in your project directory or a suitable location in your GOPATH. Import it using:

go get -u github.com/AY7295/go-option

Usage Examples

Here are some basic examples of how to use the Option type:

Creating Some and None
opt1 := option.Some(10)
opt2 := option.None[int]()
Processing an Option
opt := option.Some(5)
result := option.Process(opt, func(v int) (int, error) {
    if v == 0 {
        return 0, errors.New("zero value")
    }
    return 2 * v, nil
})
Mapping over an Option
opt := option.Some(10)
mapped := option.Map(opt, func(v int) int {
    return v + 1
})
Flattening an Option
doubleWrapped := option.Some(option.Some(20))
flattened := option.Flatten(doubleWrapped)
Testing

To run the tests for the Option package, navigate to the package directory and execute:

go test

This will run all the tests defined in option_test.go.

Documentation

Overview

Package option provides a generic implementation of the Option type, similar to Rust's Option type. It is used to encapsulate an optional value: a value that may either contain a value of type T or may be empty.

Index

Constants

This section is empty.

Variables

View Source
var (
	Nil = errors.New("nil value")
)

Nil is a pre-defined error to represent a nil value being incorrectly used to create an Option.

Functions

func IsNone added in v1.0.1

func IsNone[T any](opt Option[T]) bool

IsNone returns true if the option does not hold a value (i.e., cause is not nil).

func IsSome added in v1.0.1

func IsSome[T any](opt Option[T]) bool

IsSome returns true if the option holds a value (i.e., cause is nil).

func SetJsonCoder added in v1.1.2

func SetJsonCoder(coder JsonCoder)

func WrapFn added in v1.1.1

func WrapFn[T any](fn func() (T, error)) func() Option[T]

WrapFn wraps a function that returns a value and an error into a function that returns an Option.

Types

type JsonCoder added in v1.1.2

type JsonCoder interface {
	Marshal(any) ([]byte, error)
	Unmarshal([]byte, any) error
}

type Option

type Option[T any] interface {
	Cause() error // Cause returns an error if the option is none due to an error.
	Ok() T        // Ok returns the value if present, otherwise a zero value.
}

Option is a generic interface that defines methods for handling optional values.

func Flatten

func Flatten[T any](opt Option[Option[T]]) Option[T]

Flatten converts an Option[Option[T]] into a single Option[T]. If the outer Option is none, it returns a none Option with the same error; otherwise, it returns the inner Option.

func Map

func Map[T, U any](opt Option[T], fn func(T) U) Option[U]

Map applies a function to the value within the Option, returning a new Option with the result. If the original Option is none, the function is not executed and a new none Option with the original error is returned.

func None

func None[T any](causes ...error) Option[T]

None creates an Option that contains no value but an error. If no specific error is provided, it defaults to using Nil.

func Process

func Process[T, U any](opt Option[T], fn func(T) (U, error)) Option[U]

Process applies a function to the value within the Option, returning a new Option with the result. If the original Option is none, the function is not executed and a new none Option with the original error is returned.

func Some

func Some[T any](t T) Option[T]

Some returns an Option containing the value t. t must be valid!

func Wrap added in v1.1.1

func Wrap[T any](val T, errs ...error) Option[T]

Wrap wraps a value and an error into an Option.

Jump to

Keyboard shortcuts

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