uniongen

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 ConfigureFormField

func ConfigureFormField(field *Field, attribute *expr.AttributeExpr, context *codegen.AttributeContext)

ConfigureFormField records captured native form fields with their actual Go selectors and presence layout. FieldType is already allocated by NameScope. Unsupported form contracts remain usable in JSON-only declarations.

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
	// MethodName is the required caller-supplied allocated Go suffix for
	// constructors, accessors and setters. Builders obtain it from
	// unionnames.Resolve together with FieldName; it is not derived during emission.
	MethodName 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
	// FormValue retains the captured nonobject branch codec.
	FormValue formgen.Field
	// FormFields are the flattened object's visible captured members.
	FormFields []*FormField
	// FormError describes an unsupported form contract on a reusable type.
	FormError string
	// FormBranchAllowsEmpty reports whether the selected branch can be
	// materialized from only its discriminator; constraints still validate it.
	FormBranchAllowsEmpty bool
	// EmptyValueExpr is the Go expression that initializes an empty branch
	// value when FormBranchAllowsEmpty 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 FormField

type FormField struct {
	// Field retains the resolved codec and nullability.
	formgen.Field
	// Name is the captured form key before a caller prefix.
	Name string
	// Selector is the allocated exported Go field name.
	Selector string
	// Presence uses value as the containing Go object expression.
	Presence string
	// Required requires this member's own wire evidence.
	Required bool
}

FormField combines captured policy with a declaration-owned Go selector.

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
	// Closed rejects unevaluated JSON members at this union occurrence.
	Closed 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