Documentation ¶
Overview ¶
Package code is a small wrapper for https://github.com/dave/jennifer this package is used to easily generate go code it also has a very straight forward api.
e.x to create a structure:
structure := code.NewStruct("MyStruct") fmt.Println(structure) // type MyStruct struct{}
to add fields to the structure
structure.Fields = []code.StructField{ code.NewStructField("Name", code.NewType("string")), } fmt.Println(structure) // type MyStruct struct { // Name string // }
Index ¶
- type Code
- type Comment
- type Const
- type FieldTags
- type File
- type Function
- func (f *Function) AddDocs(docs ...Comment)
- func (f *Function) AddParameter(p Parameter)
- func (f *Function) AddResult(p Parameter)
- func (f *Function) AddStringBody(s string)
- func (f *Function) Code() *jen.Statement
- func (f *Function) Docs() []Comment
- func (f *Function) ImportAliases() []ImportAlias
- func (f *Function) String() string
- type FunctionOptions
- func BodyFunctionOption(body ...jen.Code) FunctionOptions
- func DocsFunctionOption(docs ...Comment) FunctionOptions
- func FunctionTypeName(name string) FunctionOptions
- func ParamsFunctionOption(params ...Parameter) FunctionOptions
- func RecvFunctionOption(recv *Parameter) FunctionOptions
- func ResultsFunctionOption(results ...Parameter) FunctionOptions
- type FunctionType
- type Import
- type ImportAlias
- type Interface
- type InterfaceMethod
- type Parameter
- type RawCode
- type Struct
- type StructField
- type StructType
- type Type
- type TypeOptions
- func ArrayTypeOption(tp Type) TypeOptions
- func FunctionTypeOption(m *FunctionType) TypeOptions
- func ImportTypeOption(i Import) TypeOptions
- func MapTypeOption(key Type, value Type) TypeOptions
- func PointerTypeOption() TypeOptions
- func StructTypeOption(st StructType) TypeOptions
- func VariadicTypeOption() TypeOptions
- type Var
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Code ¶
type Code interface { // Docs returns the documentation comments Docs() []Comment // String returns the string representation of the code. String() string // Code returns the jen representation of the code. Code() *jen.Statement // AddDocs adds documentation comments to the code. AddDocs(docs ...Comment) ImportAliases() []ImportAlias }
Code is the interface that all code nodes need to implement.
type Comment ¶
type Comment string
Comment represents a code comment, it implements the Code interface.
func NewComment ¶
NewComment creates a Comment with the given string.
func (Comment) AddDocs ¶
AddDocs does nothing for the comment code. We only implement this so we implement the Code interface.
func (Comment) ImportAliases ¶ added in v0.1.7
func (c Comment) ImportAliases() []ImportAlias
type Const ¶
type Const Var
Const represents a constant, it has the same attributes as the variable only that it assumes that the value is always set (there can not be any constant without initial value).
func (*Const) ImportAliases ¶ added in v0.1.7
func (c *Const) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the constant.
type FieldTags ¶
FieldTags represent the structure field tags (e.x `json:"test"`).
func NewFieldTags ¶
NewFieldTags creates a new field tags map with initial key and value. there is also an optional list of documentation comments that you can add to the variable
type File ¶
type File struct { Code []Code // contains filtered or unexported fields }
File represents a go source file.
func (*File) AppendAfter ¶
AppendAfter appends a new code node after the given code node.
func (*File) PrependBefore ¶
PrependBefore prepends a new code node before the given code node.
func (*File) SetImportAliases ¶
func (f *File) SetImportAliases(ia []ImportAlias)
SetImportAliases sets the files import aliases, if the jenFile representation of the file is nil SetImportAliases will do nothing.
type Function ¶
type Function struct { // Name is the name of the function. Name string // Recv is the receiver of the function (e.x func (rcv *MyStruct) name() {}). Recv *Parameter // Params are the functions parameters. Params []Parameter // Results are the functions results. Results []Parameter // Body is the function body. Body []jen.Code // contains filtered or unexported fields }
Function represents a function.
func NewFunction ¶
func NewFunction(name string, options ...FunctionOptions) *Function
NewFunction creates a new function with the given name and options.
func (*Function) AddParameter ¶
AddParameter adds a new parameter to the function.
func (*Function) AddStringBody ¶
AddStringBody adds raw string code to the body of the function.
func (*Function) ImportAliases ¶ added in v0.1.7
func (f *Function) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the function.
type FunctionOptions ¶
type FunctionOptions func(m *Function)
FunctionOptions is used when you call NewFunction, it is a handy way to allow multiple configurations for a function.
Code has some preconfigured option functions you can use to add parameter, results, receiver, body, docs to the function.
func BodyFunctionOption ¶
func BodyFunctionOption(body ...jen.Code) FunctionOptions
BodyFunctionOption adds given body code to the function.
func DocsFunctionOption ¶
func DocsFunctionOption(docs ...Comment) FunctionOptions
DocsFunctionOption adds given docs to the function.
func FunctionTypeName ¶ added in v0.1.10
func FunctionTypeName(name string) FunctionOptions
func ParamsFunctionOption ¶
func ParamsFunctionOption(params ...Parameter) FunctionOptions
ParamsFunctionOption adds given parameters to the function.
func RecvFunctionOption ¶
func RecvFunctionOption(recv *Parameter) FunctionOptions
RecvFunctionOption sets the function receiver.
func ResultsFunctionOption ¶
func ResultsFunctionOption(results ...Parameter) FunctionOptions
ResultsFunctionOption adds given results to the function.
type FunctionType ¶
type FunctionType Function
FunctionType is used in Type to specify a method type (e.x func(sting) int).
func NewFunctionType ¶
func NewFunctionType(options ...FunctionOptions) *FunctionType
NewFunctionType creates a new function type with the given options.
func (*FunctionType) AddDocs ¶
func (m *FunctionType) AddDocs(_ ...Comment)
AddDocs does nothing for the function type code. We only implement this so we implement the Code interface.
func (*FunctionType) Code ¶
func (m *FunctionType) Code() *jen.Statement
Code returns the jen representation of the function type.
func (*FunctionType) Docs ¶
func (m *FunctionType) Docs() []Comment
Docs does nothing for the function type code.
func (*FunctionType) String ¶
func (m *FunctionType) String() string
String returns the go code string of the function type. because the renderer does not render only function types we create a dummy structure to add the function type field to than we remove everything besides the function type.
type Import ¶
type Import struct { // FilePath is the path of the package. (e.x /User/Kujtim/go/src/github.com/go-services/code). FilePath string // Alias is the alias of the import (e.x import my_alias "fmt"). Alias string // Path is the path of the import (e.x import "github.com/go-services/code"). Path string }
Import represents an import this is mostly used for parameters and field types so we know what the package of the type is.
func NewImport ¶
NewImport creates a new Import with the given alias and path.
alias is the alias you want to give a package (e.x import my_alias "fmt") path is the import path (e.x "github.com/go-services/code")
func NewImportWithFilePath ¶
NewImportWithFilePath creates a new Import with the given alias and path and filepath.
filePath is the path in the filesystem of the source code.
type ImportAlias ¶
ImportAlias is used to specify the import alias in a file
func NewImportAlias ¶
func NewImportAlias(name, path string) ImportAlias
NewImportAlias creates a new import alias with the given name and path.
type Interface ¶
type Interface struct { // Name is the name of the interface. Name string // Methods are the interface methods, the interface can also have no methods. Methods []InterfaceMethod // contains filtered or unexported fields }
Interface is the representation of an interface.
func NewInterface ¶
func NewInterface(name string, methods []InterfaceMethod, docs ...Comment) *Interface
NewInterface creates a new interface with the given name and methods, there is also an optional list of documentation comments that you can add to the variable
func (*Interface) AddMethod ¶
func (i *Interface) AddMethod(m InterfaceMethod)
AddMethod a method to the method list
func (*Interface) ImportAliases ¶ added in v0.1.7
func (i *Interface) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the interface.
type InterfaceMethod ¶
type InterfaceMethod Function
InterfaceMethod is the representation of interface methods (e.x String() string).
func NewInterfaceMethod ¶
func NewInterfaceMethod(name string, options ...FunctionOptions) InterfaceMethod
NewInterfaceMethod creates a new interface method with the given name and options.
func (*InterfaceMethod) AddDocs ¶
func (m *InterfaceMethod) AddDocs(docs ...Comment)
AddDocs adds a list of documentation strings to the interface method.
func (*InterfaceMethod) Code ¶
func (m *InterfaceMethod) Code() *jen.Statement
Code returns the jen representation of the interface method.
func (*InterfaceMethod) Docs ¶
func (m *InterfaceMethod) Docs() []Comment
Docs returns the docs comments of the interface method.
func (*InterfaceMethod) ImportAliases ¶ added in v0.1.7
func (m *InterfaceMethod) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the interface method.
func (*InterfaceMethod) String ¶
func (m *InterfaceMethod) String() string
String returns the go code string of the interface method. because the renderer does not render only interface methods we create a dummy interface to add the interface method to than we remove everything besides the interface method.
type Parameter ¶
Parameter represents a parameter, it is used in function parameters, function return parameters.
func NewParameter ¶
NewParameter creates a new parameter with the given name and type.
func (*Parameter) AddDocs ¶
AddDocs does nothing for the parameter code. We only implement this so we implement the Code interface.
type RawCode ¶
type RawCode struct {
// contains filtered or unexported fields
}
RawCode represents raw lines of code.
func NewRawCode ¶
func (*RawCode) AddDocs ¶
AddDocs does nothing for the code. We only implement this so we implement the Code interface.
func (*RawCode) ImportAliases ¶ added in v0.1.7
func (c *RawCode) ImportAliases() []ImportAlias
type Struct ¶
type Struct struct { // Name represents the name of the structure. Name string // Fields represents the structure fields. Fields []StructField // contains filtered or unexported fields }
Struct represent a structure.
func NewStruct ¶
NewStruct creates a new structure with the given name, there is also an optional list of documentation comments that you can add to the variable
func NewStructWithFields ¶
func NewStructWithFields(name string, fields []StructField, docs ...Comment) *Struct
NewStructWithFields creates a new structure with the given name and fields, there is also an optional list of documentation comments that you can add to the variable
func (*Struct) ImportAliases ¶ added in v0.1.7
func (s *Struct) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the structure.
type StructField ¶
type StructField struct { // Parameter is used for representing the name and type because the go code is the same. Parameter // Tags represent the field tags. Tags *FieldTags // contains filtered or unexported fields }
StructField represent a structure field, it uses the parameter representation to represent the name and the type the difference between parameter and struct field is that struct fields can have docs and tags.
func NewStructField ¶
func NewStructField(name string, tp Type, docs ...Comment) *StructField
NewStructField creates a new structure field with the given name and type, there is also an optional list of documentation comments that you can add to the variable
func NewStructFieldWithTag ¶
func NewStructFieldWithTag(name string, tp Type, tags *FieldTags, docs ...Comment) *StructField
NewStructFieldWithTag creates a new structure field with the given name type and tags, there is also an optional list of documentation comments that you can add to the variable
func (*StructField) Code ¶
func (s *StructField) Code() *jen.Statement
Code returns the jen representation of the structure field.
func (*StructField) Docs ¶
func (s *StructField) Docs() []Comment
Docs returns the docs comments of the structure field.
func (*StructField) ImportAliases ¶ added in v0.1.8
func (s *StructField) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the structure field.
func (*StructField) String ¶
func (s *StructField) String() string
String returns the go code string of the struct field. because the renderer does not render only struct fields we create a dummy structure to add the struct field to than we remove everything besides the struct field.
type StructType ¶ added in v0.1.6
type StructType Struct
StructType is used in Type to specify a struct type (e.x struct{}).
func NewStructType ¶ added in v0.1.6
func NewStructType(fields ...StructField) *StructType
NewStructType creates a new struct type with the given fields
func (*StructType) AddDocs ¶ added in v0.1.6
func (s *StructType) AddDocs(_ ...Comment)
AddDocs does nothing for the struct type code. We only implement this so we implement the Code interface.
func (*StructType) Code ¶ added in v0.1.6
func (s *StructType) Code() *jen.Statement
Code returns the jen representation of the struct type.
func (*StructType) Docs ¶ added in v0.1.6
func (s *StructType) Docs() []Comment
Docs does nothing for the struct type code.
func (*StructType) String ¶ added in v0.1.6
func (s *StructType) String() string
String returns the go code string of the struct type. because the renderer does not render only struct types we create a dummy structure to add the struct type field to than we remove everything besides the struct type.
type Type ¶
type Type struct { // Import specifies the import of the type, it is used so we know how to call jen.Qual. // e.x if you want to specify the type to be `context.Context`. // the Import would be // Import{ // Path:"context" // } // and the Qualifier = Context Import *Import // Function is used in Type to specify a method type (e.x func(sting) int) // if method is set all other type parameters besides the pointer are ignored. Function *FunctionType // MapType is used if the type is a map. MapType *struct { Key Type Value Type } // Struct is used for inline struct types // e.x // var a struct{} Struct *StructType // RawType is used to specify complex types (e.x map[string]*test.SomeStruct) // if the raw type is not nil all the other parameters will be ignored. RawType *jen.Statement // Pointer tells if the type is a pointer. Pointer bool // ArrayType tells if the type is an array. ArrayType *Type // Variadic tells if the type is used for variadic functions Variadic bool // Qualifier specifies the qualifier, for simple types like `string` it is the only // parameter set on the type. Qualifier string }
Type represents a type e.x `string`, `context.Context`... the type is represented by 2 main parameters.
The Import e.x `context`
Import = &Import { Path: "context" }
The Qualifier e.x `Context` Qualifier = "Context"
this would give you the representation of `context.Context`.
func NewRawType ¶
NewRawType creates a new type with raw jen statement.
func NewType ¶
func NewType(qualifier string, options ...TypeOptions) Type
NewType creates the type with the qualifier and options given.
Options are used so we can create a simple type like `string` and complex types ¶
Code has some build in type options that can be used
- ImportTypeOption(i *Import) TypeOptions
- FunctionTypeOption(m *FunctionType) TypeOptions
- PointerTypeOption() TypeOptions
func (Type) AddDocs ¶
AddDocs does nothing for the type code. We only implement this so we implement the Code interface.
func (Type) ImportAliases ¶ added in v0.1.7
func (t Type) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the type.
type TypeOptions ¶
type TypeOptions func(t *Type)
TypeOptions is used when you call NewType, it is a handy way to allow multiple configurations for a type.
tp:= NewType("string", PointerTypeOption())
This would give you a type of pointer string. You can use many build in type option functions like:
- ImportTypeOption(i *Import) TypeOptions
- FunctionTypeOption(m *FunctionType) TypeOptions
- PointerTypeOption() TypeOptions
func ArrayTypeOption ¶ added in v0.1.6
func ArrayTypeOption(tp Type) TypeOptions
ArrayTypeOption marks the type as variadic.
func FunctionTypeOption ¶
func FunctionTypeOption(m *FunctionType) TypeOptions
FunctionTypeOption adds the given function type to the type.
func ImportTypeOption ¶
func ImportTypeOption(i Import) TypeOptions
ImportTypeOption adds the given import to the type.
func MapTypeOption ¶ added in v0.1.6
func MapTypeOption(key Type, value Type) TypeOptions
MapTypeOption sets the map type.
func PointerTypeOption ¶
func PointerTypeOption() TypeOptions
PointerTypeOption marks the type as a pointer type.
func StructTypeOption ¶ added in v0.1.6
func StructTypeOption(st StructType) TypeOptions
StructTypeOption sets the map type.
func VariadicTypeOption ¶
func VariadicTypeOption() TypeOptions
VariadicTypeOption marks the type as variadic.
type Var ¶
type Var struct { // Name is the name of the variable (e.x var {name} string) Name string // Type is the type of the variable. Type Type // The value of the variable (e.x 2, 2.9, "Some string"). // currently only supports literal values, the plan is to expend this. Value interface{} // contains filtered or unexported fields }
Var represents a variable.
func NewVar ¶
NewVar creates a new var with the given name and type, there is also an optional list of documentation comments that you can add to the variable
func NewVarWithValue ¶
NewVarWithValue creates a new var with the given name type and value.
func (*Var) ImportAliases ¶ added in v0.1.7
func (v *Var) ImportAliases() []ImportAlias
ImportAliases returns the import aliases of the variable.