optional

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2017 License: MIT Imports: 0 Imported by: 27

README

Optional

Release Software License Go Doc Go Report Card SayThanks.io

Optional is a tool that generates 'optional' type wrappers around a given type T.

It is also a library that provides optional types for the primitive Go types.

Motivation

Ever had to write a test where you want to assert something only if a value is present?

tests :=  []struct {
  catchPhrase string
  catchPhrasePresent bool
} {
    { "wubba dub dub", true },
    { "", false },
}

...

if test.catchPhrasePresent {
  assert.Equal(t, test.catchPhrase, catchPhrase)
}

Now you can simplify all that with optional types:

tests :=  []struct {
  catchPhrase optional.String
} {
    { optional.OfString("wubba dub dub") },
    { optional.EmptyString() },
  }
}

...

if test.catchPhrase.Present() {
  assert.Equal(t, test.catchPhrase.Get(), catchPhrase)
}

Inspiration

Tool

Install

go get -u github.com/markphelps/optional/cmd/optional

Usage

Typically this process would be run using go generate, like this:

//go:generate optional -type=Foo

running this command:

optional -type=Foo

in the same directory will create the file optional_foo.go containing a definition of:

type OptionalFoo struct {
	...
}

The default type is OptionalT or optionalT (depending on if the type is exported) and output file is optional_t.go. This can be overridden with the -output flag.

Library

Usage
import (
	"fmt"

	"github.com/markphelps/optional"
)

s := optional.OfString("foo")

if s.Present() {
  fmt.Println(s.Get())
}

t := optional.EmptyString()
fmt.Println(t.OrElse("bar"))

See example_test.go and the documentation for more usage.

Contributing

  1. Fork it (https://github.com/markphelps/optional/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Documentation

Overview

Example (Get)
package main

import (
	"fmt"

	"github.com/markphelps/optional"
)

func main() {
	values := []optional.String{
		optional.OfString("foo"),
		optional.OfString(""),
		optional.OfString("bar"),
		optional.EmptyString(),
	}

	for _, v := range values {
		if v.Present() {
			fmt.Println(v.Get())
		}
	}
}
Output:

foo

bar
Example (If)
package main

import (
	"fmt"

	"github.com/markphelps/optional"
)

func main() {
	values := []optional.String{
		optional.OfString("foo"),
		optional.OfString(""),
		optional.OfString("bar"),
		optional.EmptyString(),
	}

	for _, v := range values {
		v.If(func(s string) {
			fmt.Println("present")
		})
	}

}
Output:

present
present
present
Example (OrElse)
package main

import (
	"fmt"

	"github.com/markphelps/optional"
)

func main() {
	values := []optional.String{
		optional.OfString("foo"),
		optional.OfString(""),
		optional.OfString("bar"),
		optional.EmptyString(),
	}

	for _, v := range values {
		fmt.Println(v.OrElse("not present"))
	}

}
Output:

foo

bar
not present

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	// contains filtered or unexported fields
}

Bool is an optional bool

func EmptyBool

func EmptyBool() Bool

EmptyBool returns an empty optional.Bool

func OfBool

func OfBool(b bool) Bool

OfBool creates a optional.Bool from a bool

func (*Bool) Get

func (o *Bool) Get() bool

Get returns the bool value

func (*Bool) If added in v0.2.0

func (o *Bool) If(f func(b bool))

If calls the function f with the value if the value is present

func (*Bool) OrElse

func (o *Bool) OrElse(b bool) bool

OrElse returns the bool value or a default value if the value is not present

func (*Bool) Present

func (o *Bool) Present() bool

Present returns whether or not the value is present

func (*Bool) Set

func (o *Bool) Set(b bool)

Set sets the bool value

type Byte

type Byte struct {
	// contains filtered or unexported fields
}

Byte is an optional byte

func EmptyByte

func EmptyByte() Byte

EmptyByte returns an empty optional.Byte

func OfByte

func OfByte(b byte) Byte

OfByte creates a optional.Byte from a byte

func (*Byte) Get

func (o *Byte) Get() byte

Get returns the byte value

func (*Byte) If added in v0.2.0

