goption

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 5 Imported by: 0

README

Goption

goption

A way to handle nil values.

Requirements

Use Go 1.18+ is a must. Goption uses Generics.

Description

This is a copy of Java Optional. It contains almost all functions

  • Empty
  • Of
  • Optional.Get
  • Optional.IsPresent
  • Optional.OrElseError
  • Optional.OrElse

And others made specifically to go:

  • Optional.MustGet

SQL

You can use goption.Optional to sql package. Optional implements sql.Scanner and sql.Valuer

JSON Marshall

Unfortunately ,omitempty is not supported by now due how json.Marshal works. For more details can see this link.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSuchElement = errors.New("no value in this optional")
)

Functions

This section is empty.

Types

type Optional

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

func Empty

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

Empty returns an empty Optional instance.

func Of

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

Of returns an Optional with the specified present value. It does not matters if value is nil

func (Optional[T]) Get

func (c Optional[T]) Get() (T, error)

Get when a value is present returns the value, otherwise throws ErrNoSuchElement.

func (Optional[T]) IsPresent

func (c Optional[T]) IsPresent() bool

IsPresent returns true if there is a value present, otherwise false. It recognizes an empty slice as not present so it returns false

func (Optional[T]) MarshalJSON

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

func (Optional[T]) MustGet

func (c Optional[T]) MustGet() T

MustGet retrieves only a valid value. If is not present it panics with ErrNoSuchElement

func (Optional[T]) OrElse

func (c Optional[T]) OrElse(other T) T

OrElse returns the value if present, otherwise return other.

func (Optional[T]) OrElseError

func (c Optional[T]) OrElseError(err error) (T, error)

OrElseError return the contained value, if present, otherwise returns the given error.

func (*Optional[T]) Scan

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

Scan assigns a value from a database driver.

The src value will be of one of the following types:

int64
float64
bool
[]byte
string
time.Time
nil - for NULL values

An error should be returned if the value cannot be stored without loss of information.

Reference types such as []byte are only valid until the next call to Scan and should not be retained. Their underlying memory is owned by the driver. If retention is necessary, copy their values before the next call to Scan.

func (*Optional[T]) UnmarshalJSON

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

func (*Optional[T]) UnmarshalText added in v0.4.0

func (cb *Optional[T]) UnmarshalText(text []byte) error

func (Optional[T]) Value

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

Jump to

Keyboard shortcuts

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