gen

package
Version: v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2016 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

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 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 Generator

type Generator interface {
	// 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.
	Write(w io.Writer, fs *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(timport thriftPackageImporter, importPath string, packageName string) Generator

NewGenerator sets up a new generator for Go code.

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 plugin.Handle
}

Options controls how code gets generated.

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 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