func (o *Byte) If(f func(b byte))

If calls the function f with the value if the value is present

func (*Byte) OrElse

func (o *Byte) OrElse(b byte) byte

OrElse returns the byte value or a default value if the value is not present

func (*Byte) Present

func (o *Byte) Present() bool

Present returns whether or not the value is present

func (*Byte) Set

func (o *Byte) Set(b byte)

Set sets the byte value

type Complex128

type Complex128 struct {
	// contains filtered or unexported fields
}

Complex128 is an optional complex128

func EmptyComplex128

func EmptyComplex128() Complex128

EmptyComplex128 returns an empty optional.Complex128

func OfComplex128

func OfComplex128(c complex128) Complex128

OfComplex128 creates a optional.Complex128 from a complex128

func (*Complex128) Get

func (o *Complex128) Get() complex128

Get returns the complex128 value

func (*Complex128) If added in v0.2.0

func (o *Complex128) If(f func(c complex128))

If calls the function f with the value if the value is present

func (*Complex128) OrElse

func (o *Complex128) OrElse(c complex128) complex128

OrElse returns the complex128 value or a default value if the value is not present

func (*Complex128) Present

func (o *Complex128) Present() bool

Present returns whether or not the value is present

func (*Complex128) Set

func (o *Complex128) Set(c complex128)

Set sets the complex128 value

type Complex64

type Complex64 struct {
	// contains filtered or unexported fields
}

Complex64 is an optional complex64

func EmptyComplex64

func EmptyComplex64() Complex64

EmptyComplex64 returns an empty optional.Complex64

func OfComplex64

func OfComplex64(c complex64) Complex64

OfComplex64 creates a optional.Complex64 from a complex64

func (*Complex64) Get

func (o *Complex64) Get() complex64

Get returns the complex64 value

func (*Complex64) If added in v0.2.0

func (o *Complex64) If(f func(c complex64))

If calls the function f with the value if the value is present

func (*Complex64) OrElse

func (o *Complex64) OrElse(c complex64) complex64

OrElse returns the complex64 value or a default value if the value is not present

func (*Complex64) Present

func (o *Complex64) Present() bool

Present returns whether or not the value is present

func (*Complex64) Set

func (o *Complex64) Set(c complex64)

Set sets the complex64 value

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is an optional error

func EmptyError

func EmptyError() Error

EmptyError returns an empty optional.Error

func OfError

func OfError(e error) Error

OfError creates a optional.Error from a error

func (*Error) Get

func (o *Error) Get() error

Get returns the error value

func (*Error) If added in v0.2.0

func (o *Error) If(f func(e error))

If calls the function f with the value if the value is present

func (*Error) OrElse

func (o *Error) OrElse(e error) error

OrElse returns the error value or a default value if the value is not present

func (*Error) Present

func (o *Error) Present() bool

Present returns whether or not the value is present

func (*Error) Set

func (o *Error) Set(e error)

Set sets the error value

type Float32

type Float32 struct {
	// contains filtered or unexported fields
}

Float32 is an optional float32

func EmptyFloat32

func EmptyFloat32() Float32

EmptyFloat32 returns an empty optional.Float32

func OfFloat32

func OfFloat32(f float32) Float32

OfFloat32 creates a optional.Float32 from a float32

func (*Float32) Get

func (o *Float32) Get() float32

Get returns the float32 value

func (*Float32) If added in v0.2.0

func (o *Float32) If(f func(f float32))

If calls the function f with the value if the value is present

func (*Float32) OrElse

func (o *Float32) OrElse(f float32) float32

OrElse returns the float32 value or a default value if the value is not present

func (*Float32) Present

func (o *Float32) Present() bool

Present returns whether or not the value is present

func (*Float32) Set

func (o *Float32) Set(f float32)

Set sets the float32 value

type Float64

type Float64 struct {
	// contains filtered or unexported fields
}

Float64 is an optional float64

func EmptyFloat64

func EmptyFloat64() Float64

EmptyFloat64 returns an empty optional.Float64

func OfFloat64

func OfFloat64(f float64) Float64

