codegen

package
v0.0.0-...-655ad1f Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2015 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package codegen contains common code used by all code generators. Each sub-package corresponds to a code generator. The "meta" sub-package is the generator generator: it contains code that compiles and runs a specific generator tool that uses the user metadata.

Index

Constants

View Source
const Version = "0.0.1"

Version of generator tools.

Variables

View Source
var (
	// OutputDir is the path to the directory the generated files should be
	// written to.
	OutputDir string

	// DesignPackagePath is the path to the user Go design package.
	DesignPackagePath string

	// Debug toggles debug mode.
	// If debug mode is enabled then the generated files are not
	// cleaned up upon failure.
	// Also logs additional debug information.
	// Set this flag to true prior to calling Generate.
	Debug bool

	// CommandName is the name of the command being run.
	CommandName string
)
View Source
var (
	// TempCount holds the value appended to variable names to make them unique.
	TempCount int
)

Functions

func ArrayMarshaler

func ArrayMarshaler(a *design.Array, context, source, target string) string

ArrayMarshaler produces the Go code that marshals an array for rendering. source is the name of the variable that contains the array value and target the name of the variable to initialize. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func ArrayUnmarshaler

func ArrayUnmarshaler(a *design.Array, context, source, target string) string

ArrayUnmarshaler produces the Go code that initializes an array from its deserialized epresentation. source is the name of the variable that contains the raw interface{} value and target the name of the variable to initialize. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func AttributeMarshaler

func AttributeMarshaler(att *design.AttributeDefinition, context, source, target string) string

AttributeMarshaler produces the Go code that initiliazes the variable named with the value of target which holds an interface{} with the content of the variable named with the value of source which contains an instance of the attribute type data structure. The attribute view is used to render child attributes if there are any. As with TypeMarshaler the code renders media type links and runs any validation defined on the type definition.

The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func AttributeUnmarshaler

func AttributeUnmarshaler(att *design.AttributeDefinition, context, source, target string) string

AttributeUnmarshaler produces the Go code that initializes an attribute given a deserialized (interface{}) value. source is the name of the variable that contains the raw interface{} value and target the name of the variable to initialize. context is used to keep track of recursion to produce helpful error messages in case of type mismatch or validation error. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func CommandLine

func CommandLine() string

CommandLine return the command used to run this process.

func Comment

func Comment(elems ...string) string

Comment produces line comments by concatenating the given strings and producing 80 characters long lines starting with "//"

func GoNativeType

func GoNativeType(t design.DataType) string

GoNativeType returns the Go built-in type from which instances of t can be initialized.

func GoResDef

func GoResDef(ds design.DataStructure, tabs int) string

GoResDef returns the Go code that defines a resource data structure.

func GoTypeDef

func GoTypeDef(ds design.DataStructure, tabs int, jsonTags, inner bool) string

GoTypeDef returns the Go code that defines a Go type which matches the data structure definition (the part that comes after `type foo`). tabs indicates the number of tab character(s) used to tabulate the definition however the first line is never indented. jsonTags controls whether to produce json tags. inner indicates whether to prefix the struct of an attribute of type object with *.

func GoTypeName

func GoTypeName(t design.DataType, tabs int) string

GoTypeName returns the Go type name for a data type. tabs is used to properly tabulate the object struct fields and only applies to this case.

func GoTypeRef

func GoTypeRef(t design.DataType, tabs int) string

GoTypeRef returns the Go code that refers to the Go type which matches the given data type (the part that comes after `var foo`) tabs is used to properly tabulate the object struct fields and only applies to this case.

func Goify

func Goify(str string, firstUpper bool) string

Goify makes a valid Go identifier out of any string. It does that by removing any non letter and non digit character and by making sure the first character is a letter or "_". Goify produces a "CamelCase" version of the string, if firstUpper is true the first character of the identifier is uppercase otherwise it's lowercase.

func HashMarshaler

func HashMarshaler(h *design.Hash, context, source, target string) string

HashMarshaler produces the Go code that initializes the variable named target which holds a map of interface{} to interface{} with the content of the variable named source which contains an instance of the hash map. The code runs any validation defined on the hash map key and value attribute definitions. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func HashUnmarshaler

func HashUnmarshaler(h *design.Hash, context, source, target string) string

HashUnmarshaler produces the Go code that initializes a hash map from its deserialized representation. source is the name of the variable that contains the raw map[string]interface{} value and target the name of the variable to initialize. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func Indent

func Indent(s, prefix string) string

Indent inserts prefix at the beginning of each non-empty line of s. The end-of-line marker is NL.

func IndentBytes

func IndentBytes(b, prefix []byte) []byte

IndentBytes inserts prefix at the beginning of each non-empty line of b. The end-of-line marker is NL.

func MediaTypeMarshaler

func MediaTypeMarshaler(mt *design.MediaTypeDefinition, context, source, target, view string) string

MediaTypeMarshaler produces the Go code that initializes the variable named target which holds a an interface{} with the content of the variable named source which contains an instance of the media type data structure. The code runs any validation defined on the media type definition. Also view is used to know which fields to copy and which ones to omit and for fields that are media types which view to use to render it. The rendering also takes care of following links. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func ObjectMarshaler

func ObjectMarshaler(o design.DataType, context, source, target string) string

ObjectMarshaler produces the Go code that initializes the variable named target which holds a map of string to interface{} with the content of the variable named source which contains an instance of the object data structure. The code runs any validation defined on the object attribute definitions. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func ObjectUnmarshaler

func ObjectUnmarshaler(o design.DataType, context, source, target string) string

