Help us shape the future of Go! Take the 2026 Go Developer Survey

variant

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InferType added in v0.2.0

func InferType[T ~string](candidates ...Candidate[T]) (enum.Entry[T], error)

InferType returns the type of the one candidate that is set.

Types

type Candidate added in v0.2.0

type Candidate[T ~string] struct {
	Type  enum.Entry[T]
	IsSet bool
}

A Candidate is one variant of a type that holds exactly one of several variants, one field each.

func NewCandidate added in v0.2.0

func NewCandidate[T ~string](typ enum.Entry[T], isSet bool) Candidate[T]

type Variant

type Variant[X, Y any] struct {
	X X
	Y Y
}

A Variant represents a single JSON map entry having two different Go type representations X and Y. After JSON unmarshalling you can check with HasX, HasY which field has been detected, while X is preferred. An example usage is a Client DTO response which can either be struct representing a secret hash, or a simple string response if that's a non-sensitive value.

func (Variant[X, Y]) HasX

func (v Variant[X, Y]) HasX() bool

func (Variant[X, Y]) HasY

func (v Variant[X, Y]) HasY() bool

func (Variant[X, Y]) MarshalJSON

func (v Variant[X, Y]) MarshalJSON() ([]byte, error)

func (*Variant[X, Y]) UnmarshalJSON

func (v *Variant[X, Y]) UnmarshalJSON(bytes []byte) error

func (Variant[X, Y]) WithX

func (v Variant[X, Y]) WithX(action func(x *X))

func (Variant[X, Y]) WithY

func (v Variant[X, Y]) WithY(action func(y *Y))

Jump to

Keyboard shortcuts

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