optional

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package optional implements an Optional[T] type for JSON use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

type Optional[T any] struct {
	Value   T
	Null    bool
	Present bool
}

Optional is an optional value. When unmarshaling a struct, if the value is not present, o.Present and o.Null are false, and o.Value is the zero value for T.

Note that Go doesn't let us control marshaling as much as unmarshaling, so the reverse is not possible. Optional is marshaled as null if o.Null is true or o.Present is false.

func NewWithValue

func NewWithValue[T any](v T) Optional[T]

NewWithValue returns an Optional[T] with the value set to v.

func Null

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

Null returns an Optional[T] set to null.

func (Optional[T]) MarshalJSON

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

MarshalJSON implements json.Marshaler

func (*Optional[T]) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler

Jump to

Keyboard shortcuts

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