goption

package module
v0.0.0-...-7b52a63 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 9 Imported by: 0

README

goption

Test Go Reference

Optional values for Go. Inspired by the Rust implementation.

Why?

I created this to provide greater coherency of optional handling across the language. In sql, we use NullType, in json and encoding friends we use pointers, and elsewhere we use ok. This package adds a more coherent optional experience across the language and is well tested.

Features

Highly tested and aims at high utility. Attempts to follow a monadic design where if the wrapped type T implements some interface, so should Option[T]. The following are implemented:

  • json.Marshaler
  • json.Unmarshaler
  • fmt.Stringer
  • fmt.GoStringer
  • sql.Scanner
  • sql.driver.Valuer

If there are any more interfaces which should be wrapped, please open an issue or a PR. All features must be tested.

Examples

Basic

To declare a present optional value do:

myOption := Some[int](3)

To declare an empty optional value do:

empty := None[int]()

The default value for an Option[T] is None[T]():

var maybeInt Option[int]
if !maybeInt.Ok() {
  fmt.Println("The value isn't present!") // This will print, maybeInt hasn't been set.
}

To check if an optional value is present do:

opt := functionReturningOption()
if opt.Ok() {
  fmt.Println(opt.Unwrap())
}

To convert a pointer into an optional value do:

var pointer *int
myOption := FromRef(pointer) // Empty for nil pointers
sql

To move values in and out of a sql database do:

db.Exec(`
  INSERT INTO test (optional_field) VALUES ($1);
`, Some[int](0))

rows, err := db.Query(`
  SELECT * FROM test;
`)
for rows.Next() {
  var opt Option[int]
  rows.Scan(&opt)
  fmt.Println(opt)
}
json
type MyStruct struct {
  Foo Option[int]
}

var v MyStruct
json.Unmarshal([]byte(`{}`), &v)
if v.Foo.Ok() {
  fmt.Println(v.Foo.Unwrap())
}

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 value whose presence is optional.

func Apply

func Apply[In, Out any](in Option[In], f func(In) Out) Option[Out]

Apply f to the optional value.

func Do

func Do[T any](f func() T) (o Option[T])

Do runs the function f which may panic. If f does not panic Some(f()) is returned. Otherwise none is returned.

func FromRef

func FromRef[T any](t *T) Option[T]

FromRef returns an Option whose underlying value is present if t is not nil. Otherwise, it returns an empty optional value.

func None

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

None returns an empty optional value.

func Some

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

Some returns an Option whose underlying value is present.

func (Option[T]) Expect

func (o Option[T]) Expect(msg string) T

Expect unwraps o and panics with msg if it's empty.

func (*Option[T]) ExpectRef

func (o *Option[T]) ExpectRef(msg string) *T

ExpectRef unwraps o and panics with msg if it's empty.

func (Option[T]) Get

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

Get returns the underlying value and a boolean indicating if it's present.

func (Option[T]) GoString

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

GoString implements fmt.GoStringer

func (Option[T]) MarshalJSON

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

MarshalJSON marshals the underlying option data

func (Option[T]) Ok

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

Ok returns if the optional is present.

func (*Option[T]) Scan

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

Scan implements sql.Scanner for Options

func (Option[T]) String

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

String implements fmt.Stringer

func (*Option[T]) UnmarshalJSON

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

UnmarshalJSON unmarshals the underlying

func (Option[T]) Unwrap

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

Unwrap forcefully unwraps the Optional value. If the optional is not ok this function will panic.

func (Option[T]) UnwrapOr

func (o Option[T]) UnwrapOr(def T) T

UnwrapOr unwraps the optional if it's present, otherwise it returns default.

func (Option[T]) UnwrapOrDefault

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

UnwrapOrDefault unwraps T if it's present, otherwise it returns the default value for T.

func (*Option[T]) UnwrapRef

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

UnwrapRef returns a reference to the underlying T.

func (Option[T]) Value

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

type RawBytes

type RawBytes []byte

Jump to

Keyboard shortcuts

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