optional

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: MIT Imports: 4 Imported by: 0

README

Optional

A GO's "generics" version of Optionals, pretty much borrowed from Java's JDK11, but probably more "idiomatic", to avoid panics at runtime.

Usage

import (
    "github.com/nebiros/optional"
)

func main() {
    var (
        v   *string
        tmp = "something"
    )
    
    v = &tmp
    
    ov := optional.OfNillable(v)

    err := doSomething(ov)
    if err != nil {
        panic(err)
    }
}

func doSomething(s Optional[string]) error {
    if !s.IsPresent() {
        return fmt.Errorf("v not present")
    }
    
    sv, err := s.Get()
    if err != nil {
        return err
    }
    
    fmt.Println("sv: " + sv)
}

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/nebiros/optional"
)

func main() {
	ov := optional.New("something")

	sv, err := ov.Get()
	if err != nil {
		panic(err)
	}

	fmt.Println("sv: " + sv)
}
Output:

Example (Nillable)
package main

import (
	"fmt"

	"github.com/nebiros/optional"
)

func main() {
	var (
		v   *string
		tmp = "something"
	)

	v = &tmp

	ov := optional.OfNillable(v)

	if !ov.IsPresent() {
		panic("v not present")
	}

	sv, err := ov.Get()
	if err != nil {
		panic(err)
	}

	fmt.Println("sv: " + sv)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoValuePresent = errors.New("optional: no value present")
)

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]

func FlatMap

func FlatMap[T, U any](o Optional[T], f func(v T) Optional[U]) Optional[U]

func Map

func Map[T, U any](o Optional[T], f func(v T) U) Optional[U]

func New

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

func OfNillable

func OfNillable[T any](value *T) Optional[T]

func (*Optional[T]) Filter

func (o *Optional[T]) Filter(f func(v T) bool) Optional[T]

func (*Optional[T]) Get

func (o *Optional[T]) Get() (T, error)

func (*Optional[T]) IsEmpty

func (o *Optional[T]) IsEmpty() bool

func (*Optional[T]) IsPresent

func (o *Optional[T]) IsPresent() bool

func (Optional[T]) MarshalJSON

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

func (*Optional[T]) MustGet

func (o *Optional[T]) MustGet() T

func (*Optional[T]) Or

func (o *Optional[T]) Or(f func(v T) Optional[T]) Optional[T]

func (*Optional[T]) OrElse

func (o *Optional[T]) OrElse(other T) T

func (*Optional[T]) OrElseErr

func (o *Optional[T]) OrElseErr() (T, error)

func (*Optional[T]) Scan

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

func (*Optional[T]) UnmarshalJSON

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

func (*Optional[T]) Value

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

Jump to

Keyboard shortcuts

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