OfFloat64 creates a optional.Float64 from a float64

func (*Float64) Get

func (o *Float64) Get() float64

Get returns the float64 value

func (*Float64) If added in v0.2.0

func (o *Float64) If(f func(f float64))

If calls the function f with the value if the value is present

func (*Float64) OrElse

func (o *Float64) OrElse(f float64) float64

OrElse returns the float64 value or a default value if the value is not present

func (*Float64) Present

func (o *Float64) Present() bool

Present returns whether or not the value is present

func (*Float64) Set

func (o *Float64) Set(f float64)

Set sets the float64 value

type Int

type Int struct {
	// contains filtered or unexported fields
}

Int is an optional int

func EmptyInt

func EmptyInt() Int

EmptyInt returns an empty optional.Int

func OfInt

func OfInt(i int) Int

OfInt creates a optional.Int from a int

func (*Int) Get

func (o *Int) Get() int

Get returns the int value

func (*Int) If added in v0.2.0

func (o *Int) If(f func(i int))

If calls the function f with the value if the value is present

func (*Int) OrElse

func (o *Int) OrElse(i int) int

OrElse returns the int value or a default value if the value is not present

func (*Int) Present

func (o *Int) Present() bool

Present returns whether or not the value is present

func (*Int) Set

func (o *Int) Set(i int)

Set sets the int value

type Int16

type Int16 struct {
	// contains filtered or unexported fields
}

Int16 is an optional int16

func EmptyInt16

func EmptyInt16() Int16

EmptyInt16 returns an empty optional.Int16

func OfInt16

func OfInt16(i int16) Int16

OfInt16 creates a optional.Int16 from a int16

func (*Int16) Get

func (o *Int16) Get() int16

Get returns the int16 value

func (*Int16) If added in v0.2.0

func (o *Int16) If(f func(i int16))

If calls the function f with the value if the value is present

func (*Int16) OrElse

func (o *Int16) OrElse(i int16) int16

OrElse returns the int16 value or a default value if the value is not present

func (*Int16) Present

func (o *Int16) Present() bool

Present returns whether or not the value is present

func (*Int16) Set

func (o *Int16) Set(i int16)

Set sets the int16 value

type Int32

type Int32 struct {
	// contains filtered or unexported fields
}

Int32 is an optional int32

func EmptyInt32

func EmptyInt32() Int32

EmptyInt32 returns an empty optional.Int32

func OfInt32

func OfInt32(i int32) Int32

OfInt32 creates a optional.Int32 from a int32

func (*Int32) Get

func (o *Int32) Get() int32

Get returns the int32 value

func (*Int32) If added in v0.2.0

func (o *Int32) If(f func(i int32))

If calls the function f with the value if the value is present

func (*Int32) OrElse

func (o *Int32) OrElse(i int32) int32

OrElse returns the int32 value or a default value if the value is not present

func (*Int32) Present

func (o *Int32) Present() bool

Present returns whether or not the value is present

func (*Int32) Set

func (o *Int32) Set(i int32)

Set sets the int32 value

type Int64

type Int64 struct {
	// contains filtered or unexported fields
}

Int64 is an optional int64

func EmptyInt64

func EmptyInt64() Int64

EmptyInt64 returns an empty optional.Int64

func OfInt64

func OfInt64(i int64) Int64

OfInt64 creates a optional.Int64 from a int64

func (*Int64) Get

func (o *Int64) Get() int64

Get returns the int64 value

func (*Int64) If added in v0.2.0

func (o *Int64) If(f func(i int64))

If calls the function f with the value if the value is present

func (*Int64) OrElse

func (o *Int64) OrElse(i int64) int64

OrElse returns the int64 value or a default value if the value is not present

func (*Int64) Present

func (o *Int64) Present() bool

Present returns whether or not the value is present

func (*Int64) Set

func (o *Int64) Set(i int64)

Set sets the int64 value

type Int8

type Int8 struct {
	// contains filtered or unexported fields
}

Int8 is an optional int8

func EmptyInt8

func EmptyInt8() Int8

EmptyInt8 returns an empty optional.Int8

func OfInt8

