opt

package
v1.78.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: BSD-3-Clause Imports: 5 Imported by: 27

Documentation

Overview

Package opt defines optional types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool string

Bool represents an optional boolean to be JSON-encoded. The string is either "true", "false", or the empty string to mean unset.

As a special case, the underlying string may also be the string "unset" as as a synonym for the empty string. This lets the explicit unset value be exchanged over an encoding/json "omitempty" field without it being dropped.

func NewBool added in v1.58.0

func NewBool(b bool) Bool

NewBool constructs a new Bool value equal to b. The returned Bool is set, unless Set("") or Clear() methods are called.

func (*Bool) Clear

func (b *Bool) Clear()

func (Bool) EqualBool added in v0.100.0

func (b Bool) EqualBool(v bool) bool

EqualBool reports whether b is equal to v. If b is empty or not a valid bool, it reports false.

func (Bool) Get

func (b Bool) Get() (v bool, ok bool)

func (Bool) MarshalJSON

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

func (*Bool) Scan added in v1.16.0

func (b *Bool) Scan(src any) error

Scan implements database/sql.Scanner.

func (*Bool) Set

func (b *Bool) Set(v bool)

func (*Bool) UnmarshalJSON

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

type BoolFlag added in v1.74.0

type BoolFlag struct {
	*Bool
}

BoolFlag is a wrapper for Bool that implements flag.Value.

func (*BoolFlag) Set added in v1.74.0

func (b *BoolFlag) Set(s string) error

Set the value of b, using any value supported by strconv.ParseBool.

func (*BoolFlag) String added in v1.74.0

func (b *BoolFlag) String() string

String returns "true" or "false" if the value is set, or an empty string otherwise.

type Value added in v1.70.0

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value is an optional value to be JSON-encoded. With encoding/json, a zero Value is marshaled as a JSON null. With github.com/go-json-experiment/json, a zero Value is omitted from the JSON object if the Go struct field specified with omitzero. The omitempty tag option should never be used with Value fields.

func ValueOf added in v1.70.0

func ValueOf[T any](v T) Value[T]

ValueOf returns an optional Value containing the specified value. It treats nil slices and maps as empty slices and maps.

func (*Value[T]) Clear added in v1.70.0

func (o *Value[T]) Clear()

Clear resets o to an empty state.

func (Value[T]) Equal added in v1.70.0

func (o Value[T]) Equal(v Value[T]) bool

Equal reports whether o is equal to v. Two optional values are equal if both are empty, or if both are set and the underlying values are equal. If the template type T implements an Equal(T) bool method, it will be used instead of the == operator for value comparison. If T is not comparable, it returns false.

func (Value[T]) Get added in v1.70.0

func (o Value[T]) Get() T

Get returns the value of o. If a value hasn't been set, a zero value of T will be returned.

func (Value[T]) GetOk added in v1.70.0

func (o Value[T]) GetOk() (v T, ok bool)

Get returns the value and a flag indicating whether the value is set.

func (Value[T]) GetOr added in v1.72.0

func (o Value[T]) GetOr(def T) T

GetOr returns the value of o or def if a value hasn't been set.

func (*Value[T]) IsSet added in v1.70.0

func (o *Value[T]) IsSet() bool

IsSet reports whether o has a value set.

func (Value[T]) MarshalJSON added in v1.70.0

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

MarshalJSON implements [json.Marshaler].

func (Value[T]) MarshalJSONV2 added in v1.70.0

func (o Value[T]) MarshalJSONV2(enc *jsontext.Encoder, opts jsonv2.Options) error

MarshalJSONV2 implements jsonv2.MarshalerV2.

func (*Value[T]) Set added in v1.70.0

func (o *Value[T]) Set(v T)

Set assigns the specified value to the optional value o.

func (Value[T]) String added in v1.70.0

func (o Value[T]) String() string

String implements fmt.Stringer.

func (*Value[T]) UnmarshalJSON added in v1.70.0

func (o *Value[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements [json.Unmarshaler].

func (*Value[T]) UnmarshalJSONV2 added in v1.70.0

func (o *Value[T]) UnmarshalJSONV2(dec *jsontext.Decoder, opts jsonv2.Options) error

UnmarshalJSONV2 implements jsonv2.UnmarshalerV2.

Jump to

Keyboard shortcuts

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