gen

package
v1.31.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2023 License: MIT Imports: 29 Imported by: 10

Documentation

Overview

Package gen generates Go code based on a compiled Thrift module specification.

NOTE: All APIs in this packages should be considered UNSTABLE and subject to change WITHOUT a major version bump.

Index

Constants

View Source
const NoZapLabel = "go.nolog"

NoZapLabel allows opt out of Zap logging for struct fields. Fields of Thrift structs will use this annotation to opt-out of being logged when that struct is logged. i.e.

struct ZapOptOutStruct {
	1: required string name
	2: required string optout (go.nolog)
}

The above struct will be logged without the optout string.

Variables

This section is empty.

Functions

func Constant

func Constant(g Generator, c *compile.Constant) error

Constant generates code for `const` expressions in Thrift files.

func ConstantValue

func ConstantValue(g Generator, c compile.ConstantValue, t compile.TypeSpec) (string, error)

ConstantValue generates an expression containing the given constant value of the given type.

The constant must already have been linked to the given type.

func ConstantValuePtr

func ConstantValuePtr(g Generator, c compile.ConstantValue, t compile.TypeSpec) (string, error)

ConstantValuePtr generates an expression which is a pointer to a value of type $t.

func Generate

func Generate(m *compile.Module, o *Options) error

Generate generates code based on the given options.

func Service

func Service(g Generator, s *compile.ServiceSpec) (map[string]*bytes.Buffer, error)

Service generates code for the given service.

Returns a map from file name to contents for that file. The file names are relative to the package directory for the service.

func ServiceFunction

func ServiceFunction(g Generator, s *compile.ServiceSpec, f *compile.FunctionSpec) error

ServiceFunction generates code for the given function of the given service.

func Services added in v1.20.0

func Services(g Generator, services map[string]*compile.ServiceSpec) error

Services generates code for all services into a single file and stores the code in the generator to be written.

func TypeCode

func TypeCode(g Generator, spec compile.TypeSpec) string

TypeCode gets an expression of type 'wire.Type' that represents the over-the-wire type code for the given TypeSpec.

func TypeDefinition

func TypeDefinition(g Generator, spec compile.TypeSpec) error

TypeDefinition generates code for the given TypeSpec.

func Version added in v0.4.0

func Version(g Generator, importPath string) error

Version generates an init() function checking the version of the library during generation with the one used at runtime.

Types

type CodeGenerator added in v1.24.0

type CodeGenerator struct {
	ServiceGenerator api.ServiceGenerator
}

CodeGenerator lists possible code generators for a plugin.

type Generator

type Generator interface {
	// MangleType generates a unique name for the given type that may be used
	// in private helper types and functions. This name is unique across all
	// types in the current context including imported types.
	MangleType(spec compile.TypeSpec) string

	// TextTemplate renders the given template with the given template
	// context.
	TextTemplate(s string, data interface{}, opts ...TemplateOption) (string, error)

	// DeclareFromTemplate renders the given template, and includes the
	// declarations from the generated Go code in the package being generated.
	// The next call to Write will write these declarations and any other
	// declarations that have not been written so far.
	DeclareFromTemplate(s string, data interface{}, opts ...TemplateOption) error

	// EnsureDeclared is similar to DeclareFromTemplate except that it simply
	// ignores conflicting definitions.
	EnsureDeclared(s string, data interface{}, opts ...TemplateOption) error

	// LookupTypeName returns the fully qualified name that should be used for
	// the given Thrift type. It imports the corresponding Go package if
	// necessary.
	//
	// This must be called with user-defined types only.
	LookupTypeName(compile.TypeSpec) (string, error)

	// LookupConstantName returns the fully qualified name that should be used
	// for the given Thrift constant. It imports the corresponding Go package if
	// necessary.
	LookupConstantName(*compile.Constant) (string, error)

	// Import ensures that the given package has been imported in the generated
	// code. Returns the name that should be used to reference the imported
	// module.
	Import(path string) string

	// Write the generated code to the given Writer and start a new file for
	// this package. All consecutive calls to DeclareFromTemplate will be
	// accumulated until the next Write call.
	//
	// A single Write must corresponds to a single file in the generated
	// package.
	//
	// The FileSet argument is deprecated and will be ignored.
	Write(w io.Writer, _ *token.FileSet) error
}

Generator allows generating declarations and other templated text for a single Go package which may be spread across multiple files.

Multiple files may be written by calling Write multiple times between calls to DeclareFromTemplate.

func NewGenerator

func NewGenerator(o *GeneratorOptions) Generator

NewGenerator sets up a new generator for Go code.

type GeneratorOptions added in v1.13.0

type GeneratorOptions struct {
	Importer    ThriftPackageImporter
	ImportPath  string
	PackageName string

	NoZap                 bool
	EnumTextMarshalStrict bool
}

GeneratorOptions controls a generator's behavior

type Namespace

type Namespace interface {
	// Generates a new name based on the given name. Prefers the provided name
	// but the name may be altered if it conflicts with another name in the
	// current scope.
	//
	// The returned name is also reserved so that future names cannot conflict
	// with it.
	NewName(name string) string

	// Reserve the given name in this namespace. Fails with an error if the name
	// is already taken.
	Reserve(name string) error

	// Forget the given name. Nothing happens if this name wasn't already
	// taken in this namespace. The name will NOT be removed from parent
	// namespaces.
	Forget(name string)

	// Create a new Child namespace. The child namespace cannot use any names
	// defined in this namespace or any of its parent namespaces.
	Child() Namespace

	// Rotate returns the oldest known name that was used for the given
	// requested name, and moves it to the end of the list of names used for
	// that requested name.
	//
	// This may be used to access the names used in a template dynamically in a
	// deterministic order if recording them in local variables is not possible.
	Rotate(name string) (string, error)
}