func OfInt8(i int8) Int8

OfInt8 creates a optional.Int8 from a int8

func (*Int8) Get

func (o *Int8) Get() int8

Get returns the int8 value

func (*Int8) If added in v0.2.0

func (o *Int8) If(f func(i int8))

If calls the function f with the value if the value is present

func (*Int8) OrElse

func (o *Int8) OrElse(i int8) int8

OrElse returns the int8 value or a default value if the value is not present

func (*Int8) Present

func (o *Int8) Present() bool

Present returns whether or not the value is present

func (*Int8) Set

func (o *Int8) Set(i int8)

Set sets the int8 value

type Rune

type Rune struct {
	// contains filtered or unexported fields
}

Rune is an optional rune

func EmptyRune

func EmptyRune() Rune

EmptyRune returns an empty optional.Rune

func OfRune

func OfRune(r rune) Rune

OfRune creates a optional.Rune from a rune

func (*Rune) Get

func (o *Rune) Get() rune

Get returns the rune value

func (*Rune) If added in v0.2.0

func (o *Rune) If(f func(r rune))

If calls the function f with the value if the value is present

func (*Rune) OrElse

func (o *Rune) OrElse(r rune) rune

OrElse returns the rune value or a default value if the value is not present

func (*Rune) Present

func (o *Rune) Present() bool

Present returns whether or not the value is present

func (*Rune) Set

func (o *Rune) Set(r rune)

Set sets the rune value

type String

type String struct {
	// contains filtered or unexported fields
}

String is an optional string

func EmptyString

func EmptyString() String

EmptyString returns an empty optional.String

func OfString

func OfString(s string) String

OfString creates a optional.String from a string

func (*String) Get

func (o *String) Get() string

Get returns the string value

func (*String) If added in v0.2.0

func (o *String) If(f func(s string))

If calls the function f with the value if the value is present

func (*String) OrElse

func (o *String) OrElse(s string) string

OrElse returns the string value or a default value if the value is not present

func (*String) Present

func (o *String) Present() bool

Present returns whether or not the value is present

func (*String) Set

func (o *String) Set(s string)

Set sets the string value

type Uint

type Uint struct {
	// contains filtered or unexported fields
}

Uint is an optional uint

func EmptyUint

func EmptyUint() Uint

EmptyUint returns an empty optional.Uint

func OfUint

func OfUint(u uint) Uint

OfUint creates a optional.Uint from a uint

func (*Uint) Get

func (o *Uint) Get() uint

Get returns the uint value

func (*Uint) If added in v0.2.0

func (o *Uint) If(f func(u uint))

If calls the function f with the value if the value is present

func (*Uint) OrElse

func (o *Uint) OrElse(u uint) uint

OrElse returns the uint value or a default value if the value is not present

func (*Uint) Present

func (o *Uint) Present() bool

Present returns whether or not the value is present

func (*Uint) Set

func (o *Uint) Set(u uint)

Set sets the uint value

type Uint16

type Uint16 struct {
	// contains filtered or unexported fields
}

Uint16 is an optional uint16

func EmptyUint16

func EmptyUint16() Uint16

EmptyUint16 returns an empty optional.Uint16

func OfUint16

func OfUint16(u uint16) Uint16

OfUint16 creates a optional.Uint16 from a uint16

func (*Uint16) Get

func (o *Uint16) Get() uint16

Get returns the uint16 value

func (*Uint16) If added in v0.2.0

func (o *Uint16) If(f func(u uint16))

If calls the function f with the value if the value is present

func (*Uint16) OrElse

func (o *Uint16) OrElse(u uint16) uint16

OrElse returns the uint16 value or a default value if the value is not present

func (*Uint16) Present

func (o *Uint16) Present() bool

Present returns whether or not the value is present

func (*Uint16) Set

func (o *Uint16) Set(u uint16)

Set sets the uint16 value

type Uint32

type Uint32 struct {
	// contains filtered or unexported fields
}

Uint32 is an optional uint32

func EmptyUint32

func EmptyUint32() Uint32

EmptyUint32 returns an empty optional.Uint32

func OfUint32

