optional

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

README

License

Optional

Optional is a simple package which provides Optional[T] type and helper functions for sql.Null* types.

Usage example

	val := None[int]()
	val = Of(2)
	slc, err := val.MarshalJSON()
	fmt.Println(string(slc), err)

	ptr := val.Ptr()
	fmt.Println(*ptr)
	// Output:
	// 2 <nil>
	// 2

Inspiration

Documentation

Overview

Example
val := None[int]()
val = Of(2)
slc, err := val.MarshalJSON()
fmt.Println(string(slc), err)

ptr := val.Ptr()
fmt.Println(*ptr)
Output:

2 <nil>
2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

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

Optional represents a type T that may be "null" - that, which value is missing.

func None

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

None is helper function which returns empty Optional

func Of

func Of[T any](val T) Optional[T]

Of creates a new Optional with valid state and provided value.

func (Optional[T]) Get

func (o Optional[T]) Get() (T, bool)

Get returns a value, true on non-empty optional and zero-value and false otherwise.

func (Optional[T]) MarshalJSON

func (o Optional[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Optional[T]) Ptr

func (o Optional[T]) Ptr() *T

Ptr returns a pointer to value or nil pointer. It is safe to change the value inside the pointer (if the T type supports this).

func (*Optional[T]) UnmarshalJSON

func (o *Optional[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler

func (Optional[T]) Valid

func (o Optional[T]) Valid() bool

Valid tells of there any value inside the optional.

func (Optional[T]) ValueOr

func (o Optional[T]) ValueOr(val T) T

ValueOr returns a value on non-empty optional or provided value otherwise.

func (Optional[T]) ValueOrZero

func (o Optional[T]) ValueOrZero() T

ValueOrZero returns a value on non-empty optional and zero-value otherwise.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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