Documentation ¶
Index ¶
- func BuildIndent(indentLevel int) string
- type AnonymousFunc
- func (ifg *AnonymousFunc) AddStatements(statements ...Statement) *AnonymousFunc
- func (ifg *AnonymousFunc) Generate(indentLevel int) (string, error)
- func (ifg *AnonymousFunc) Invocation(funcInvocation *FuncInvocation) *AnonymousFunc
- func (ifg *AnonymousFunc) Statements(statements ...Statement) *AnonymousFunc
- type AnonymousFuncSignature
- func (f *AnonymousFuncSignature) AddParameters(funcParameters ...*FuncParameter) *AnonymousFuncSignature
- func (f *AnonymousFuncSignature) AddReturnTypes(returnTypes ...string) *AnonymousFuncSignature
- func (f *AnonymousFuncSignature) Generate(indentLevel int) (string, error)
- func (f *AnonymousFuncSignature) Parameters(funcParameters ...*FuncParameter) *AnonymousFuncSignature
- func (f *AnonymousFuncSignature) ReturnTypes(returnTypes ...string) *AnonymousFuncSignature
- type Case
- type CodeBlock
- type Comment
- type CompositeLiteral
- func (c *CompositeLiteral) AddField(key string, value Statement) *CompositeLiteral
- func (c *CompositeLiteral) AddFieldRaw(key string, value interface{}) *CompositeLiteral
- func (c *CompositeLiteral) AddFieldStr(key string, value string) *CompositeLiteral
- func (c *CompositeLiteral) Generate(indentLevel int) (string, error)
- type DefaultCase
- type Else
- type ElseIf
- type For
- type Func
- type FuncInvocation
- func (fig *FuncInvocation) AddParameters(parameters ...string) *FuncInvocation
- func (fig *FuncInvocation) Generate(indentLevel int) (string, error)
- func (fig *FuncInvocation) GenericsTypes(typeNames TypeArguments) *FuncInvocation
- func (fig *FuncInvocation) Parameters(parameters ...string) *FuncInvocation
- type FuncParameter
- type FuncReceiver
- type FuncReturnType
- type FuncSignature
- func (f *FuncSignature) AddParameters(funcParameters ...*FuncParameter) *FuncSignature
- func (f *FuncSignature) AddReturnTypeStatements(returnTypes ...*FuncReturnType) *FuncSignature
- func (f *FuncSignature) AddReturnTypes(returnTypes ...string) *FuncSignature
- func (f *FuncSignature) Generate(indentLevel int) (string, error)
- func (f *FuncSignature) Parameters(funcParameters ...*FuncParameter) *FuncSignature
- func (f *FuncSignature) ReturnTypeStatements(returnTypes ...*FuncReturnType) *FuncSignature
- func (f *FuncSignature) ReturnTypes(returnTypes ...string) *FuncSignature
- func (f *FuncSignature) TypeParameters(typeParameters TypeParameters) *FuncSignature
- type If
- type Import
- type Interface
- type Newline
- type Package
- type RawStatement
- type ReturnStatement
- type Root
- func (g *Root) AddStatements(statements ...Statement) *Root
- func (g *Root) EnableSyntaxChecking() *Root
- func (g *Root) Generate(indentLevel int) (string, error)
- func (g *Root) Gofmt(gofmtOptions ...string) *Root
- func (g *Root) Goimports() *Root
- func (g *Root) Statements(statements ...Statement) *Root
- type Statement
- type Struct
- type StructField
- type Switch
- type TypeArguments
- type TypeParameter
- type TypeParameterNames
- type TypeParameters
Examples ¶
- AnonymousFunc.Generate
- AnonymousFuncSignature.Generate
- Case.Generate
- CodeBlock.Generate
- Comment.Generate
- DefaultCase.Generate
- Else.Generate
- ElseIf.Generate
- For.Generate
- Func.Generate
- FuncInvocation.Generate
- FuncInvocation.Generate (WithGenericsTypes)
- FuncReceiver.Generate
- FuncReceiver.Generate (WithGenericsTypeParameterNames)
- FuncSignature.Generate
- If.Generate
- Import.Generate
- Interface.Generate
- Newline.Generate
- Package.Generate
- RawStatement.Generate
- ReturnStatement.Generate
- Root.Generate
- Struct.Generate
- Switch.Generate
- TypeArguments.Generate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildIndent ¶ added in v1.5.0
BuildIndent returns indent block (i.e. \t characters) according to given level.
Types ¶
type AnonymousFunc ¶ added in v1.0.0
type AnonymousFunc struct {
// contains filtered or unexported fields
}
AnonymousFunc represents a code generator for anonymous func.
func NewAnonymousFunc ¶ added in v1.0.0
func NewAnonymousFunc(goFunc bool, signature *AnonymousFuncSignature, statements ...Statement) *AnonymousFunc
NewAnonymousFunc returns a new `AnonymousFunc`. If `goFunc` is true, the anonymous function will be `go func`.
func (*AnonymousFunc) AddStatements ¶ added in v1.0.0
func (ifg *AnonymousFunc) AddStatements(statements ...Statement) *AnonymousFunc
AddStatements adds statements for the function to `AnonymousFunc`. This does *not* set, just add. This method returns a *new* `AnonymousFunc`; it means this method acts as immutable.
func (*AnonymousFunc) Generate ¶ added in v1.0.0
func (ifg *AnonymousFunc) Generate(indentLevel int) (string, error)
Generate generates an anonymous func as golang code.
Example ¶
generator := NewAnonymousFunc( true, NewAnonymousFuncSignature(). AddParameters( NewFuncParameter("foo", "string"), NewFuncParameter("bar", "int64"), ). AddReturnTypes("string", "error"), NewComment(" do something"), NewRawStatement(`fmt.Printf("%d", i)`), ).Invocation(NewFuncInvocation("foo", "bar")) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*AnonymousFunc) Invocation ¶ added in v1.1.0
func (ifg *AnonymousFunc) Invocation(funcInvocation *FuncInvocation) *AnonymousFunc
Invocation sets an invocation of the anonymous func to `AnonymousFunc`. This method returns a *new* `AnonymousFunc`; it means this method acts as immutable.
func (*AnonymousFunc) Statements ¶ added in v1.0.0
func (ifg *AnonymousFunc) Statements(statements ...Statement) *AnonymousFunc
Statements sets statements for the function to `AnonymousFunc`. This does *not* add, just set. This method returns a *new* `AnonymousFunc`; it means this method acts as immutable.
type AnonymousFuncSignature ¶ added in v1.0.0
type AnonymousFuncSignature struct {
// contains filtered or unexported fields
}
AnonymousFuncSignature represents a code generator for signature of anonymous func.
func NewAnonymousFuncSignature ¶ added in v1.0.0
func NewAnonymousFuncSignature() *AnonymousFuncSignature
NewAnonymousFuncSignature returns a new `AnonymousFuncSignature`.
func (*AnonymousFuncSignature) AddParameters ¶ added in v1.1.0
func (f *AnonymousFuncSignature) AddParameters(funcParameters ...*FuncParameter) *AnonymousFuncSignature
AddParameters adds parameters of function to `AnonymousFuncSignature`. This does "not" set, just add. This method returns a *new* `AnonymousFuncSignature`; it means this method acts as immutable.
func (*AnonymousFuncSignature) AddReturnTypes ¶ added in v1.0.0
func (f *AnonymousFuncSignature) AddReturnTypes(returnTypes ...string) *AnonymousFuncSignature
AddReturnTypes adds return types of the function to `AnonymousFuncSignature`. This does "not" set, just add. This method returns a *new* `AnonymousFuncSignature`; it means this method acts as immutable.
func (*AnonymousFuncSignature) Generate ¶ added in v1.0.0
func (f *AnonymousFuncSignature) Generate(indentLevel int) (string, error)
Generate generates a signature of the anonymous func as golang code.
Example ¶
generator := NewAnonymousFuncSignature(). AddParameters( NewFuncParameter("foo", "string"), NewFuncParameter("bar", "int64"), ). AddReturnTypes("string", "error") generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*AnonymousFuncSignature) Parameters ¶ added in v1.1.0
func (f *AnonymousFuncSignature) Parameters(funcParameters ...*FuncParameter) *AnonymousFuncSignature
Parameters sets parameters of function to `AnonymousFuncSignature`. This does "not" add, just set. This method returns a *new* `AnonymousFuncSignature`; it means this method acts as immutable.
func (*AnonymousFuncSignature) ReturnTypes ¶ added in v1.0.0
func (f *AnonymousFuncSignature) ReturnTypes(returnTypes ...string) *AnonymousFuncSignature
ReturnTypes sets return types of the function to `AnonymousFuncSignature`. This does "not" add, just set. This method returns a *new* `AnonymousFuncSignature`; it means this method acts as immutable.
type Case ¶ added in v1.0.0
type Case struct {
// contains filtered or unexported fields
}
Case represents a code generator for `case` statement. See also: https://tour.golang.org/flowcontrol/9
func (*Case) AddStatements ¶ added in v1.0.0
AddStatements adds statements to `Case`. This does *not* set, just add. This method returns a *new* `Case`; it means this method acts as immutable.
func (*Case) Generate ¶ added in v1.0.0
Generate generates `case` statement as golang code.
Example ¶
generator := NewCase(`"foo"`, NewComment(" this is foo")). AddStatements(NewRawStatement(`fmt.Printf("this is foo\n")`)) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*Case) Statements ¶ added in v1.0.0
Statements sets statements to `Case`. This does *not* add, just set. This method returns a *new* `Case`; it means this method acts as immutable.
type CodeBlock ¶ added in v1.0.0
type CodeBlock struct {
// contains filtered or unexported fields
}
CodeBlock represents a code generator for plain code block.
example:
{ // do something fmt.Println("blah blah") }
func NewCodeBlock ¶ added in v1.0.0
NewCodeBlock returns a new `CodeBlock`.
func (*CodeBlock) AddStatements ¶ added in v1.0.0
AddStatements adds statements to `CodeBlock`. This does *not* set, just add. This method returns a *new* `CodeBlock`; it means this method acts as immutable.
func (*CodeBlock) Generate ¶ added in v1.0.0
Generate generates plain code block as golang code.
Example ¶
generator := NewCodeBlock(NewComment(" do something")) generator = generator.AddStatements(NewRawStatement(`fmt.Printf("code block\n")`)) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*CodeBlock) Statements ¶ added in v1.0.0
Statements sets statements to `CodeBlock`. This does *not* add, just set. This method returns a *new* `CodeBlock`; it means this method acts as immutable.
type Comment ¶ added in v1.0.0
type Comment struct {
// contains filtered or unexported fields
}
Comment represents a code generator for one line comment.
func NewComment ¶ added in v1.0.0
NewComment returns a new `Comment`.
func NewCommentf ¶ added in v1.1.0
NewCommentf returns a new `Comment` with formatting. If `args` is not empty, this method formats `stmt` with `args` by `fmt.Sprintf`.
type CompositeLiteral ¶ added in v1.2.0
type CompositeLiteral struct {
// contains filtered or unexported fields
}
CompositeLiteral represents a code generator for composite literal. Please see also: https://golang.org/doc/effective_go.html#composite_literals
func NewCompositeLiteral ¶ added in v1.2.0
func NewCompositeLiteral(typ string) *CompositeLiteral
NewCompositeLiteral returns a new `CompositeLiteral`.
func (*CompositeLiteral) AddField ¶ added in v1.2.0
func (c *CompositeLiteral) AddField(key string, value Statement) *CompositeLiteral
AddField adds a field as `Statement` to `ComposeLiteral`. This method returns a *new* `Struct`; it means this method acts as immutable.
func (*CompositeLiteral) AddFieldRaw ¶ added in v1.2.0
func (c *CompositeLiteral) AddFieldRaw(key string, value interface{}) *CompositeLiteral
AddFieldRaw adds a field as raw text to `ComposeLiteral`. This method returns a *new* `Struct`; it means this method acts as immutable.
func (*CompositeLiteral) AddFieldStr ¶ added in v1.2.0
func (c *CompositeLiteral) AddFieldStr(key string, value string) *CompositeLiteral
AddFieldStr adds a field as string to `ComposeLiteral`. This method returns a *new* `Struct`; it means this method acts as immutable.
type DefaultCase ¶ added in v1.0.0
type DefaultCase struct {
// contains filtered or unexported fields
}
DefaultCase represents a code generator for `default` block of `switch-case` notation.
func NewDefaultCase ¶ added in v1.0.0
func NewDefaultCase(statements ...Statement) *DefaultCase
NewDefaultCase returns a new `DefaultCase`.
func (*DefaultCase) AddStatements ¶ added in v1.0.0
func (d *DefaultCase) AddStatements(statements ...Statement) *DefaultCase
AddStatements adds statements for `default` block to `DefaultCase`. This does *not* set, just add. This method returns a *new* `DefaultCase`; it means this method acts as immutable.
func (*DefaultCase) Generate ¶ added in v1.0.0
func (d *DefaultCase) Generate(indentLevel int) (string, error)
Generate generates `default` block as golang code.
Example ¶
generator := NewDefaultCase( NewComment(" XXX test test"), NewComment(" do something"), ).AddStatements(NewRawStatement(`fmt.Printf("test\n")`)) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*DefaultCase) Statements ¶ added in v1.0.0
func (d *DefaultCase) Statements(statements ...Statement) *DefaultCase
Statements sets statements for `default` block to `DefaultCase`. This does *not* add, just set. This method returns a *new* `DefaultCase`; it means this method acts as immutable.
type Else ¶ added in v1.0.0
type Else struct {
// contains filtered or unexported fields
}
Else represents a code generator for `else` block.
func (*Else) AddStatements ¶ added in v1.0.0
AddStatements adds statements for `else` block to `Else`. This does *not* set, just add. This method returns a *new* `Else`; it means this method acts as immutable.
func (*Else) Generate ¶ added in v1.0.0
Generate generates `else` block as golang code.
Example ¶
generator := NewElse( NewComment(" XXX test test"), NewComment(" do something"), ).AddStatements( NewRawStatement(`fmt.Printf("%d", i)`), ) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*Else) Statements ¶ added in v1.0.0
Statements sets statements for `else` block to `Else`. This does *not* add, just set. This method returns a *new* `Else`; it means this method acts as immutable.
type ElseIf ¶ added in v1.0.0
type ElseIf struct {
// contains filtered or unexported fields
}
ElseIf represents a code generator for `else-if` block.
func (*ElseIf) AddStatements ¶ added in v1.0.0
AddStatements adds statements for the `else-if` block to `ElseIf`. This does *not* set, just add. This method returns a *new* `ElseIf`; it means this method acts as immutable.
func (*ElseIf) Generate ¶ added in v1.0.0
Generate generates `else-if` block as golang code.
Example ¶
generator := NewElseIf("i > 0"). AddStatements( NewComment(" XXX: test test"), NewComment(" do something"), ). AddStatements(NewRawStatement(`fmt.Printf("%d", i)`)) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*ElseIf) Statements ¶ added in v1.0.0
Statements sets statements for the `else-if` block to `ElseIf`. This does *not* add, just set. This method returns a *new* `ElseIf`; it means this method acts as immutable.
type For ¶ added in v1.0.0
type For struct {
// contains filtered or unexported fields
}
For represents a code generator for `for` block.
func (*For) AddStatements ¶ added in v1.0.0
AddStatements adds statements for `for` block to `For`. This does *not* set, just add. This method returns a *new* `For`; it means this method acts as immutable.
func (*For) Generate ¶ added in v1.0.0
Generate generates a `for` block as golang code.
Example ¶
generator := NewFor( "i := 0; i < foo; i++", NewComment(" do something"), ).AddStatements(NewRawStatement(`fmt.Printf("%d", i)`)) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*For) Statements ¶ added in v1.0.0
Statements sets statements for `for` block to `For`. This does *not* add, just set. This method returns a *new* `For`; it means this method acts as immutable.
type Func ¶ added in v1.0.0
type Func struct {
// contains filtered or unexported fields
}
Func represents a code generator for the func.
func NewFunc ¶ added in v1.0.0
func NewFunc(receiver *FuncReceiver, signature *FuncSignature, statements ...Statement) *Func
NewFunc returns a new `Func`.
func (*Func) AddStatements ¶ added in v1.0.0
AddStatements adds statements for the func to `Func`. This does *not* set, just add. This method returns a *new* `Func`; it means this method acts as immutable.
func (*Func) Generate ¶ added in v1.0.0
Generate generates a func block as golang code.
Example ¶
generator := NewFunc( NewFuncReceiver("m", "*MyStruct"), NewFuncSignature("myFunc"). AddParameters( NewFuncParameter("foo", ""), NewFuncParameter("bar", "string"), ). AddReturnTypes("string", "error"), ).AddStatements( NewComment(" do something"), NewNewline(), NewReturnStatement("foo+bar", "nil"), ) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*Func) Statements ¶ added in v1.0.0
Statements sets statements for the func to `Func`. This does *not* add, just set. This method returns a *new* `Func`; it means this method acts as immutable.
type FuncInvocation ¶ added in v1.0.0
type FuncInvocation struct {
// contains filtered or unexported fields
}
FuncInvocation represents a code generator for func invocation.
func NewFuncInvocation ¶ added in v1.0.0
func NewFuncInvocation(parameters ...string) *FuncInvocation
NewFuncInvocation returns a new `FuncInvocation`.
func (*FuncInvocation) AddParameters ¶ added in v1.0.0
func (fig *FuncInvocation) AddParameters(parameters ...string) *FuncInvocation
AddParameters adds parameters of func invocation to `FuncInvocation`. This does *not* set, just add. This method returns a *new* `FuncInvocation`; it means this method acts as immutable.
func (*FuncInvocation) Generate ¶ added in v1.0.0
func (fig *FuncInvocation) Generate(indentLevel int) (string, error)
Generate generates the func invocation as golang code.
Example ¶
generator := NewFuncInvocation("foo").AddParameters("bar") generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
Example (WithGenericsTypes) ¶
generator := NewFuncInvocation("foo").AddParameters("bar").GenericsTypes(TypeArguments{"string", "int64"}) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*FuncInvocation) GenericsTypes ¶ added in v1.7.0
func (fig *FuncInvocation) GenericsTypes(typeNames TypeArguments) *FuncInvocation
GenericsTypes makes a new FuncInvocation value that is based on the receiver value with the given generics type names.
func (*FuncInvocation) Parameters ¶ added in v1.0.0
func (fig *FuncInvocation) Parameters(parameters ...string) *FuncInvocation
Parameters sets parameters of func invocation to `FuncInvocation`. This does *not* add, just set. This method returns a *new* `FuncInvocation`; it means this method acts as immutable.
type FuncParameter ¶
type FuncParameter struct {
// contains filtered or unexported fields
}
FuncParameter represents a parameter of the func.
func NewFuncParameter ¶
func NewFuncParameter(name string, typ string) *FuncParameter
NewFuncParameter returns a new `FuncSignature`.
type FuncReceiver ¶ added in v1.0.0
type FuncReceiver struct {
// contains filtered or unexported fields
}
FuncReceiver represents a code generator for the receiver of the func.
func NewFuncReceiver ¶ added in v1.0.0
func NewFuncReceiver(name string, typ string) *FuncReceiver
NewFuncReceiver returns a new `FuncReceiver`.
func NewFuncReceiverWithGenerics ¶ added in v1.7.0
func NewFuncReceiverWithGenerics(name string, typ string, genericsTypeParameterNames TypeParameterNames) *FuncReceiver
func (*FuncReceiver) Generate ¶ added in v1.0.0
func (f *FuncReceiver) Generate(indentLevel int) (string, error)
Generate generates a receiver of the func as golang code.
Example ¶
funcReceiver := NewFuncReceiver("f", "*Foo") generated, err := funcReceiver.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
Example (WithGenericsTypeParameterNames) ¶
funcReceiver := NewFuncReceiverWithGenerics("f", "*Foo", TypeParameterNames{"T", "U"}) generated, err := funcReceiver.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
type FuncReturnType ¶ added in v1.4.0
type FuncReturnType struct {
// contains filtered or unexported fields
}
FuncReturnType represents a return type of the func.
func NewFuncReturnType ¶ added in v1.4.0
func NewFuncReturnType(typ string, name ...string) *FuncReturnType
NewFuncReturnType returns a new `FuncReturnType`. `name` is an optional parameter. If this parameter is specified, FuncReturnType generates code as named return type.
func NewFuncReturnTypeWithGenerics ¶ added in v1.7.0
func NewFuncReturnTypeWithGenerics(typ string, genericsTypeParameterNames TypeParameterNames, name ...string) *FuncReturnType
NewFuncReturnTypeWithGenerics ret urns a new `FuncReturnType` with the generics type parameter name, e.g. `T`.
type FuncSignature ¶ added in v1.0.0
type FuncSignature struct {
// contains filtered or unexported fields
}
FuncSignature represents a code generator for the signature of the func.
func NewFuncSignature ¶ added in v1.0.0
func NewFuncSignature(funcName string) *FuncSignature
NewFuncSignature returns a new `FuncSignature`.
func (*FuncSignature) AddParameters ¶ added in v1.1.0
func (f *FuncSignature) AddParameters(funcParameters ...*FuncParameter) *FuncSignature
AddParameters adds parameters of the func to `FuncSignature`. This does *not* set, just add. This method returns a *new* `FuncSignature`; it means this method acts as immutable.
func (*FuncSignature) AddReturnTypeStatements ¶ added in v1.4.0
func (f *FuncSignature) AddReturnTypeStatements(returnTypes ...*FuncReturnType) *FuncSignature
AddReturnTypeStatements sets return types of the func to `FuncSignature`. This does *not* add, just set. This method returns a *new* `FuncSignature`; it means this method acts as immutable.
func (*FuncSignature) AddReturnTypes ¶ added in v1.0.0
func (f *FuncSignature) AddReturnTypes(returnTypes ...string) *FuncSignature
AddReturnTypes adds return types of the func to `FuncSignature`. This does *not* set, just add.
This method accepts a return type as `string`. If you want to use the parameter as named one, please consider using `AddReturnTypeStatements()` instead of this (or use this method with string parameter like: `err error`).
This method returns a *new* `FuncSignature`; it means this method acts as immutable.
func (*FuncSignature) Generate ¶ added in v1.0.0
func (f *FuncSignature) Generate(indentLevel int) (string, error)
Generate generates a signature of the func as golang code.
Example ¶
generator := NewFuncSignature( "myFunc", ).TypeParameters(TypeParameters{ NewTypeParameter("T", "string"), }).AddParameters( NewFuncParameter("foo", "T"), NewFuncParameter("bar", "int"), ).AddReturnTypeStatements( NewFuncReturnTypeWithGenerics("MyStruct", TypeParameterNames{"T", "U"}), NewFuncReturnType("error"), ) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*FuncSignature) Parameters ¶ added in v1.1.0
func (f *FuncSignature) Parameters(funcParameters ...*FuncParameter) *FuncSignature
Parameters sets parameters of the func to `FuncSignature`. This does *not* add, just set. This method returns a *new* `FuncSignature`; it means this method acts as immutable.
func (*FuncSignature) ReturnTypeStatements ¶ added in v1.4.0
func (f *FuncSignature) ReturnTypeStatements(returnTypes ...*FuncReturnType) *FuncSignature
ReturnTypeStatements sets return types of the func to `FuncSignature`. This does *not* add, just set. This method returns a *new* `FuncSignature`; it means this method acts as immutable.
func (*FuncSignature) ReturnTypes ¶ added in v1.0.0
func (f *FuncSignature) ReturnTypes(returnTypes ...string) *FuncSignature
ReturnTypes sets return types of the func to `FuncSignature`. This does *not* add, just set.
This method accepts a return type as `string`. If you want to use the parameter as named one, please consider using `ReturnTypeStatements()` instead of this (or use this method with string parameter like: `err error`).
This method returns a *new* `FuncSignature`; it means this method acts as immutable.
func (*FuncSignature) TypeParameters ¶ added in v1.6.0
func (f *FuncSignature) TypeParameters(typeParameters TypeParameters) *FuncSignature
TypeParameters sets the TypeParameters onto the caller FuncSignature.
type If ¶ added in v1.0.0
type If struct {
// contains filtered or unexported fields
}
If represents a code generator for `if`, `else-if` and `else` block.
func (*If) AddElseIf ¶ added in v1.1.0
AddElseIf adds `else-if` block to `If`. This does *not* set, just add. This method returns a *new* `If`; it means this method acts as immutable.
func (*If) AddStatements ¶ added in v1.0.0
AddStatements adds statements for `if` block to `If`. This does *not* set, just add. This method returns a *new* `If`; it means this method acts as immutable.
func (*If) Else ¶ added in v1.1.0
Else sets `else` block to `If`. This method returns a *new* `If`; it means this method acts as immutable.
func (*If) ElseIf ¶ added in v1.1.0
ElseIf sets `else-if` block to `If`. This does *not* add, just set. This method returns a *new* `If`; it means this method acts as immutable.
func (*If) Generate ¶ added in v1.0.0
Generate generates `if` block as golang code.
Example ¶
generator := NewIf("i == 0", NewComment(" if"), ).AddElseIf( NewElseIf("i < 0", NewComment(" else if 1")), nil, NewElseIf("i > 0", NewComment(" else if 2")), ).Else(NewElse( NewComment(" else"), )) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*If) Statements ¶ added in v1.0.0
Statements sets statements for `if` block to `If`. This does *not* add, just set. This method returns a *new* `If`; it means this method acts as immutable.
type Import ¶ added in v1.0.0
type Import struct {
// contains filtered or unexported fields
}
Import represents a code generator for `import` statement.
func (*Import) AddImports ¶ added in v1.0.0
AddImports adds import items to `Import`. This does *not* set, just add. This method returns a *new* `Import`; it means this method acts as immutable.
type Interface ¶ added in v1.0.0
type Interface struct {
// contains filtered or unexported fields
}
Interface represents a code generator for `interface` block.
func NewInterface ¶ added in v1.0.0
func NewInterface(name string, funcSignatures ...*FuncSignature) *Interface
NewInterface returns a new `Interface`.
func (*Interface) AddSignatures ¶ added in v1.1.0
func (ig *Interface) AddSignatures(sig ...*FuncSignature) *Interface
AddSignatures adds signatures of the func to `Interface`. This does *not* set, just add. This method returns a *new* `Interface`; it means this method acts as immutable.
func (*Interface) Generate ¶ added in v1.0.0
Generate generates `interface` block as golang code.
Example ¶
generator := NewInterface( "MyInterface", NewFuncSignature("fooFunc"). AddParameters(NewFuncParameter("foo", "T")). AddReturnTypes("string", "error"), ).AddSignatures( NewFuncSignature("barFunc"), ).TypeParameters(TypeParameters{ NewTypeParameter("T", "any"), }) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*Interface) Signatures ¶ added in v1.1.0
func (ig *Interface) Signatures(sig ...*FuncSignature) *Interface
Signatures sets signatures of the func to `Interface`. This does *not* add, just set. This method returns a *new* `Interface`; it means this method acts as immutable.
func (*Interface) TypeParameters ¶ added in v1.7.0
func (ig *Interface) TypeParameters(typeParameters TypeParameters) *Interface
TypeParameters makes a new Interface value based on the receiver value with the given generics TypeParameters.
type Newline ¶ added in v1.0.0
type Newline struct { }
Newline represents a code generator for newline character.
type Package ¶ added in v1.0.0
type Package struct {
// contains filtered or unexported fields
}
Package represents a code generator for `package` statement.
func NewPackage ¶ added in v1.0.0
NewPackage returns a new `Package`.
type RawStatement ¶ added in v1.0.0
type RawStatement struct {
// contains filtered or unexported fields
}
RawStatement represents a code generator for `raw statement`. `raw statement` means plain text statement.
func NewRawStatement ¶ added in v1.0.0
func NewRawStatement(stmt string) *RawStatement
NewRawStatement returns a new `RawStatement`.
func NewRawStatementf ¶ added in v1.1.0
func NewRawStatementf(stmt string, args ...interface{}) *RawStatement
NewRawStatementf returns a new `RawStatement` with formatting. If `args` is not empty, this method formats `stmt` with `args` by `fmt.Sprintf`.
func (*RawStatement) Generate ¶ added in v1.0.0
func (r *RawStatement) Generate(indentLevel int) (string, error)
Generate generates a raw statement.
Example ¶
generator := NewRawStatement("i := 1 + 1") generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*RawStatement) WithNewline ¶ added in v1.0.0
func (r *RawStatement) WithNewline(with bool) *RawStatement
WithNewline specifies whether append newline or not. Default value is `true`, so this method might be used when you want to suppress to break the line.
type ReturnStatement ¶ added in v1.0.0
type ReturnStatement struct {
// contains filtered or unexported fields
}
ReturnStatement represents a code generator for `return` statement.
func NewReturnStatement ¶ added in v1.0.0
func NewReturnStatement(returnItems ...string) *ReturnStatement
NewReturnStatement returns a new `ReturnStatement`.
func (*ReturnStatement) AddReturnItems ¶ added in v1.0.0
func (r *ReturnStatement) AddReturnItems(returnItems ...string) *ReturnStatement
AddReturnItems adds return items to `ReturnStatement`. This does *not* set, just add. This method returns a *new* `ReturnStatement`; it means this method acts as immutable.
func (*ReturnStatement) Generate ¶ added in v1.0.0
func (r *ReturnStatement) Generate(indentLevel int) (string, error)
Generate generates `return` statement as golang code.
Example ¶
generator := NewReturnStatement("foo") generator = generator.AddReturnItems("err") generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*ReturnStatement) ReturnItems ¶ added in v1.0.0
func (r *ReturnStatement) ReturnItems(returnItems ...string) *ReturnStatement
ReturnItems sets return items to `ReturnStatement`. This does *not* add, just set. This method returns a *new* `ReturnStatement`; it means this method acts as immutable.
type Root ¶ added in v1.0.0
type Root struct {
// contains filtered or unexported fields
}
Root is a code generator for the entry point.
func (*Root) AddStatements ¶ added in v1.0.0
AddStatements adds statements to Root. This does *not* set, just add. This method returns a *new* `Root`; it means this method acts as immutable.
func (*Root) EnableSyntaxChecking ¶ added in v1.0.0
EnableSyntaxChecking enables syntax checking. If this option is enabled, it checks the syntax of the code on code generation phase. This method returns a *new* `Root`; it means this method acts as immutable.
func (*Root) Generate ¶ added in v1.0.0
Generate generates golang code according to registered statements.
Example ¶
myFuncSignature := NewFuncSignature("MyFunc"). AddParameters( NewFuncParameter("foo", "string"), ). AddReturnTypes("string", "error") generator := NewRoot( NewComment(" THIS CODE WAS AUTO GENERATED"), NewNewline(), NewPackage("mypkg"), NewInterface("MyInterface"). AddSignatures(myFuncSignature), NewNewline(), NewStruct("MyStruct"). AddField("Foo", "string"). AddField("Bar", "int64"), NewNewline(), ).AddStatements( NewFunc( NewFuncReceiver("m", "*MyStruct"), NewFuncSignature("MyFunc"). AddParameters( NewFuncParameter("foo", "string"), ). AddReturnTypes("string", "error"), ).AddStatements( NewCodeBlock( NewRawStatement("str := ").WithNewline(false), NewAnonymousFunc( false, NewAnonymousFuncSignature(). AddParameters(NewFuncParameter("bar", "string")). AddReturnTypes("string"), NewReturnStatement("bar"), ).Invocation(NewFuncInvocation("foo")), NewNewline(), NewIf(`str == ""`). AddStatements( NewFor(`i := 0; i < 3; i++`).AddStatements( NewRawStatement(`fmt.Printf("%d\n", i)`), ), ), NewNewline(), NewSwitch("str"). AddCase( NewCase( `""`, NewComment(" empty string"), ), NewCase( `"foo"`, NewComment(" foo string"), ), ). Default( NewDefaultCase(NewComment(" default")), ), NewNewline(), NewReturnStatement("str", "nil"), ), ), ) generated, err := generator. Gofmt("-s"). Goimports(). Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*Root) Gofmt ¶ added in v1.0.0
Gofmt enables `gofmt`. If `gofmt` is enabled, it applies `gofmt` on code generation phase. This method returns a *new* `Root`; it means this method acts as immutable.
func (*Root) Goimports ¶ added in v1.0.0
Goimports enables `goimports`. If `goimports` is enabled, it applies `goimports` on code generation phase. This method returns a *new* `Root`; it means this method acts as immutable.
func (*Root) Statements ¶ added in v1.0.0
Statements sets statements to Root. This does *not* add, just set. This method returns a *new* `Root`; it means this method acts as immutable.
type Statement ¶ added in v1.0.0
Statement is an interface that has a responsibility to generate the golang code.
type Struct ¶ added in v1.0.0
type Struct struct {
// contains filtered or unexported fields
}
Struct represents a code generator for `struct` notation.
func (*Struct) AddField ¶ added in v1.0.0
AddField adds a struct field to `Struct`. This method returns a *new* `Struct`; it means this method acts as immutable.
func (*Struct) Generate ¶ added in v1.0.0
Generate generates `struct` block as golang code.
Example ¶
generator := NewStruct("MyStruct") generator = generator. TypeParameters(TypeParameters{NewTypeParameter("T", "string")}). AddField("foo", "T"). AddField("bar", "int64", `custom:"tag"`) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
func (*Struct) TypeParameters ¶ added in v1.6.0
func (sg *Struct) TypeParameters(typeParameters TypeParameters) *Struct
TypeParameters sets the TypeParameters onto the current caller Struct.
type StructField ¶
type StructField struct {
// contains filtered or unexported fields
}
StructField represents a field of the struct.
type Switch ¶ added in v1.0.0
type Switch struct {
// contains filtered or unexported fields
}
Switch represents a code generator for `switch` statement. See also: https://tour.golang.org/flowcontrol/9
func (*Switch) AddCase ¶ added in v1.1.0
AddCase adds `case` statements to `Switch`. This does *not* set, just add. This method returns a *new* `Switch`; it means this method acts as immutable.
func (*Switch) Case ¶ added in v1.1.0
Case sets `case` statements to `Switch`. This does *not* add, just set. This method returns a *new* `Switch`; it means this method acts as immutable.
func (*Switch) Default ¶ added in v1.1.0
func (s *Switch) Default(statement *DefaultCase) *Switch
Default sets a `default` statement to `Switch`. This method returns a *new* `Switch`; it means this method acts as immutable.
func (*Switch) Generate ¶ added in v1.0.0
Generate generates `switch` statement as golang code.
Example ¶
generator := NewSwitch("str") generator = generator.AddCase( NewCase(`"foo"`, NewRawStatement(`fmt.Printf("str is foo\n")`)), NewCase(`"bar"`, NewRawStatement(`fmt.Printf("str is bar\n")`)), ) generator = generator.Default( NewDefaultCase(NewRawStatement(`fmt.Printf("here is default\n")`)), ) generated, err := generator.Generate(0) if err != nil { log.Fatal(err) } fmt.Println(generated)
Output:
type TypeArguments ¶ added in v1.6.0
type TypeArguments []string
TypeArguments is an array of type argument for go generics.
func (TypeArguments) Generate ¶ added in v1.6.0
func (tas TypeArguments) Generate(indentLevel int) (string, error)
Generate generates the type-arguments as golang code.
Example ¶
generated, err := TypeArguments{"int", "string"}.Generate(0) if err != nil { log.Fatal(err) } fmt.Println("// example: f(T, U)") fmt.Printf("f%s(intVar, strVar)\n", generated)
Output:
type TypeParameter ¶ added in v1.6.0
type TypeParameter struct {
// contains filtered or unexported fields
}
TypeParameter is a "generic" type parameter. e,g. `T string`; T is a parameter and int is type.
func NewTypeParameter ¶ added in v1.6.0
func NewTypeParameter(parameter string, typ string) *TypeParameter
NewTypeParameter returns a new TypeParameter.
func NewTypeParameters ¶ added in v1.7.0
func NewTypeParameters(parameter string, types []string) *TypeParameter
NewTypeParameters returns a new TypeParameter. If the `types` argument has the multiple types, those types become a union type.
type TypeParameterNames ¶ added in v1.7.0
type TypeParameterNames []string
TypeParameterNames is an array of type parameter names (e.g. `T`) for go generics.
type TypeParameters ¶ added in v1.6.0
type TypeParameters []*TypeParameter
TypeParameters is an array of TypeParameter pointers for go generics.
Source Files ¶
- anonymous_func.go
- anonymous_func_signature.go
- case.go
- code_block.go
- comment.go
- composite_literal.go
- default_case.go
- else.go
- else_if.go
- for.go
- frame_fetcher.go
- func.go
- func_invocation.go
- func_receiver.go
- func_signature.go
- if.go
- import.go
- interface.go
- newline.go
- package.go
- raw_statement.go
- return_statement.go
- root.go
- statement.go
- struct.go
- switch.go
- type_parameters.go