opt

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

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

func New

func New[T any](item T) Optional[T]

func Nil

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

func None

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

func (Optional[T]) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface. It returns "null" if the value is either absent or nil.

func (Optional[T]) Nil

func (e Optional[T]) Nil() bool

func (Optional[T]) OrElse

func (e Optional[T]) OrElse(d T) T

OrElse checks if the value of Optional struct exists and is not nil, if so it returns it, otherwise it returns the given argument d.

func (Optional[T]) Present

func (e Optional[T]) Present() bool

func (Optional[T]) Ptr

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

Ptr returns a pointer to the inner value if the value is present and not nil. Otherwise it returns nil.

func (*Optional[T]) Scan

func (e *Optional[T]) Scan(src any) error

Scan implements sql.Scanner interface. Upon calling this function (e.g. from sql.Rows Scan function), the isPresent is set to true. It sets isNil to true if the database value (src) is null, otherwise it sets the value to [Optional.Item] and sets isNil to false.

func (*Optional[T]) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface. When this function is called (e.g. by json.Unmarshal), the isPresent value is set to true. This is because it means there is a field matching this value field name, and there is a value for it. If the JSON value of data is null, the soil is set to true. If the json.Unmarshal returns error isPresent and isNil are set to false before returning the error.

func (Optional[T]) Valid

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

func (Optional[T]) Value

func (e Optional[T]) Value() (driver.Value, error)

Value implements driver.Valuer interface. If the wrapped value Item implements driver.Valuer that Value() function will be called, otherwise it will return the Item directly. driver.Valuer mentions that the returned value must be of type:

  • [int64]

  • [float64]

  • [bool]

  • [[]byte]

  • [string]

  • time.Time

It's up to the end-usage of the Optional struct whether this requirement will be met depending on the type of [Optional.Item].

Jump to

Keyboard shortcuts

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