option

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 9 Imported by: 1

README

github.com/typomaker/option

Optional type for the Golang. A generic value wrapper that adds two additional value representations:

  • Zero - Same as undefined. Basic value state, persists until None or Some value is assigned.
  • None - Same as null. A logically explicit absence of a value.
  • Some - Usual value representation.

Usage

import "github.com/typomaker/option"
Value initialization and checks.
import "github.com/typomaker/option"
var value option.Option[string]
if value.IsZero() { // if value is undefined.
    value = option.Some("foo") // then define it using Some[string](...).
}
if value.IsSome() { // if value is defined and not null
    value = option.None[string]() // then set it null using None[string]() 
}
if value.IsNone() { // if value is defined and null
    value = option.Option[string]{} // then undefine it with the zero Option struct.
}

Value getting.
var value option.Option[string]

value.Get() // returns value if defined not null, otherwise panics.
value.GetOr("fallback") // returns value if defined not null, otherwise passed value.
value.GetOrZero() // always returns value, if undefined or null then zero value.
value.GetOrFunc(func() string {return "fallback"}) // returns a value if defined not null. otherwise, returns the result of the passed function.
Nilable value.
var value *string 
var optional = option.Nilable(value) // If value is not nil, then returns Some[string](*value), otherwise None[string]().
value = optional.GetNilable() // returns &value is some, otherwise returns nil.
Convert value to optional.

SomeOrNone function returns Some[T](...) for non zero and not nil value, otherwise returns None[T]()

fmt.Printf("%#v\n", SomeOrNone(0))
fmt.Printf("%#v\n", SomeOrZero(0))
fmt.Printf("%#v\n", SomeOrNone(1))
fmt.Printf("%#v\n", SomeOrNone((*string)(nil)))
fmt.Printf("%#v\n", SomeOrNone("123123"))
// Output:
// option.None[int]()
// option.Option[int]{}
// option.Some[int](1)
// option.None[*string]()
// option.Some[string]("123123")
OneOf helper

Returns first some option from passed. If passed doesn't contain some value, then returns None[T]().

var value1 = option.None[int]()
var value2 = option.Some[int](1)
var value3 = option.Some[int](2)

fmt.Printf("%#v", option.OneOf(value1, value2, value3))

// Output:
// Some[int](1)
GetOf helper

Returns value of first some option from passed.

var value1 = option.None[int]()
var value2 = option.Some[int](1)
var value3 = option.Some[int](2)

fmt.Printf("%#v", option.GetOf(value1, value2, value3))

// Output:
// 1
PickOf helper

Returns all values of passed options, exclude the zero and none option.

var value1 = option.None[int]()
var value2 = option.Some[int](1)
var value3 = option.None[int]()
var value4 = option.Option[int]{}

fmt.Printf("%#v", option.PickOf(value1, value2, value3, value4))

// Output:
// []int{1, 2}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetOf

func GetOf[T any](op ...Option[T]) T

GetOf returns value of first some value. If there are no some value, then return zero value.

Example
var value1 = option.None[int]()
var value2 = option.Some[int](1)
var value3 = option.Some[int](2)

fmt.Printf("%#v", option.GetOf(value1, value2, value3))
Output:

1

func IsNone

func IsNone(nn ...Noneable) (ok bool)

IsNone returns a true if all passed values is none.

func IsSome

func IsSome(ss ...Someable) (ok bool)

IsSome returns a true if all passed values is some.

func IsZero

func IsZero(zz ...Zeroable) (ok bool)

IsZero returns a true if all passed values is zero.

func PickOf

func PickOf[T any](op ...Option[T]) []T

PickOf returns all some values.

Example
var value1 = option.None[int]()
var value2 = option.Some[int](1)
var value3 = option.Some[int](2)

fmt.Printf("%#v", option.PickOf(value1, value2, value3))
Output:

[]int{1, 2}

Types

type Bool

type Bool = Option[bool]

type Complex64

type Complex64 = Option[complex64]

type Complex128

type Complex128 = Option[complex128]

type Duration

type Duration = Option[time.Duration]

type Float32

type Float32 = Option[float32]

type Float64

type Float64 = Option[float64]

type Int

type Int = Option[int]

type Int8

type Int8 = Option[int8]

type Int16

type Int16 = Option[int16]

type Int32

type Int32 = Option[int32]

type Int64

type Int64 = Option[int64]

type Noneable

type Noneable interface{ IsNone() bool }

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}
Example (States)
var value option.Option[string]
fmt.Println("value is zero, same as undefined:", value.IsZero())

value = option.None[string]()
fmt.Println("value is none, defined and same as null:", value.IsNone())

value = option.Some[string]("hello world")
fmt.Println("value is some, defined and not null:", value.IsSome())
Output:

value is zero, same as undefined: true
value is none, defined and same as null: true
value is some, defined and not null: true

func Nilable added in v1.0.1

func Nilable[T any](v *T) Option[T]

func None

func None[T any]() Option[T]

func OneOf added in v1.0.1

