opt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: MIT Imports: 3 Imported by: 0

README

Go Optionals

This package is a tiny implementation of Optionals in go, using the new go1.18 generics.

Optionals are useful because sometimes errors are actually errors, and nil is a valid response, but you want to retain type safety.

Example:

import (
	"fmt"
	"github.com/frenchie4111/go-generic-optional"
)

type database interface {
	getUser(userID string) (*User, error)
}
const db database

func getUser(userID: string) Optional[User], error {
	if !db.connected() {
		return opt.New[User](), fmt.Errorf("Database not connected")
	}

	user, err := db.getUser()
	if err {
		return opt.New[User](), fmt.Errorf("Failed to getUser: %v", err)
	}

	return opt.Of(user)
}

func main() {
	user, err := getUser("user-id-1")

	if err != nil {
		// handle error cases
		return
	}

	opt.If(user, func(user User)) {
		fmt.Println("Found user", user.firstName)
	}
}

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 {
	// contains filtered or unexported fields
}

func If

func If[T any, R any](optional Optional[T], handler func(T) R) Optional[R]

If allows you to handle an optional if it exists, otherwise return

func New

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

New creates a new Optional without a value.

func Of

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

Of creates a new Optional with a value.

func (Optional[T]) Exists

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

Returns true if the optional value has been set

func (Optional[T]) Get

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

Get returns the value and whether it exists. It's invalid to use the returned value if the bool is false.

func (Optional[Value]) GetOrElse

func (o Optional[Value]) GetOrElse(defaultValue Value) Value

GetOrElse returns the value if it exists and returns defaultValue otherwise.

func (Optional[Value]) MarshalJSON

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

MarshalJSON implements the json.Marshaller interface. Optionals wil marshall and unmarshall as a nullable json field. Value type must also implement json.Marshaller.

func (Optional[Value]) MustGet

func (o Optional[Value]) MustGet() Value

MustGet returns the value if it exists and panics otherwise.

func (*Optional[Value]) Scan

func (o *Optional[Value]) Scan(value any) error

Scan implements the Scanner interface.

func (*Optional[Value]) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface. Optionals will marshall and unmarshall as a nullable json field. Value type must also implement json.Unmarshaler.

func (Optional[Value]) Value

func (o Optional[Value]) Value() (driver.Value, error)

Value implements the Valuer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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