generator

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BingenSupportTemplate is the template name for supporting bingen code, including the file
	// header, package declaration, imports, and various utilities.
	BingenSupportTemplate = "support.go.tmpl"

	// BingenMarshallerTemplate is the template name for the binary marshalling generation. It contains
	// the MarshalBinary() implementation as well as various sub-templates for writing different types.
	BingenMarshallerTemplate = "marshaller.go.tmpl"

	// BingenUnmarshallerTemplate is the template name for the binary unmarshalling generation. It contains
	// the UnmarshalBinary() implementation as well as various sub-templates for reading different types.
	BingenUnmarshallerTemplate = "unmarshaller.go.tmpl"

	// BingenStreamerTemplate is the template name for the binary streaming generation. It contains variations
	// to the unmarshalling generation as well as calls into the unmarshalling template.
	BingenStreamerTemplate = "streamer.go.tmpl"
)

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(newErr string) string

func Generate

func Generate(dir string, pkg string, bufferImport string, tc types.TypeCollection)

func MakeMap

func MakeMap(kvPairs ...any) map[string]any

MakeMap is a helper function made available to the teplating engine to create dynamic parameters to pass to various templates. ie: (Map "Context" .Context "Type" .Type.InnerType)

Templates accept a single object called a "pipeline". From that object, all properties can be accessed via . notation. So, for the above pipeline, .Context and .Type. In scenarios where templates create parameters on the fly, in order to pass those parameters to different templates, you can use this func to build the pipeline.

func StreamErrorHandler

func StreamErrorHandler(errString string) string

func ToDefaultValue

func ToDefaultValue(typeCode uint8, value string) string

ToDefaultValue provides a primitive default value mapping to string formatters for quickly generating the type casted default value for a type and default.

func WriteMarshallerTemplate

func WriteMarshallerTemplate(writer io.Writer, params MarshallerParams) error

WriteMarshallerTemplate writes the binary marshalling code to the provided `io.Writer`

func WriteStreamerTemplate

func WriteStreamerTemplate(writer io.Writer, params StreamParams) error

WriteStreamerTemplate writes all of the streaming unmarshalling code to the provided `io.Writer`

func WriteSupportTemplate

func WriteSupportTemplate(writer io.Writer, params SupportParams) error

WriteSupportTemplate writes the file header, package declaration, imports, and all supporting bingen code to the provided `io.Writer`

func WriteUnmarshallerTemplate

func WriteUnmarshallerTemplate(writer io.Writer, params UnmarshallerParams) error

WriteUnmarshallerTemplate writes the binary unmarshalling code to the provided `io.Writer`

Types

type GeneratorContext

type GeneratorContext interface {
	// VersionSetConst returns the version set constant used for this generator
	VersionSetConst() string

	// HandleError handles any inline errors that occur during reading or writing by returning the
	// generated error handling code for the provided error string
	HandleError(errString string) string

	// IsStreamable returns true if we should generate streaming implementations of the type
	IsStreamable() bool

	// IsStringTable returns true if we store/load a table of strings in the format
	IsStringTable() bool

	// IsPreProcess returns true if we should pre process the generated struct before encoding.
	IsPreProcess() bool

	// IsPostProcess returns true if we should post process the generated struct before returning.
	IsPostProcess() bool

	// IsMigration returns true if we should test for a migration scenario (version delta), and call
	// a migration func.
	IsMigration() bool

	// NextVar returns the next available general variable name which can be used to avoid collision.
	NextVar() string

	// NextMapVar returns the next available map variable name which can be used to avoid collisions.
	NextMapVar() string

	// NextLoopVar returns the next available loop index variable name which can be used to avoid collisions.
	NextLoopVar() string

	// NextErrVar returns the next available ok variable name.
	NextOKVar() string

	// NextErrVar returns the next available error variable name.
	NextErrVar() string

	// WithErrorHandler allows the context to be copied with a new error handler override
	WithErrorHandler(handler func(string) string) GeneratorContext
}

GeneratorContext is a stateful context that controls the flow of generation based on the scope of the generation.

type GeneratorContextFactory

type GeneratorContextFactory interface {
	// NewContext creates a new GeneratorContext implementation
	NewContext(opts *types.GenerateTypeOpts) GeneratorContext
}

GeneratorContextFactory creates new GeneratorContext instances

func NewGeneratorContextFactory

func NewGeneratorContextFactory(errorHandler func(string) string) GeneratorContextFactory

NewGeneratorContextFactory creates a default GeneratorContext with the default error handler

type MarshallerParams

type MarshallerParams struct {
	Context GeneratorContext
	Type    types.GenType
	Target  vars.Target
}

MarshallerParams contains the general set of parameters to pass to any type marshalling generator. It contains the current generator context, the type currently being marshalled, and the name of the target to write from.

func ToWriteParams

func ToWriteParams(context GeneratorContext, t types.GenType, target vars.Target) MarshallerParams

ToWriteParams is a helper function made available to the templating engine to create MarshallerParams inline.

type StreamParams

type StreamParams struct {
	Context GeneratorContext
	Type    types.GenType
}

StreamParams contains the general set of parameters to pass to any type streaming generator. It contains the current generator context and the type currently being generated.

func ToStreamParams

func ToStreamParams(context GeneratorContext, t types.GenType) StreamParams

ToStreamParams is a helper function made available to the templating engine to create StreamParams inline.

type SupportParams

type SupportParams struct {
	Package         string
	Imports         []string
	BufferImport    string
	VersionSets     []meta.VersionSet
	Types           []types.GenType
	StreamableTypes []*types.StructType
}

SupportParams contains the general set of parameters to pass to the support generator. This includes code that supports all bingen operations.

type UnmarshallerParams

type UnmarshallerParams struct {
	Context GeneratorContext
	Type    types.GenType
	Target  vars.Target
	Set     bool
}

UnmarshallerParams contains the general set of parameters to pass to any type unmarshalling generator. It contains the current generator context, the type currently being unmarshalled, the target to read into, and whether or not that target requires initialization (Set should be false if the target already exists).

func ToReadParams

func ToReadParams(context GeneratorContext, t types.GenType, target vars.Target, set bool) UnmarshallerParams

ToReadParams is a helper function made available to the templating engine to create UnmarshallerParams inline.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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