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

uniongen

package
v1.10.0-alpha.15 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package uniongen renders Go sum types for service, transport and plugin generators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Emit

func Emit(stmt *jen.Statement, data *Type)

Emit appends the union declaration, constructors, accessors and codecs to stmt.

func MarshalFormBody

func MarshalFormBody(data *Type) string

MarshalFormBody renders the body of the union MarshalForm method.

func MarshalJSONBody

func MarshalJSONBody(data *Type) string

MarshalJSONBody renders the body of the union MarshalJSON method.

func UnmarshalFormBody

func UnmarshalFormBody(data *Type) string

UnmarshalFormBody renders the body of the union UnmarshalForm method.

func UnmarshalJSONBody

func UnmarshalJSONBody(data *Type) string

UnmarshalJSONBody renders the body of the union UnmarshalJSON method.

func ValidateBody

func ValidateBody(data *Type) string

ValidateBody renders the body of the union Validate method.

Types

type Field

type Field struct {
	// Name is the branch name as defined in the DSL.
	Name string
	// KindConst is the Go identifier for the kind constant of this branch.
	KindConst string
	// FieldName is the struct field name in the union.
	FieldName string
	// FieldType is the Go type used in the union struct field and public API.
	FieldType string
	// AllowsNull reports whether the branch contract admits an explicit JSON null.
	AllowsNull bool
	// ValidateCode validates a decoded untagged branch held in v.
	ValidateCode string
	// ValidateRef is the Go expression that validates a decoded branch.
	ValidateRef string
	// FlatFormObject is true when the branch value is object-shaped and form
	// encoding should flatten its fields under the current prefix.
	FlatFormObject bool
	// FlatFormObjectAllowsEmpty is true when the flattened object branch may be
	// selected with only the discriminator because it has no required fields.
	FlatFormObjectAllowsEmpty bool
	// EmptyValueExpr is the Go expression that initializes an empty branch
	// value when FlatFormObjectAllowsEmpty is true.
	EmptyValueExpr string
	// EmitPrimitiveAlias is true when the branch uses a generated primitive alias
	// that must be declared in the same file as the union type. Service data
	// sets it only for bare Any branches, whose alias is loom.JSONValue.
	EmitPrimitiveAlias bool
	// PrimitiveAliasType is the underlying Go type used by the generated branch
	// alias (for example "string" or "float64").
	PrimitiveAliasType string
	// TypeTag is the JSON "type" discriminator value for this branch.
	TypeTag string
}

Field describes a single branch of a union.

type Type

type Type struct {
	// Name is the Go type name of the union struct.
	Name string
	// KindName is the Go type name of the discriminator kind.
	KindName string
	// Fields describes each union branch.
	Fields []*Field
	// Loc defines the file and Go package of the union type if overridden via
	// Meta. When nil the type is generated in the default service file.
	Loc *codegen.Location
	// TypeKey is the discriminator field name for JSON marshaling (defaults to "type").
	TypeKey string
	// ValueKey is the value field name for JSON marshaling (defaults to "value").
	ValueKey string
	// Untagged reports whether JSON marshaling emits the selected branch value
	// directly instead of the canonical discriminator/value wrapper.
	Untagged bool
	// JSONOnly omits form codecs for transports that only consume JSON.
	// The zero value preserves both JSON and form emission.
	JSONOnly bool
	// JSON holds shared untagged JSON analysis; nil for tagged unions.
	JSON *unionjson.Union
	// HasScalarFormBranch is true when at least one branch keeps canonical
	// type/value form encoding.
	HasScalarFormBranch bool
}

Type describes a generated sum-type union for service and transport code.

Jump to

Keyboard shortcuts

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