Documentation ¶
Overview ¶
Package gen implements code generation functionality that work on interface specifications from the parse package. Code is generated using the package "github.com/dave/jennifer/jen".
Index ¶
- func AddDefaultPackageComment(f *jen.File)
- func LowercaseFirst(s string) string
- func UppercaseFirst(s string) string
- type GenResult
- type GeneratedFile
- type Generator
- type SimpleGenerator
- func (s *SimpleGenerator) GenFunction(receiver jen.Code, name string, params jen.Code, returns jen.Code, ...) jen.Code
- func (s *SimpleGenerator) GenFunctionParams(params []parse.Param) jen.Code
- func (s *SimpleGenerator) GenParamNames(params []parse.Param) []string
- func (s *SimpleGenerator) GenParamType(p parse.ParamType) jen.Code
- func (s *SimpleGenerator) GenParamTypes(params []parse.Param) []jen.Code
- func (s *SimpleGenerator) GenReturnParams(params []parse.Param) jen.Code
- func (s *SimpleGenerator) GenStructType(name string, fields []jen.Code) jen.Code
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddDefaultPackageComment ¶
Adds the following package comment to the given code file: "generated code, do not edit"
func LowercaseFirst ¶
Lowercase first letter of string, useful for generating e.g. parameter or variable names.
func UppercaseFirst ¶
Uppercase first letter of string, userful for generating e.g. exported struct or function names.
Types ¶
type GenResult ¶
type GenResult struct { // Piece of code, that does not include any package or import statements. Code *jen.Group // Full path of package this code belongs to, e.g. "example.com/abc/xyz". PackagePath string // Package name of package this code belongs to, usually the last section of the package path, e.g. "xyz". PackageName string // Define explicit aliases for imports used by this code. // Maps from package path to alias, e.g. "example.com/abc/xyz":"x" Imports map[string]string // Path of the file the code should ultimately be written to. OutputFile string }
A piece of code returned by a code generator, that is assigned to a particular output package/file. Multiple instances of GenResult can be merged into a single code file, since different code generators might provide parts of it.
type GeneratedFile ¶
func MergeResults ¶
func MergeResults(results []GenResult) []GeneratedFile
Generates a list of GeneratedFile values by merging together all the code pieces for the same output file path into a single code file.
type Generator ¶
The interface all code generators should implement. A code generator should be created with a specification that defines what exactly needs to be generated. Calling the Generate() method should then always yield the same result.
A code generator should not actually write any files, instead the Generate() method should return a list of GenResult values that each define a piece of code belonging to an output file.
type SimpleGenerator ¶
type SimpleGenerator struct{}
SimpleGenerator provides functions to make it easier to generate common code elements like parameters, struct types and functions.
func NewSimpleGenerator ¶
func NewSimpleGenerator() *SimpleGenerator
func (*SimpleGenerator) GenFunction ¶
func (s *SimpleGenerator) GenFunction(receiver jen.Code, name string, params jen.Code, returns jen.Code, body []jen.Code) jen.Code
Generates a function with the given receiver, name, parameters, return values and statements in the body.
func (*SimpleGenerator) GenFunctionParams ¶
func (s *SimpleGenerator) GenFunctionParams(params []parse.Param) jen.Code
Generatesa parameter list for the given parameter specification. E.g. (ctx context.Context, p1 string, p2 int) If parameter specifications do not contain a name, consecutive integers will be used, e.g. "p0", "p1", "p2".
func (*SimpleGenerator) GenParamNames ¶
func (s *SimpleGenerator) GenParamNames(params []parse.Param) []string
Returns a list of parameter names for the given parameter specification. If a parameter specification contains a name, that name will be used, otherwise a name is generated by using consecutive integers, i.e. "p0", "p1", "p2". The underscore in the generated param names is used to avoid any naming conflicts with existing code elements.
func (*SimpleGenerator) GenParamType ¶
func (s *SimpleGenerator) GenParamType(p parse.ParamType) jen.Code
Generates a type e.g. for a function parameter or return value.
func (*SimpleGenerator) GenParamTypes ¶
func (s *SimpleGenerator) GenParamTypes(params []parse.Param) []jen.Code
func (*SimpleGenerator) GenReturnParams ¶
func (s *SimpleGenerator) GenReturnParams(params []parse.Param) jen.Code
func (*SimpleGenerator) GenStructType ¶
Generates a struct type with the given name and fields.