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 ¶ added in v1.58.0
NewBool constructs a new Bool value equal to b. The returned Bool is set, unless Set("") or Clear() methods are called.
func (Bool) EqualBool ¶ added in v0.100.0
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 ¶ 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
Set the value of b, using any value supported by strconv.ParseBool.
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
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
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
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]) MarshalJSON ¶ added in v1.70.0
MarshalJSON implements [json.Marshaler].
func (Value[T]) MarshalJSONV2 ¶ added in v1.70.0
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
String implements fmt.Stringer.
func (*Value[T]) UnmarshalJSON ¶ added in v1.70.0
UnmarshalJSON implements [json.Unmarshaler].
func (*Value[T]) UnmarshalJSONV2 ¶ added in v1.70.0
UnmarshalJSONV2 implements jsonv2.UnmarshalerV2.