optional

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 2 Imported by: 28

README

Optional

Build Status Release Software License Go Doc Go Report Card SayThanks.io

Optional is a library that provides option types for the primitive Go types.

It can also be used as a tool to generate option type wrappers around your own types.

Motivation

In Go, variables declared without an explicit initial value are given their zero value. Most of the time this is what you want, but sometimes you want to be able to tell if a variable was set or if it's just a zero value. That's where option types come in handy.

Inspiration

Tool

Install

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

Usage

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

import _ "github.com/markphelps/optional

//go:generate optional -type=Foo
type Foo struct {
  ...
}

🗒️ Note: the blank import is necessary to prevent go mod tidy from removing this library as a dependency if you are only using it to generate your own types.

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
package main

import (
    "fmt"

    "github.com/markphelps/optional"
)

func main() {
    s := optional.NewString("foo")

    value, err := s.Get()
    if err != nil {
        // handle error!
    } else {
        fmt.Println(value)
    }

    t := optional.String{}
    fmt.Println(t.OrElse("bar"))
}

See example_test.go and the documentation for more usage.

Marshalling/Unmarshalling JSON

Note: v0.6.0 introduces a potential breaking change to anyone depending on marshalling non-present values to their zero values instead of null. See: #9 for more context.

Note: v0.8.0 removes JSON support from complex64 and complex128 types per #13

Option types marshal to/from JSON as you would expect:

Marshalling
package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    var value = struct {
        Field optional.String `json:"field,omitempty"`
    }{
        Field: optional.NewString("bar"),
    }

    out, _ := json.Marshal(value)

    fmt.Println(string(out))
    // outputs: {"field":"bar"}
}
Unmarshalling
package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    var value = &struct {
        Field optional.String `json:"field,omitempty"`
    }{}

    _ = json.Unmarshal([]byte(`{"field":"bar"}`), value)

    value.Field.If(func(s string) {
        fmt.Println(s)
    })
    // outputs: bar
}

See example_test.go for more examples.

Test Coverage

As you can see test coverage is a bit lacking for the library. This is simply because testing generated code is not super easy. I'm currently working on improving test coverage for the generated types, but in the meantime checkout string_test.go and int_test.go for examples.

Also checkout:

Golden Files

If changing the API you may need to update the golden files for your tests to pass by running:

go test ./cmd/optional/... -update.

Contributing

  1. Fork it
  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.NewString("foo"),
		optional.NewString(""),
		optional.NewString("bar"),
		{},
	}

	for _, v := range values {
		value, err := v.Get()
		if err != nil {
			fmt.Println(err.Error())
		} else {
			fmt.Println(value)
		}
	}

}
Output:

foo

bar
value not present
Example (If)
package main

import (
	"fmt"

	"github.com/markphelps/optional"
)

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

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

}
Output:

present
present
present
Example (MarshalJSON)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/markphelps/optional"
)

func main() {
	type example struct {
		Field    *optional.String `json:"field,omitempty"`
		FieldTwo *optional.String `json:"field_two"`
	}

	var values = []optional.String{
		optional.NewString("foo"),
		optional.NewString(""),
		optional.NewString("bar"),
	}

	for _, v := range values {
		out, _ := json.Marshal(&example{
			Field:    &v,
			FieldTwo: &v,
		})
		fmt.Println(string(out))
	}

	out, _ := json.Marshal(&example{})
	fmt.Println(string(out))

}
Output:

{"field":"foo","field_two":"foo"}
{"field":"","field_two":""}
{"field":"bar","field_two":"bar"}
{"field_two":null}
Example (MustGet)
package main

import (
	"fmt"

	"github.com/markphelps/optional"
)

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

	for _, v := range values {
		if v.Present() {
			value := v.MustGet()
			fmt.Println(value)
		} else {
			fmt.Println("not present")
		}
	}

}
Output:

foo

bar
not present
Example (OrElse)
package main

import (
	"fmt"

	"github.com/markphelps/optional"
)

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

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

}
Output:

