optionext

package
v5.19.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0, MIT Imports: 6 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

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

Option represents a values that represents a values existence.

nil is usually used on Go however this has two problems: 1. Checking if the return values is nil is NOT enforced and can lead to panics. 2. Using nil is not good enough when nil itself is a valid value.

This implements the sql.Scanner interface and can be used as a sql value for reading and writing. It supports: - String - Bool - Uint8 - Float64 - Int16 - Int32 - Int64 - interface{}/any - time.Time - Struct - when type is convertable to []byte and assumes JSON. - Slice - when type is convertable to []byte and assumes JSON. - Map types - when type is convertable to []byte and assumes JSON.

This also implements the `json.Marshaler` and `json.Unmarshaler` interfaces. The only caveat is a None value will result in a JSON `null` value. there is no way to hook into the std library to make `omitempty` not produce any value at this time.

func None

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

None creates an empty Option that represents no values.

func Some

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

Some creates a new Option with the given values.

func (Option[T]) IsNone

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

IsNone returns true if the option is empty.

func (Option[T]) IsSome

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

IsSome returns true if the option is not empty.

func (Option[T]) MarshalJSON

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

MarshalJSON implements the `json.Marshaler` interface.

func (*Option[T]) Scan

func (o *Option[T]) Scan(value any) error

Scan implements the sql.Scanner interface.

func (*Option[T]) UnmarshalJSON

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

UnmarshalJSON implements the `json.Unmarshaler` interface.

func (Option[T]) Unwrap

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

Unwrap returns the values if the option is not empty or panics.

func (Option[T]) Value

func (o Option[T]) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

Jump to

Keyboard shortcuts

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