Documentation ¶
Overview ¶
Package tmpl implements a protobuf template-based generator.
Index ¶
- Variables
- type FileMap
- type FileMapDataItem
- type FileMapGenerate
- type Generator
- func (g *Generator) Generate() (response *plugin.CodeGeneratorResponse, err error)
- func (g *Generator) GenerateOutput(name string, ctx interface{}) (*plugin.CodeGeneratorResponse_File, error)
- func (g *Generator) ParseFileMap(dir, data string) error
- func (g *Generator) SetRequest(r *plugin.CodeGeneratorRequest) error
Constants ¶
This section is empty.
Variables ¶
var Preload = (&tmplFuncs{}).funcMap()
Functions ¶
This section is empty.
Types ¶
type FileMap ¶
type FileMap struct { // Dir is the directory to resolve template paths mentioned in the filemap // relative to. It should be the directory that this file map resides inside // of. The path will be converted to a unix-style one for protobuf, which // only deals with unix-style paths. Dir string `xml:",omitempty"` Generate []*FileMapGenerate `xml:"Generate"` }
FileMap represents a file mapping.
type FileMapDataItem ¶
FileMapDataItem represents a single pair in a map.
type FileMapGenerate ¶
type FileMapGenerate struct { // Template is the path of the template file to use for generating the // target. Template string // Target is the target proto file for generation. It must match one of the // input proto files, or else the template will not be executed. Target string `xml:",omitempty"` // Output is the output file to write the executed template contents to. Output string // Include is a list of template files to include for execution of the // template. Include []string `xml:"Includes>Include,omitempty"` // Data is effectively an map of items to pass onto the template during // execution. Data []*FileMapDataItem `xml:"Data>Item,omitempty"` }
FileMapGenerate represents a generate tag.
func (*FileMapGenerate) DataMap ¶
func (f *FileMapGenerate) DataMap() map[string]string
DataMap returns f.Data but as a Go map. It panics if there are any duplicate keys.
type Generator ¶
type Generator struct { // FileMap is the map of template files to use for the generation process. FileMap FileMap // RootDir is the root directory path prefix to place onto URLs for generated // types. RootDir string // APIHost is the base URL to use for rendering grpc-gateway routes, e.g.: // // http://api.mysite.com/ // APIHost string // ReadFile if non-nil is used to read template files, otherwise // ioutil.ReadFile is used. ReadFile func(path string) ([]byte, error) // contains filtered or unexported fields }
Generator is the type whose methods generate the output, stored in the associated response structure.
func (*Generator) Generate ¶
func (g *Generator) Generate() (response *plugin.CodeGeneratorResponse, err error)
Generate generates a response for g.Request (which you should unmarshal data into using protobuf).
If any error is encountered during generation, it is returned and should be considered fatal to the generation process (the response will be nil).
func (*Generator) GenerateOutput ¶
func (g *Generator) GenerateOutput(name string, ctx interface{}) (*plugin.CodeGeneratorResponse_File, error)
GenerateOutput generates a CodeGeneratorResponse_File for the output file name.
The ctx parameter specifies an arbitrary context for which to execute the template with, it is exposed to the executed template file as "Ctx".
func (*Generator) ParseFileMap ¶
ParseFileMap parses and executes a filemap template.
func (*Generator) SetRequest ¶
func (g *Generator) SetRequest(r *plugin.CodeGeneratorRequest) error
SetRequest sets the request the generator is generating a response for. If an error is returned generation is not safe (the request is bad) until a different request object is set successfully through this method.