foo

bar
not present
Example (Set)
package main

import (
	"fmt"

	"github.com/markphelps/optional"
)

func main() {
	var (
		values = []string{
			"foo",
			"",
			"bar",
		}

		s = optional.NewString("baz")
	)

	for _, v := range values {
		s.Set(v)
		value, err := s.Get()
		if err != nil {
			fmt.Println(err.Error())
		} else {
			fmt.Println(value)
		}
	}

}
Output:

foo

bar
Example (UnmarshalJSON)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/markphelps/optional"
)

func main() {
	var values = []string{
		`{"field":"foo","field_two":"foo"}`,
		`{"field":"","field_two":""}`,
		`{"field":"null","field_two":"null"}`,
		`{"field":"bar","field_two":"bar"}`,
		"{}",
	}

	for _, v := range values {
		var o = &struct {
			Field    optional.String `json:"field,omitempty"`
			FieldTwo optional.String `json:"field_two"`
		}{}

		if err := json.Unmarshal([]byte(v), o); err != nil {
			fmt.Println(err)
		}

		o.Field.If(func(s string) {
			fmt.Println(s)
		})

		o.FieldTwo.If(func(s string) {
			fmt.Println(s)
		})
	}

	
Output:

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 NewBool added in v0.3.0

func NewBool(v bool) Bool

NewBool creates an optional.Bool from a bool.

func NewBoolFromPtr added in v0.11.0

func NewBoolFromPtr(v *bool) Bool

NewBoolFromPtr creates an optional.Bool from a bool pointer.

func (Bool) Get

func (b Bool) Get() (bool, error)

Get returns the bool value or an error if not present.

func (Bool) If added in v0.2.0

func (b Bool) If(fn func(bool))

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

func (Bool) MarshalJSON added in v0.4.0

func (b Bool) MarshalJSON() ([]byte, error)

func (Bool) MustGet added in v0.10.0

func (b Bool) MustGet() bool

MustGet returns the bool value or panics if not present.

func (Bool) OrElse

func (b Bool) OrElse(v bool) bool

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

func (Bool) Present

func (b Bool) Present() bool

Present returns whether or not the value is present.

func (*Bool) Set

func (b *Bool) Set(v bool)

Set sets the bool value.

func (Bool) ToPtr added in v0.11.0

func (b Bool) ToPtr() *bool

ToPtr returns a *bool of the value or nil if not present.

func (*Bool) UnmarshalJSON added in v0.4.0

func (b *Bool) UnmarshalJSON(data []byte) error

type Byte

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

Byte is an optional byte.

func NewByte added in v0.3.0

func NewByte(v byte) Byte

NewByte creates an optional.Byte from a byte.

func NewByteFromPtr added in v0.11.0

func NewByteFromPtr(v *byte) Byte

NewByteFromPtr creates an optional.Byte from a byte pointer.

func (Byte) Get

func (b Byte) Get() (byte, error)

Get returns the byte value or an error if not present.

func (Byte) If added in v0.2.0

func (b Byte) If(fn func(byte))

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

func (Byte) MarshalJSON added in v0.4.0

func (b Byte) MarshalJSON() ([]byte, error)

func (Byte) MustGet added in v0.10.0

func (b Byte) MustGet() byte

MustGet returns the byte value or panics if not present.

func (Byte) OrElse

func (b Byte) OrElse(v byte) byte

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

func (Byte) Present

func (b Byte) Present() bool

Present returns whether or not the value is present.

func (*Byte) Set

func (b *Byte) Set(v byte)

Set sets the byte value.

func (Byte) ToPtr added in v0.11.0

func (b Byte) ToPtr() *byte

ToPtr returns a *byte of the value or nil if not present.

func (*Byte) UnmarshalJSON added in v0.4.0

func (b *Byte) UnmarshalJSON(data []byte) error

type Complex128

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

Complex128 is an optional complex128.

func NewComplex128 added in v0.3.0

func NewComplex128(v complex128) Complex128

NewComplex128 creates an optional.Complex128 from a complex128.

func NewComplex128FromPtr added in v0.11.0

func NewComplex128FromPtr(v *complex128) Complex128

NewComplex128FromPtr creates an optional.Complex128 from a complex128 pointer.

func (Complex128) Get

func (c Complex128) Get() (complex128, error)

Get returns the complex128 value or an error if not present.

func (Complex128) If added in v0.2.0

func (c Complex128) If(fn func(complex128))

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

func (Complex128) MustGet added in v0.10.0

func (c Complex128) MustGet() complex128

MustGet returns the complex128 value or panics if not present.

func (Complex128) OrElse

func (c Complex128) OrElse(v complex128) complex128

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

func (Complex128) Present

func (c Complex128) Present() bool

Present returns whether or not the value is present.

func (*Complex128) Set

func (c *Complex128) Set(v complex128)

Set sets the complex128 value.

func (Complex128) ToPtr added in v0.11.0

func (c Complex128) ToPtr() *complex128

ToPtr returns a *complex128 of the value or nil if not present.

type Complex64

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

Complex64 is an optional complex64.

func NewComplex64 added in v0.3.0

func NewComplex64(v complex64) Complex64

NewComplex64 creates an optional.Complex64 from a complex64.

func NewComplex64FromPtr added in v0.11.0

func NewComplex64FromPtr(v *complex64) Complex64

NewComplex64FromPtr creates an optional.Complex64 from a complex64 pointer.

func (Complex64) Get

func (c Complex64) Get() (complex64, error)

Get returns the complex64 value or an error if not present.

func (Complex64) If added in v0.2.0

func (c Complex64) If(fn func(complex64))

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

func (Complex64) MustGet added in v0.10.0

func (c Complex64) MustGet() complex64

MustGet returns the complex64 value or panics if not present.

func (Complex64) OrElse

func (c Complex64) OrElse(v complex64) complex64

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

func (Complex64) Present

func (c Complex64) Present() bool

Present returns whether or not the value is present.

func (*Complex64) Set

func (c *Complex64) Set(v complex64)

Set sets the complex64 value.

func (Complex64) ToPtr added in v0.11.0

func (c Complex64) ToPtr() *complex64

ToPtr returns a *complex64 of the value or nil if not present.

type Error

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

Error is an optional error.

func NewError added in v0.3.0

func NewError(v error) Error

NewError creates an optional.Error from a error.

func NewErrorFromPtr added in v0.11.0

func NewErrorFromPtr(v *error) Error

NewErrorFromPtr creates an optional.Error from a error pointer.

func (Error) Get

func (e Error) Get() (error, error)

Get returns the error value or an error if not present.

func (Error) If added in v0.2.0

func (e Error) If(fn func(error))

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

func (Error) MarshalJSON added in v0.4.0

func (e Error) MarshalJSON() ([]byte, error)

func (Error) MustGet added in v0.10.0

func (e Error) MustGet() error

MustGet returns the error value or panics if not present.

func (Error) OrElse

func (e Error) OrElse(v error) error

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

func (Error) Present

func (e Error) Present() bool

Present returns whether or not the value is present.

func (*Error) Set

func (e *Error) Set(v error)

Set sets the error value.

func (Error) ToPtr added in v0.11.0

func (e Error) ToPtr() *error

ToPtr returns a *error of the value or nil if not present.

func (*Error) UnmarshalJSON added in v0.4.0

func (e *Error) UnmarshalJSON(data []byte) error

type Float32

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

Float32 is an optional float32.

func NewFloat32 added in v0.3.0

func NewFloat32(v float32) Float32

NewFloat32 creates an optional.Float32 from a float32.

func NewFloat32FromPtr added in v0.11.0

func NewFloat32FromPtr(v *float32) Float32

NewFloat32FromPtr creates an optional.Float32 from a float32 pointer.

func (Float32) Get

func (f Float32) Get() (float32, error)

Get returns the float32 value or an error if not present.

func (Float32) If added in v0.2.0

func (f Float32) If(fn func(float32))

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

func (Float32) MarshalJSON added in v0.4.0

func (f Float32) MarshalJSON() ([]byte, error)

func (Float32) MustGet added in v0.10.0

func (f Float32) MustGet() float32

MustGet returns the float32 value or panics if not present.

func (Float32) OrElse

func (f Float32) OrElse(v float32) float32

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

func (Float32) Present

func (f Float32) Present() bool

Present returns whether or not the value is present.

func (*Float32) Set

func (f *Float32) Set(v float32)

Set sets the float32 value.

func (Float32) ToPtr added in v0.11.0

func (f Float32) ToPtr() *float32

ToPtr returns a *float32 of the value or nil if not present.

func (*Float32) UnmarshalJSON added in v0.4.0

func (f *Float32) UnmarshalJSON(data []byte) error

type Float64

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

Float64 is an optional float64.

func NewFloat64 added in v0.3.0

func NewFloat64(v float64) Float64

NewFloat64 creates an optional.Float64 from a float64.

func NewFloat64FromPtr added in v0.11.0

func NewFloat64FromPtr(v *float64) Float64

NewFloat64FromPtr creates an optional.Float64 from a float64 pointer.

func (Float64) Get

func (f Float64) Get() (float64, error)

Get returns the float64 value or an error if not present.

func (Float64) If added in v0.2.0

func (f Float64) If(fn func(float64))

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

func (Float64) MarshalJSON added in v0.4.0

func (f Float64) MarshalJSON() ([]byte, error)

func (Float64) MustGet added in v0.10.0

func (f Float64) MustGet() float64

MustGet returns the float64 value or panics if not present.

func (Float64) OrElse

func (f Float64) OrElse(v float64) float64

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

func (Float64) Present

func (f Float64) Present() bool

Present returns whether or not the value is present.

func (*Float64) Set

func (f *Float64) Set(v float64)

Set sets the float64 value.

func (Float64) ToPtr added in v0.11.0

func (f Float64) ToPtr() *float64

ToPtr returns a *float64 of the value or nil if not present.

func (*Float64) UnmarshalJSON added in v0.4.0

func (f *Float64) UnmarshalJSON(data []byte) error

type Int

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

Int is an optional int.

func NewInt added in v0.3.0

func NewInt(v int) Int

NewInt creates an optional.Int from a int.

func NewIntFromPtr added in v0.11.0

func NewIntFromPtr(v *int) Int

NewIntFromPtr creates an optional.Int from a int pointer.

func (Int) Get

func (i Int) Get() (int, error)

Get returns the int value or an error if not present.

func (Int) If added in v0.2.0

func (i Int) If(fn func(int))

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

func (Int) MarshalJSON added in v0.4.0

func (i Int) MarshalJSON() ([]byte, error)

func (Int) MustGet added in v0.10.0

func (i Int) MustGet() int

MustGet returns the int value or panics if not present.

func (Int) OrElse

func (i Int) OrElse(v int) int

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

func (Int) Present

func (i Int) Present() bool

Present returns whether or not the value is present.

func (*Int) Set

func (i *Int) Set(v int)

Set sets the int value.

func (Int) ToPtr added in v0.11.0

func (i Int) ToPtr() *int

ToPtr returns a *int of the value or nil if not present.

func (*Int) UnmarshalJSON added in v0.4.0

func (i *Int) UnmarshalJSON(data []byte) error

type Int16

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

Int16 is an optional int16.

func NewInt16 added in v0.3.0

func NewInt16(v int16) Int16

NewInt16 creates an optional.Int16 from a int16.

func NewInt16FromPtr added in v0.11.0

func NewInt16FromPtr(v *int16) Int16

NewInt16FromPtr creates an optional.Int16 from a int16 pointer.

func (Int16) Get

func (i Int16) Get() (int16, error)

Get returns the int16 value or an error if not present.

func (Int16) If added in v0.2.0

func (i Int16) If(fn func(int16))

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

func (Int16) MarshalJSON added in v0.4.0

func (i Int16) MarshalJSON() ([]byte, error)

func (Int16) MustGet added in v0.10.0

func (i Int16) MustGet() int16

MustGet returns the int16 value or panics if not present.

func (Int16) OrElse

func (i Int16) OrElse(v int16) int16

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

func (Int16) Present

func (i Int16) Present() bool

Present returns whether or not the value is present.

func (*Int16) Set

func (i *Int16) Set(v int16)

Set sets the int16 value.

func (Int16) ToPtr added in v0.11.0

func (i Int16) ToPtr() *int16

ToPtr returns a *int16 of the value or nil if not present.

func (*Int16) UnmarshalJSON added in v0.4.0

func (i *Int16) UnmarshalJSON(data []byte) error

type Int32

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

Int32 is an optional int32.

func NewInt32 added in v0.3.0

func NewInt32(v int32) Int32

NewInt32 creates an optional.Int32 from a int32.

func NewInt32FromPtr added in v0.11.0

func NewInt32FromPtr(v *int32) Int32

NewInt32FromPtr creates an optional.Int32 from a int32 pointer.

func (Int32) Get

func (i Int32) Get() (int32, error)

Get returns the int32 value or an error if not present.

func (Int32) If added in v0.2.0

func (i Int32) If(fn func(int32))

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

func (Int32) MarshalJSON added in v0.4.0

func (i Int32) MarshalJSON() ([]byte, error)

func (Int32) MustGet added in v0.10.0

func (i Int32) MustGet() int32

MustGet returns the int32 value or panics if not present.

func (Int32) OrElse

func (i Int32) OrElse(v int32) int32

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

func (Int32) Present

func (i Int32) Present() bool

Present returns whether or not the value is present.

func (*Int32) Set

func (i *Int32) Set(v int32)

Set sets the int32 value.

func (Int32) ToPtr added in v0.11.0

func (i Int32) ToPtr() *int32

ToPtr returns a *int32 of the value or nil if not present.

func (*Int32) UnmarshalJSON added in v0.4.0

func (i *Int32) UnmarshalJSON(data []byte) error

type Int64

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

Int64 is an optional int64.

func NewInt64 added in v0.3.0

func NewInt64(v int64) Int64

NewInt64 creates an optional.Int64 from a int64.

func NewInt64FromPtr added in v0.11.0

func NewInt64FromPtr(v *int64) Int64

NewInt64FromPtr creates an optional.Int64 from a int64 pointer.

func (Int64) Get

func (i Int64) Get() (int64, error)

Get returns the int64 value or an error if not present.

func (Int64) If added in v0.2.0

func (i Int64) If(fn func(int64))

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

func (Int64) MarshalJSON added in v0.4.0

func (i Int64) MarshalJSON() ([]byte, error)

func (Int64) MustGet added in v0.10.0

func (i Int64) MustGet() int64

MustGet returns the int64 value or panics if not present.

func (Int64) OrElse

func (i Int64) OrElse(v int64) int64

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

func (Int64) Present

func (i Int64) Present() bool

Present returns whether or not the value is present.

func (*Int64) Set

func (i *Int64) Set(v int64)

Set sets the int64 value.

func (Int64) ToPtr added in v0.11.0

func (i Int64) ToPtr() *int64

ToPtr returns a *int64 of the value or nil if not present.

func (*Int64) UnmarshalJSON added in v0.4.0

func (i *Int64) UnmarshalJSON(data []byte) error

type Int8

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

Int8 is an optional int8.

func NewInt8 added in v0.3.0

func NewInt8(v int8) Int8

NewInt8 creates an optional.Int8 from a int8.

func NewInt8FromPtr added in v0.11.0

func NewInt8FromPtr(v *int8) Int8

NewInt8FromPtr creates an optional.Int8 from a int8 pointer.

func (Int8) Get

func (i Int8) Get() (int8, error)

Get returns the int8 value or an error if not present.

func (Int8) If added in v0.2.0

func (i Int8) If(fn func(int8))

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

func (Int8) MarshalJSON added in v0.4.0

func (i Int8) MarshalJSON() ([]byte, error)

func (Int8) MustGet added in v0.10.0

func (i Int8) MustGet() int8

MustGet returns the int8 value or panics if not present.

func (Int8) OrElse

func (i Int8) OrElse(v int8) int8

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

func (Int8) Present

func (i Int8) Present() bool

Present returns whether or not the value is present.

func (*Int8) Set

func (i *Int8) Set(v int8)

Set sets the int8 value.

func (Int8) ToPtr added in v0.11.0

func (i Int8) ToPtr() *int8

ToPtr returns a *int8 of the value or nil if not present.

func (*Int8) UnmarshalJSON added in v0.4.0

func (i *Int8) UnmarshalJSON(data []byte) error

type Rune

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

Rune is an optional rune.

func NewRune added in v0.3.0

func NewRune(v rune) Rune

NewRune creates an optional.Rune from a rune.

func NewRuneFromPtr added in v0.11.0

func NewRuneFromPtr(v *rune) Rune

NewRuneFromPtr creates an optional.Rune from a rune pointer.

func (Rune) Get

func (r Rune) Get() (rune, error)

Get returns the rune value or an error if not present.

func (Rune) If added in v0.2.0

func (r Rune) If(fn func(rune))

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

func (Rune) MarshalJSON added in v0.4.0

func (r Rune) MarshalJSON() ([]byte, error)

func (Rune) MustGet added in v0.10.0

func (r Rune) MustGet() rune

MustGet returns the rune value or panics if not present.

func (Rune) OrElse

func (r Rune) OrElse(v rune) rune

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

func (Rune) Present

func (r Rune) Present() bool

Present returns whether or not the value is present.

func (*Rune) Set

func (r *Rune) Set(v rune)

Set sets the rune value.

func (Rune) ToPtr added in v0.11.0

func (r Rune) ToPtr() *rune

ToPtr returns a *rune of the value or nil if not present.

func (*Rune) UnmarshalJSON added in v0.4.0

func (r *Rune) UnmarshalJSON(data []byte) error

type String

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

String is an optional string.

func NewString added in v0.3.0

func NewString(v string) String

NewString creates an optional.String from a string.

func NewStringFromPtr added in v0.11.0

func NewStringFromPtr(v *string) String

NewStringFromPtr creates an optional.String from a string pointer.

func (String) Get

func (s String) Get() (string, error)

Get returns the string value or an error if not present.

func (String) If added in v0.2.0

func (s String) If(fn func(string))

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

func (String) MarshalJSON added in v0.4.0

func (s String) MarshalJSON() ([]byte, error)

func (String) MustGet added in v0.10.0

func (s String) MustGet() string

MustGet returns the string value or panics if not present.

func (String) OrElse

func (s String) OrElse(v string) string

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

func (String) Present

func (s String) Present() bool

Present returns whether or not the value is present.

func (*String) Set

func (s *String) Set(v string)

Set sets the string value.

func (String) ToPtr added in v0.11.0

func (s String) ToPtr() *string

ToPtr returns a *string of the value or nil if not present.

func (*String) UnmarshalJSON added in v0.4.0

func (s *String) UnmarshalJSON(data []byte) error

type Uint

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

Uint is an optional uint.

func NewUint added in v0.3.0

func NewUint(v uint) Uint

NewUint creates an optional.Uint from a uint.

func NewUintFromPtr added in v0.11.0

func NewUintFromPtr(v *uint) Uint

NewUintFromPtr creates an optional.Uint from a uint pointer.

func (Uint) Get

func (u Uint) Get() (uint, error)

Get returns the uint value or an error if not present.

func (Uint) If added in v0.2.0

func (u Uint) If(fn func(uint))

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

func (Uint) MarshalJSON added in v0.4.0

func (u Uint) MarshalJSON() ([]byte, error)

func (Uint) MustGet added in v0.10.0

func (u Uint) MustGet() uint

MustGet returns the uint value or panics if not present.

func (Uint) OrElse

func (u Uint) OrElse(v uint) uint

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

func (Uint) Present

func (u Uint) Present() bool

Present returns whether or not the value is present.

func (*Uint) Set

func (u *Uint) Set(v uint)

Set sets the uint value.

func (Uint) ToPtr added in v0.11.0

func (u Uint) ToPtr() *uint

ToPtr returns a *uint of the value or nil if not present.

func (*Uint) UnmarshalJSON added in v0.4.0

func (u *Uint) UnmarshalJSON(data []byte) error

type Uint16

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

Uint16 is an optional uint16.

func NewUint16 added in v0.3.0

func NewUint16(v uint16) Uint16

NewUint16 creates an optional.Uint16 from a uint16.

func NewUint16FromPtr added in v0.11.0

func NewUint16FromPtr(v *uint16) Uint16

NewUint16FromPtr creates an optional.Uint16 from a uint16 pointer.

func (Uint16) Get

func (u Uint16) Get() (uint16, error)

Get returns the uint16 value or an error if not present.

func (Uint16) If added in v0.2.0

func (u Uint16) If(fn func(uint16))

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

func (Uint16) MarshalJSON added in v0.4.0

func (u Uint16) MarshalJSON() ([]byte, error)

func (Uint16) MustGet added in v0.10.0

func (u Uint16) MustGet() uint16

MustGet returns the uint16 value or panics if not present.

func (Uint16) OrElse

func (u Uint16) OrElse(v uint16) uint16

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

func (Uint16) Present

func (u Uint16) Present() bool

Present returns whether or not the value is present.

func (*Uint16) Set

func (u *Uint16) Set(v uint16)

Set sets the uint16 value.

func (Uint16) ToPtr added in v0.11.0

func (u Uint16) ToPtr() *uint16

ToPtr returns a *uint16 of the value or nil if not present.

func (*Uint16) UnmarshalJSON added in v0.4.0

func (u *Uint16) UnmarshalJSON(data []byte) error

type Uint32

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

Uint32 is an optional uint32.

func NewUint32 added in v0.3.0

func NewUint32(v uint32) Uint32

NewUint32 creates an optional.Uint32 from a uint32.

func NewUint32FromPtr added in v0.11.0

func NewUint32FromPtr(v *uint32) Uint32

NewUint32FromPtr creates an optional.Uint32 from a uint32 pointer.

func (Uint32) Get

func (u Uint32) Get() (uint32, error)

Get returns the uint32 value or an error if not present.

func (Uint32) If added in v0.2.0

func (u Uint32) If(fn func(uint32))

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

func (Uint32) MarshalJSON added in v0.4.0

func (u Uint32) MarshalJSON() ([]byte, error)

func (Uint32) MustGet added in v0.10.0

func (u Uint32) MustGet() uint32

MustGet returns the uint32 value or panics if not present.

func (Uint32) OrElse

func (u Uint32) OrElse(v uint32) uint32

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

func (Uint32) Present

func (u Uint32) Present() bool

Present returns whether or not the value is present.

func (*Uint32) Set

func (u *Uint32) Set(v uint32)

Set sets the uint32 value.

func (Uint32) ToPtr added in v0.11.0

func (u Uint32) ToPtr() *uint32

ToPtr returns a *uint32 of the value or nil if not present.

func (*Uint32) UnmarshalJSON added in v0.4.0

func (u *Uint32) UnmarshalJSON(data []byte) error

type Uint64

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

Uint64 is an optional uint64.

func NewUint64 added in v0.3.0

func NewUint64(v uint64) Uint64

NewUint64 creates an optional.Uint64 from a uint64.

func NewUint64FromPtr added in v0.11.0

func NewUint64FromPtr(v *uint64) Uint64

NewUint64FromPtr creates an optional.Uint64 from a uint64 pointer.

func (Uint64) Get

func (u Uint64) Get() (uint64, error)

Get returns the uint64 value or an error if not present.

func (Uint64) If added in v0.2.0

func (u Uint64) If(fn func(uint64))

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

func (Uint64) MarshalJSON added in v0.4.0

func (u Uint64) MarshalJSON() ([]byte, error)

func (Uint64) MustGet added in v0.10.0

func (u Uint64) MustGet() uint64

MustGet returns the uint64 value or panics if not present.

func (Uint64) OrElse

func (u Uint64) OrElse(v uint64) uint64

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

func (Uint64) Present

func (u Uint64) Present() bool

Present returns whether or not the value is present.

func (*Uint64) Set

func (u *Uint64) Set(v uint64)

Set sets the uint64 value.

func (Uint64) ToPtr added in v0.11.0

func (u Uint64) ToPtr() *uint64

ToPtr returns a *uint64 of the value or nil if not present.

func (*Uint64) UnmarshalJSON added in v0.4.0

func (u *Uint64) UnmarshalJSON(data []byte) error

type Uint8

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

Uint8 is an optional uint8.

func NewUint8 added in v0.3.0

func NewUint8(v uint8) Uint8

NewUint8 creates an optional.Uint8 from a uint8.

func NewUint8FromPtr added in v0.11.0

func NewUint8FromPtr(v *uint8) Uint8

NewUint8FromPtr creates an optional.Uint8 from a uint8 pointer.

func (Uint8) Get

func (u Uint8) Get() (uint8, error)

Get returns the uint8 value or an error if not present.

func (Uint8) If added in v0.2.0

func (u Uint8) If(fn func(uint8))

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

func (Uint8) MarshalJSON added in v0.4.0

func (u Uint8) MarshalJSON() ([]byte, error)

func (Uint8) MustGet added in v0.10.0

func (u Uint8) MustGet() uint8

MustGet returns the uint8 value or panics if not present.

func (Uint8) OrElse

func (u Uint8) OrElse(v uint8) uint8

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

func (Uint8) Present

func (u Uint8) Present() bool

Present returns whether or not the value is present.

func (*Uint8) Set

func (u *Uint8) Set(v uint8)

Set sets the uint8 value.

func (Uint8) ToPtr added in v0.11.0

func (u Uint8) ToPtr() *uint8

ToPtr returns a *uint8 of the value or nil if not present.

func (*Uint8) UnmarshalJSON added in v0.4.0

func (u *Uint8) UnmarshalJSON(data []byte) error

type Uintptr

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

Uintptr is an optional uintptr.

func NewUintptr added in v0.3.0

func NewUintptr(v uintptr) Uintptr

NewUintptr creates an optional.Uintptr from a uintptr.

func NewUintptrFromPtr added in v0.11.0

func NewUintptrFromPtr(v *uintptr) Uintptr

NewUintptrFromPtr creates an optional.Uintptr from a uintptr pointer.

func (Uintptr) Get

func (u Uintptr) Get() (uintptr, error)

Get returns the uintptr value or an error if not present.

func (Uintptr) If added in v0.2.0

func (u Uintptr) If(fn func(uintptr))

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

func (Uintptr) MarshalJSON added in v0.4.0

func (u Uintptr) MarshalJSON() ([]byte, error)

func (Uintptr) MustGet added in v0.10.0

func (u Uintptr) MustGet() uintptr

MustGet returns the uintptr value or panics if not present.

func (Uintptr) OrElse

func (u Uintptr) OrElse(v uintptr) uintptr

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

func (Uintptr) Present

func (u Uintptr) Present() bool

Present returns whether or not the value is present.

func (*Uintptr) Set

func (u *Uintptr) Set(v uintptr)

Set sets the uintptr value.

func (Uintptr) ToPtr added in v0.11.0

func (u Uintptr) ToPtr() *uintptr

ToPtr returns a *uintptr of the value or nil if not present.

func (*Uintptr) UnmarshalJSON added in v0.4.0

func (u *Uintptr) UnmarshalJSON(data []byte) error

Directories

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

Jump to

Keyboard shortcuts

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