Documentation ¶
Overview ¶
Package opt defines optional types.
Index ¶
- type Bool
- type BoolFlag
- type Value
- func (o *Value[T]) Clear()
- func (o Value[T]) Equal(v Value[T]) bool
- func (o Value[T]) Get() T
- func (o Value[T]) GetOk() (v T, ok bool)
- func (o Value[T]) GetOr(def T) T
- func (o *Value[T]) IsSet() bool
- func (o Value[T]) MarshalJSON() ([]byte, error)
- func (o Value[T]) MarshalJSONV2(enc *jsontext.Encoder, opts jsonv2.Options) error
- func (o *Value[T]) Set(v T)
- func (o Value[T]) String() string
- func (o *Value[T]) UnmarshalJSON(b []byte) error
- func (o *Value[T]) UnmarshalJSONV2(dec *jsontext.Decoder, opts jsonv2.Options) error
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 ¶
NewBool constructs a new Bool value equal to b. The returned Bool is set, unless Set("") or Clear() methods are called.
func (Bool) EqualBool ¶
EqualBool reports whether b is equal to v. If b is empty or not a valid bool, it reports false.
func (Bool) MarshalJSON ¶
func (*Bool) UnmarshalJSON ¶
type BoolFlag ¶
type BoolFlag struct {
*Bool
}
BoolFlag is a wrapper for Bool that implements flag.Value.
func (*BoolFlag) Set ¶
Set the value of b, using any value supported by strconv.ParseBool.
type Value ¶
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 ¶
ValueOf returns an optional Value containing the specified value. It treats nil slices and maps as empty slices and maps.
func (Value[T]) Equal ¶
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 ¶
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]) GetOr ¶
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]) MarshalJSON ¶
MarshalJSON implements [json.Marshaler].
func (Value[T]) MarshalJSONV2 ¶
MarshalJSONV2 implements jsonv2.MarshalerV2.
func (*Value[T]) Set ¶
func (o *Value[T]) Set(v T)
Set assigns the specified value to the optional value o.
func (*Value[T]) UnmarshalJSON ¶
UnmarshalJSON implements [json.Unmarshaler].
func (*Value[T]) UnmarshalJSONV2 ¶
UnmarshalJSONV2 implements jsonv2.UnmarshalerV2.