ObjectUnmarshaler produces the Go code that initializes an object type from its deserialized representation. source is the name of the variable that contains the raw interface{} value and target the name of the variable to initialize. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func PrimitiveUnmarshaler

func PrimitiveUnmarshaler(p design.Primitive, context, source, target string) string

PrimitiveUnmarshaler produces the Go code that initializes a primitive type from its deserialized representation. source is the name of the variable that contains the raw interface{} value and target the name of the variable to initialize. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func RecursiveChecker

func RecursiveChecker(att *design.AttributeDefinition, required bool, target, context string, depth int) string

RecursiveChecker produces Go code that runs the validation checks recursively over the given attribute.

func RegisterFlags

func RegisterFlags(r FlagRegistry)

RegisterFlags registers the global flags.

func Tabs

func Tabs(depth int) string

Tabs returns a string made of depth tab characters.

func Tempvar

func Tempvar() string

Tempvar generates a unique variable name.

func TypeMarshaler

func TypeMarshaler(t design.DataType, context, source, target string) string

TypeMarshaler produces the Go code that initializes the variable named target which is an interface{} with the content of the variable named source which contains an instance of the type data structure. The code takes care of rendering media types according to the view defined on the attribute if any. It also renders media type links. Finally it validates the results using any type validation that is defined on the type attributes (if the type contains attributes). The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func TypeUnmarshaler

func TypeUnmarshaler(t design.DataType, context, source, target string) string

TypeUnmarshaler produces the Go code that initializes a variable of the given type given a deserialized (interface{}) value. source is the name of the variable that contains the raw interface{} value and target the name of the variable to initialize. context is used to keep track of recursion to produce helpful error messages in case of type mismatch or validation error. The generated code assumes that there is a variable called "err" of type error that it can use to record errors.

func ValidationChecker

func ValidationChecker(att *design.AttributeDefinition, required bool, target, context string, depth int) string

ValidationChecker produces Go code that runs the validation defined in the given attribute definition against the content of the variable named target recursively. context is used to keep track of recursion to produce helpful error messages in case of type validation error. The generated code assumes that there is a pre-existing "err" variable of type error. It initializes that variable in case a validation fails. Note: we do not want to recurse here, recursion is done by the marshaler/unmarshaler code.

func WriteTabs

func WriteTabs(buf *bytes.Buffer, count int)

WriteTabs is a helper function that writes count tabulation characters to buf.

Types

type BaseCommand

type BaseCommand struct {
	CmdName        string
	CmdDescription string
}

BaseCommand provides the basic logic for all commands. It implements the Command interface. Commands may then specialize to provide the specific Run behavior.

func NewBaseCommand

func NewBaseCommand(name, desc string) *BaseCommand

NewBaseCommand instantiates a base command.

func (*BaseCommand) Description

func (b *BaseCommand) Description() string

Description returns the command description.

func (*BaseCommand) Name

func (b *BaseCommand) Name() string

Name returns the command name.

func (*BaseCommand) RegisterFlags

func (b *BaseCommand) RegisterFlags(r FlagRegistry)

RegisterFlags is a dummy implementation, override in sub-command.

func (*BaseCommand) Run

func (b *BaseCommand) Run() ([]string, error)

Run is a dummy implementation, override in sub-command.

type Command

type Command interface {
	// Name of the command
	Name() string

	// Description returns the description used by the goa tool help.
	Description() string

	// RegisterFlags initializes the given registry flags with all
	// the flags relevant to this command.
	RegisterFlags(r FlagRegistry)

	// Run generates the generator code then compiles and runs it.
	// It returns the list of generated files.
	// Run uses the variables initialized by kingpin.Parse and
	// defined in RegisterFlags.
	Run() ([]string, error)
}

Command is the interface implemented by all generation goa commands. There is one command per generation target (i.e. app, docs, etc.)

type FlagRegistry

type FlagRegistry interface {
	// Flag defines a new flag with the given long name and help.
	Flag(name, help string) *kingpin.FlagClause
}

FlagRegistry is the interface implemented by kingpin.Application and kingpin.CmdClause to register flags.

type GoGenerator

type GoGenerator struct {
	// Filename of destination file
	Filename string
	// HeaderTmpl is the generic generated code header template.
	HeaderTmpl *template.Template
	// FuncMap is the template helper functions map.
	FuncMap template.FuncMap
}

GoGenerator provide the basic implementation for a Go code generator. Other generators can use this basic generator and provide specialized behavior that implements the generator package Generate function.

func NewGoGenerator

func NewGoGenerator(filename string) *GoGenerator

NewGoGenerator returns a Go code generator that writes to the given file.

func (*GoGenerator) FormatCode

func (w *GoGenerator) FormatCode() error

FormatCode runs "goimports -w" on the generated file.

func (*GoGenerator) Write

func (w *GoGenerator) Write(b []byte) (int, error)

Write implements io.Writer so that variables of type *GoGenerator can be used in template.Execute.

func (*GoGenerator) WriteHeader

func (w *GoGenerator) WriteHeader(title, pack string, imports []*ImportSpec) error

WriteHeader writes the generic generated code header.

type ImportSpec

type ImportSpec struct {
	Name string
	Path string
}

ImportSpec defines a generated import statement.

func NewImport

func NewImport(name, path string) *ImportSpec

NewImport creates an import spec.

func SimpleImport

func SimpleImport(path string) *ImportSpec

SimpleImport creates an import with no explicit path component.

func (*ImportSpec) Code

func (s *ImportSpec) Code() string

Code returns the Go import statement for the ImportSpec.

Jump to

Keyboard shortcuts

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