func OneOf[T any](op ...Option[T]) Option[T]

OneOf returns first some option. If there are no some options, then returns none value.

Example
var value1 = option.None[int]()
var value2 = option.Some[int](1)
var value3 = option.Some[int](2)

fmt.Printf("%#v", option.OneOf(value1, value2, value3))
Output:

option.Some[int](1)

func Some

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

func SomeOrNone added in v1.0.1

func SomeOrNone[T any](value T) Option[T]

SomeOrNone returns some if the value is non zero and nil. Othrwise returns None

Example:
	log.Println(SomeOrNone(0)) // None[int]()
	log.Println(SomeOrNone(1)) // Some[int](1)
	log.Println(SomeOrNone((*string)(nil))) // None[*string]()
	ptr := "foo"
	log.Println(SomeOrNone(&ptr)) // Some[*string](foo)
Example
fmt.Printf("%#v\n", SomeOrNone(0))
fmt.Printf("%#v\n", SomeOrNone(1))
fmt.Printf("%#v\n", SomeOrNone((*string)(nil)))
fmt.Printf("%#v\n", SomeOrNone("123123"))
Output:

option.None[int]()
option.Some[int](1)
option.None[*string]()
option.Some[string]("123123")

func SomeOrZero added in v1.0.1

func SomeOrZero[T any](value T) Option[T]

SomeOrZero returns some if the value is non zero and nil. Othrwise returns None

Example:
	log.Println(SomeOrNone(0)) // Option[int]{}
	log.Println(SomeOrNone(1)) // Some[int](1)
	log.Println(SomeOrNone((*string)(nil))) // Option[*string]{}
	ptr := "foo"
	log.Println(SomeOrNone(&ptr)) // Some[*string](foo)
Example
fmt.Printf("%#v\n", SomeOrZero(0))
fmt.Printf("%#v\n", SomeOrZero(1))
fmt.Printf("%#v\n", SomeOrZero((*string)(nil)))
fmt.Printf("%#v\n", SomeOrZero("123123"))
Output:

option.Option[int]{}
option.Some[int](1)
option.Option[*string]{}
option.Some[string]("123123")

func (Option[T]) Get

func (o Option[T]) Get() T

Get returns a value if it some, in other case panics.

func (Option[T]) GetNilable added in v1.0.1

func (o Option[T]) GetNilable() *T

GetNilable returns the nil value if the option is none. Pointer is refers to a copy of the origin value, so that means any changes to the pointer don't affect the value of the option.

Example
fmt.Println("none", option.None[int]().GetNilable())
fmt.Printf("some %v\n", *option.Some("1").GetNilable())
fmt.Println("zero", option.Option[int]{}.GetNilable())
Output:

none <nil>
some 1
zero <nil>

func (Option[T]) GetOr

func (o Option[T]) GetOr(value T) T

GetOr returns the value if the option is none.

Example
fmt.Println("none", option.None[int]().GetOr(1))
fmt.Println("some", option.Some(2).GetOr(1))
fmt.Println("zero", option.Option[int]{}.GetOr(3))
Output:

none 1
some 2
zero 3

func (Option[T]) GetOrFunc

func (o Option[T]) GetOrFunc(getter func() T) T

GetOrFunc retunrs value from getter if the option is none

Example
fmt.Println("none", option.None[int]().GetOrFunc(func() int { return 1 }))
fmt.Println("some", option.Some(2).GetOrFunc(func() int { return 1 }))
fmt.Println("zero", option.Option[int]{}.GetOrFunc(func() int { return 3 }))
Output:

none 1
some 2
zero 3

func (Option[T]) GetOrZero

func (o Option[T]) GetOrZero() T

GetOrZero returns the zero value if the option is none.

Example
fmt.Println("none", option.None[int]().GetOrZero())
fmt.Println("some", option.Some(1).GetOrZero())
fmt.Println("zero", option.Option[int]{}.GetOrZero())
Output:

none 0
some 1
zero 0

func (Option[T]) GoString

func (o Option[T]) GoString() string

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

IsNone returns a true if value is none.

func (Option[T]) IsSome

func (o Option[T]) IsSome() bool

IsNone returns a true if value is some.

func (Option[T]) IsZero

func (o Option[T]) IsZero() bool

IsZero returns a true if value is zero.

func (Option[T]) MarshalJSON

func (o Option[T]) MarshalJSON() (b []byte, err error)

MarshalJSON is a implementation of the json.Marshaler.

func (Option[T]) String

func (o Option[T]) String() string

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON is a implementation of the json.Unmarshaler.

type Someable

type Someable interface{ IsSome() bool }

type String

type String = Option[string]

type Time

type Time = Option[time.Time]

type Uint

type Uint = Option[uint]

type Uint8

type Uint8 = Option[uint8]

type Uint16

type Uint16 = Option[uint16]

type Uint32

type Uint32 = Option[uint32]

type Uint64

type Uint64 = Option[uint64]

type Zeroable

type Zeroable interface{ IsZero() bool }

Jump to

Keyboard shortcuts

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