func OfUint32(u uint32) Uint32

OfUint32 creates a optional.Uint32 from a uint32

func (*Uint32) Get

func (o *Uint32) Get() uint32

Get returns the uint32 value

func (*Uint32) If added in v0.2.0

func (o *Uint32) If(f func(u uint32))

If calls the function f with the value if the value is present

func (*Uint32) OrElse

func (o *Uint32) OrElse(u uint32) uint32

OrElse returns the uint32 value or a default value if the value is not present

func (*Uint32) Present

func (o *Uint32) Present() bool

Present returns whether or not the value is present

func (*Uint32) Set

func (o *Uint32) Set(u uint32)

Set sets the uint32 value

type Uint64

type Uint64 struct {
	// contains filtered or unexported fields
}

Uint64 is an optional uint64

func EmptyUint64

func EmptyUint64() Uint64

EmptyUint64 returns an empty optional.Uint64

func OfUint64

func OfUint64(u uint64) Uint64

OfUint64 creates a optional.Uint64 from a uint64

func (*Uint64) Get

func (o *Uint64) Get() uint64

Get returns the uint64 value

func (*Uint64) If added in v0.2.0

func (o *Uint64) If(f func(u uint64))

If calls the function f with the value if the value is present

func (*Uint64) OrElse

func (o *Uint64) OrElse(u uint64) uint64

OrElse returns the uint64 value or a default value if the value is not present

func (*Uint64) Present

func (o *Uint64) Present() bool

Present returns whether or not the value is present

func (*Uint64) Set

func (o *Uint64) Set(u uint64)

Set sets the uint64 value

type Uint8

type Uint8 struct {
	// contains filtered or unexported fields
}

Uint8 is an optional uint8

func EmptyUint8

func EmptyUint8() Uint8

EmptyUint8 returns an empty optional.Uint8

func OfUint8

func OfUint8(u uint8) Uint8

OfUint8 creates a optional.Uint8 from a uint8

func (*Uint8) Get

func (o *Uint8) Get() uint8

Get returns the uint8 value

func (*Uint8) If added in v0.2.0

func (o *Uint8) If(f func(u uint8))

If calls the function f with the value if the value is present

func (*Uint8) OrElse

func (o *Uint8) OrElse(u uint8) uint8

OrElse returns the uint8 value or a default value if the value is not present

func (*Uint8) Present

func (o *Uint8) Present() bool

Present returns whether or not the value is present

func (*Uint8) Set

func (o *Uint8) Set(u uint8)

Set sets the uint8 value

type Uintptr

type Uintptr struct {
	// contains filtered or unexported fields
}

Uintptr is an optional uintptr

func EmptyUintptr

func EmptyUintptr() Uintptr

EmptyUintptr returns an empty optional.Uintptr

func OfUintptr

func OfUintptr(u uintptr) Uintptr

OfUintptr creates a optional.Uintptr from a uintptr

func (*Uintptr) Get

func (o *Uintptr) Get() uintptr

Get returns the uintptr value

func (*Uintptr) If added in v0.2.0

func (o *Uintptr) If(f func(u uintptr))

If calls the function f with the value if the value is present

func (*Uintptr) OrElse

func (o *Uintptr) OrElse(u uintptr) uintptr

OrElse returns the uintptr value or a default value if the value is not present

func (*Uintptr) Present

func (o *Uintptr) Present() bool

Present returns whether or not the value is present

func (*Uintptr) Set

func (o *Uintptr) Set(u uintptr)

Set sets the uintptr value

Directories

Path Synopsis
cmd
optional
Optional is a tool that generates 'optional' type wrappers around a given type T. Typically this process would be run using go generate, like this: //go:generate optional -type=Foo running this command optional -type=Foo in the same directory will create the file optional_foo.go containing a definition of type OptionalFoo struct { ...
Optional is a tool that generates 'optional' type wrappers around a given type T. Typically this process would be run using go generate, like this: //go:generate optional -type=Foo running this command optional -type=Foo in the same directory will create the file optional_foo.go containing a definition of type OptionalFoo struct { ...

Jump to

Keyboard shortcuts

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