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
- Variables
- func AttributeMarshaler(att *design.AttributeDefinition, context, source, target string) string
- func AttributeUnmarshaler(att *design.AttributeDefinition, context, source, target string) string
- func CommandLine() string
- func Comment(elems ...string) string
- func GoNativeType(t design.DataType) string
- func GoResDef(ds design.DataStructure, tabs int) string
- func GoTypeDef(ds design.DataStructure, tabs int, jsonTags, inner bool) string
- func GoTypeName(t design.DataType, tabs int) string
- func GoTypeRef(t design.DataType, tabs int) string
- func Goify(str string, firstUpper bool) string
- func Indent(s, prefix string) string
- func IndentBytes(b, prefix []byte) []byte
- func MediaTypeMarshaler(mt *design.MediaTypeDefinition, context, source, target, view string) string
- func MediaTypeMarshalerImpl(mt *design.MediaTypeDefinition, view string) string
- func RecursiveChecker(att *design.AttributeDefinition, required bool, target, context string, ...) string
- func RegisterFlags(r FlagRegistry)
- func RunTemplate(tmpl *template.Template, data interface{}) string
- func Tabs(depth int) string
- func Tempvar() string
- func TypeMarshaler(t design.DataType, context, source, target string) string
- func TypeUnmarshaler(t design.DataType, context, source, target string) string
- func UserTypeMarshalerImpl(u *design.UserTypeDefinition) string
- func UserTypeUnmarshalerImpl(u *design.UserTypeDefinition, context string) string
- func ValidationChecker(att *design.AttributeDefinition, required bool, target, context string, ...) string
- func WriteTabs(buf *bytes.Buffer, count int)
- type BaseCommand
- type Command
- type FlagRegistry
- type GoGenerator
- type ImportSpec
Constants ¶
const Version = "0.0.1"
Version of generator tools.
Variables ¶
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 )
var ( // TempCount holds the value appended to variable names to make them unique. TempCount int )
Functions ¶
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 ¶
Comment produces line comments by concatenating the given strings and producing 80 characters long lines starting with "//"
func GoNativeType ¶
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 ¶
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 ¶
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 ¶
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 Indent ¶
Indent inserts prefix at the beginning of each non-empty line of s. The end-of-line marker is NL.
func IndentBytes ¶
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 MediaTypeMarshalerImpl ¶
func MediaTypeMarshalerImpl(mt *design.MediaTypeDefinition, view string) string
MediaTypeMarshalerImpl returns the Go code for a function that marshals and validates instances of the given media type into raw values using the given view to render the attributes.
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 RunTemplate ¶
RunTemplate executs the given template with the given input and returns the rendered string.
func TypeMarshaler ¶
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 ¶
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 UserTypeMarshalerImpl ¶
func UserTypeMarshalerImpl(u *design.UserTypeDefinition) string
UserTypeMarshalerImpl returns the Go code for a function that marshals and validates instances of the given user type into raw values using the given view to render the attributes.
func UserTypeUnmarshalerImpl ¶
func UserTypeUnmarshalerImpl(u *design.UserTypeDefinition, context string) string
UserTypeUnmarshalerImpl returns the code implementing the user type unmarshaler function.
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.
Types ¶
type BaseCommand ¶
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) 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 ¶
ImportSpec defines a generated import statement.
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.