Namespace allows finding names that don't conflict with other names in certain scopes.

func NewNamespace

func NewNamespace() Namespace

NewNamespace creates a new namespace.

type Options

type Options struct {
	// OutputDir is the directory into which all generated code is written.
	//
	// This must be an absolute path.
	OutputDir string

	// PackagePrefix controls the import path prefix for all generated
	// packages.
	PackagePrefix string

	// ThriftRoot is the directory within whose tree all Thrift files consumed
	// are contained. The locations of the Thrift files relative to the
	// ThriftFile determines the module structure in OutputDir.
	//
	// This must be an absolute path.
	ThriftRoot string

	// NoRecurse determines whether code should be generated for included Thrift
	// files as well. If true, code gets generated only for the first module.
	NoRecurse bool

	// If true, we will not generate versioncheck.go files.
	NoVersionCheck bool

	// Code generation plugin
	Plugin CodeGenerator

	// Do not generate types.go
	NoTypes bool

	// Do not generate constants.go
	NoConstants bool

	// Do not generate service helpers
	NoServiceHelpers bool

	// Do not embed IDLs in generated code
	NoEmbedIDL bool

	// Do not generate Zap logging code
	NoZap bool

	// Name of the file to be generated by ThriftRW.
	OutputFile string

	// Generates an error on MarshalText and MarshalJSON if the enum value is
	// unrecognized.
	EnumTextMarshalStrict bool
}

Options controls how code gets generated.

type StreamGenerator added in v1.29.0

type StreamGenerator struct {
	// contains filtered or unexported fields
}

StreamGenerator generates code that knows how to encode and decode Thrift objects in a streaming fashion.

func (*StreamGenerator) Decode added in v1.29.0

func (sg *StreamGenerator) Decode(g Generator, spec compile.TypeSpec, reader string) (string, error)

Decode generates an expression that can deserialize Thrift data into their raw types.

func (*StreamGenerator) DecodePtr added in v1.29.0

func (sg *StreamGenerator) DecodePtr(g Generator, spec compile.TypeSpec, lhs string, reader string) (string, error)

DecodePtr generates an expression that assigns the "lhs" to a pointer of the decoded value.

func (*StreamGenerator) Encode added in v1.29.0

func (sg *StreamGenerator) Encode(g Generator, spec compile.TypeSpec, varName string, sw string) (string, error)

Encode generates code that knows how to serialize Thrift types into bytes.

func (*StreamGenerator) EncodePtr added in v1.29.0

func (sg *StreamGenerator) EncodePtr(g Generator, spec compile.TypeSpec, varName string, sw string) (string, error)

EncodePtr is the same as Encode except varName is expected to be a reference to a value of the given type.

type TemplateOption

type TemplateOption func(Generator, *template.Template) *template.Template

TemplateOption customizes templates.

func TemplateFunc

func TemplateFunc(name string, f interface{}) TemplateOption

TemplateFunc adds a function to the template.

The function may be anything accepted by text/template. If the function accepts a Generator as its first argument, it will be provided automatically.

type ThriftPackageImporter added in v1.12.0

type ThriftPackageImporter interface {
	// RelativePackage returns the import path for the top-level package of the
	// given Thrift file relative to the ImportPrefix.
	RelativePackage(file string) (importPath string, err error)

	// RelativePackage returns the relative path of the given Thrift file
	// relative to the ThriftRoot.
	RelativeThriftFilePath(file string) (relPath string, err error)

	// Package returns the import path for the top-level package of the given Thrift
	// file.
	Package(file string) (importPath string, err error)
}

ThriftPackageImporter determines import paths from a Thrift root.

type WireGenerator

type WireGenerator struct {
	// contains filtered or unexported fields
}

WireGenerator is responsible for generating code that knows how to convert between Thrift types and their Value representations.

func (*WireGenerator) FromWire

func (w *WireGenerator) FromWire(g Generator, spec compile.TypeSpec, value string) (string, error)

FromWire generates an expression of type ($spec, error) which reads the Value at $value into a $spec.

func (*WireGenerator) FromWirePtr

func (w *WireGenerator) FromWirePtr(g Generator, spec compile.TypeSpec, lhs string, value string) (string, error)

FromWirePtr generates a string assigning the given Value to the given lhs, which is a pointer to a value of the given type.

A variable err of type error MUST be in scope and will be assigned the parse error, if any.

func (*WireGenerator) ToWire

func (w *WireGenerator) ToWire(g Generator, spec compile.TypeSpec, varName string) (string, error)

ToWire generates an expression of type (Value, error) object containing the wire representation of the variable $varName of type $spec or an error.

func (*WireGenerator) ToWirePtr

func (w *WireGenerator) ToWirePtr(g Generator, spec compile.TypeSpec, varName string) (string, error)

ToWirePtr is the same as ToWire expect `varName` is expected to be a reference to a value of the given type.

Jump to

Keyboard shortcuts

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