Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildIndent

func BuildIndent(indentLevel int) string

BuildIndent returns indent block (i.e. \t characters) according to given level.

Types

type AnonymousFunc

type AnonymousFunc struct {
	// contains filtered or unexported fields
}

AnonymousFunc represents a code generator for anonymous func.

func NewAnonymousFunc

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

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

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

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

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

type AnonymousFuncSignature struct {
	// contains filtered or unexported fields
}

AnonymousFuncSignature represents a code generator for signature of anonymous func.

func NewAnonymousFuncSignature

func NewAnonymousFuncSignature() *AnonymousFuncSignature

NewAnonymousFuncSignature returns a new `AnonymousFuncSignature`.

func (*AnonymousFuncSignature) AddParameters

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

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

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

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

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

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 NewCase

func NewCase(condition string, statements ...Statement) *Case

NewCase creates a new `Case`.

func (*Case) AddStatements

func (c *Case) AddStatements(statements ...Statement) *Case

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

func (c *Case) Generate(indentLevel int) (string, error)

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

func (c *Case) Statements(statements ...Statement) *Case

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

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

func NewCodeBlock(statements ...Statement) *CodeBlock

NewCodeBlock returns a new `CodeBlock`.

func (*CodeBlock) AddStatements

func (c *CodeBlock) AddStatements(statements ...Statement) *CodeBlock

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

func (c *CodeBlock) Generate(indentLevel int) (string, error)

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

func (c *CodeBlock) Statements(statements ...Statement) *CodeBlock

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

type Comment struct {
	// contains filtered or unexported fields
}

Comment represents a code generator for one line comment.

func NewComment

func NewComment(comment string) *Comment

NewComment returns a new `Comment`.

func NewCommentf

func NewCommentf(comment string, args ...interface{}) *Comment

NewCommentf returns a new `Comment` with formatting. If `args` is not empty, this method formats `stmt` with `args` by `fmt.Sprintf`.

func (*Comment) Generate

func (c *Comment) Generate(indentLevel int) (string, error)

Generate generates one line comment statement.

Example
generator := NewComment("this is one line comment")

generated, err := generator.Generate(0)
if err != nil {
	log.Fatal(err)
}
fmt.Println(generated)
Output:

type CompositeLiteral

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

func NewCompositeLiteral(typ string) *CompositeLiteral

NewCompositeLiteral returns a new `CompositeLiteral`.

func (*CompositeLiteral) AddField

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

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

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.

func (*CompositeLiteral) Generate

func (c *CompositeLiteral) Generate(indentLevel int) (string, error)

Generate generates composite literal block as golang code.

type DefaultCase

type DefaultCase struct {
	// contains filtered or unexported fields
}

DefaultCase represents a code generator for `default` block of `switch-case` notation.

func NewDefaultCase

func NewDefaultCase(statements ...Statement) *DefaultCase

NewDefaultCase returns a new `DefaultCase`.

func (*DefaultCase) AddStatements

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

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

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

type Else struct {
	// contains filtered or unexported fields
}

Else represents a code generator for `else` block.

func NewElse

func NewElse(statements ...Statement) *Else

NewElse returns a new `Else`.

func (*Else) AddStatements

func (e *Else) AddStatements(statements ...Statement) *Else

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

func (e *Else) Generate(indentLevel int) (string, error)

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

func (e *Else) Statements(statements ...Statement) *Else

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

type ElseIf struct {
	// contains filtered or unexported fields
}

ElseIf represents a code generator for `else-if` block.

func NewElseIf

func NewElseIf(condition string, statements ...Statement) *ElseIf

NewElseIf returns a new `ElseIf`.

func (*ElseIf) AddStatements

func (ei *ElseIf) AddStatements(statements ...Statement) *ElseIf

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

func (ei *ElseIf) Generate(indentLevel int) (string, error)

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

func (ei *ElseIf) Statements(statements ...Statement) *ElseIf

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

type For struct {
	// contains filtered or unexported fields
}

For represents a code generator for `for` block.

func NewFor

func NewFor(condition string, statements ...Statement) *For

NewFor returns a new `For`.

func (*For) AddStatements

func (fg *For) AddStatements(statements ...Statement) *For

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

func (fg *For) Generate(indentLevel int) (string, error)

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

func (fg *For) Statements(statements ...Statement) *For

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

type Func struct {
	// contains filtered or unexported fields
}

Func represents a code generator for the func.

func NewFunc

func NewFunc(receiver *FuncReceiver, signature *FuncSignature, statements ...Statement) *Func

NewFunc returns a new `Func`.

func (*Func) AddStatements

func (fg *Func) AddStatements(statements ...Statement) *Func

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

func (fg *Func) Generate(indentLevel int) (string, error)

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

func (fg *Func) Statements(statements ...Statement) *Func

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

type FuncInvocation struct {
	// contains filtered or unexported fields
}

FuncInvocation represents a code generator for func invocation.

func NewFuncInvocation

func NewFuncInvocation(parameters ...string) *FuncInvocation

NewFuncInvocation returns a new `FuncInvocation`.

func (*FuncInvocation) AddParameters

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

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:

func (*FuncInvocation) Parameters

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

type FuncReceiver struct {
	// contains filtered or unexported fields
}

FuncReceiver represents a code generator for the receiver of the func.

func NewFuncReceiver

func NewFuncReceiver(name string, typ string) *FuncReceiver

NewFuncReceiver returns a new `FuncReceiver`.

func (*FuncReceiver) Generate

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:

type FuncReturnType

type FuncReturnType struct {
	// contains filtered or unexported fields
}

FuncReturnType represents a return type of the func.

func NewFuncReturnType

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 (*FuncReturnType) Generate

func (frt *FuncReturnType) Generate(indentLevel int) (string, error)

Generate generates a return type of the func as golang code.

type FuncSignature

type FuncSignature struct {
	// contains filtered or unexported fields
}

FuncSignature represents a code generator for the signature of the func.

func NewFuncSignature

func NewFuncSignature(funcName string) *FuncSignature

NewFuncSignature returns a new `FuncSignature`.

func (*FuncSignature) AddParameters

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

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

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

func (f *FuncSignature) Generate(indentLevel int) (string, error)

Generate generates a signature of the func as golang code.

Example
generator := NewFuncSignature(
	"myFunc",
).AddParameters(
	NewFuncParameter("foo", "string"),
	NewFuncParameter("bar", "int"),
).AddReturnTypes("string", "error")

generated, err := generator.Generate(0)
if err != nil {
	log.Fatal(err)
}
fmt.Println(generated)
Output:

func (*FuncSignature) Parameters

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

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

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.

type If

type If struct {
	// contains filtered or unexported fields
}

If represents a code generator for `if`, `else-if` and `else` block.

func NewIf

func NewIf(condition string, statements ...Statement) *If

NewIf returns a new `If`.

func (*If) AddElseIf

func (ig *If) AddElseIf(blocks ...*ElseIf) *If

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

func (ig *If) AddStatements(statements ...Statement) *If

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

func (ig *If) Else(block *Else) *If

Else sets `else` block to `If`. This method returns a *new* `If`; it means this method acts as immutable.

func (*If) ElseIf

func (ig *If) ElseIf(blocks ...*ElseIf) *If

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

func (ig *If) Generate(indentLevel int) (string, error)

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

func (ig *If) Statements(statements ...Statement) *If

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

type Import struct {
	// contains filtered or unexported fields
}

Import represents a code generator for `import` statement.

func NewImport

func NewImport(names ...string) *Import

NewImport returns a new `Import`.

func (*Import) AddImports

func (ig *Import) AddImports(imps ...string) *Import

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.

func (*Import) Generate

func (ig *Import) Generate(indentLevel int) (string, error)

Generate generates `import` statement as golang code.

Example
generator := NewImport("fmt", "os").
	AddImports("exec", "math")

generated, err := generator.Generate(0)
if err != nil {
	log.Fatal(err)
}
fmt.Println(generated)
Output:

func (*Import) Imports

func (ig *Import) Imports(imps ...string) *Import

Imports sets import items to `Import`. This does *not* add, just set. This method returns a *new* `Import`; it means this method acts as immutable.

type Interface

type Interface struct {
	// contains filtered or unexported fields
}

Interface represents a code generator for `interface` block.

func NewInterface

func NewInterface(name string, funcSignatures ...*FuncSignature) *Interface

NewInterface returns a new `Interface`.

func (*Interface) AddSignatures

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

func (ig *Interface) Generate(indentLevel int) (string, error)

Generate generates `interface` block as golang code.

Example
generator := NewInterface(
	"MyInterface",
	NewFuncSignature("fooFunc").
		AddParameters(NewFuncParameter("foo", "string")).
		AddReturnTypes("string", "error"),
).AddSignatures(
	NewFuncSignature("barFunc"),
)

generated, err := generator.Generate(0)
if err != nil {
	log.Fatal(err)
}
fmt.Println(generated)
Output:

func (*Interface) Signatures

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.

type Newline

type Newline struct {
}

Newline represents a code generator for newline character.

func NewNewline

func NewNewline() *Newline

NewNewline returns a new `Newline`.

func (*Newline) Generate

func (n *Newline) Generate(indentLevel int) (string, error)

Generate generates a newline statement as golang code.

Example
generator := NewNewline()

generated, err := generator.Generate(0)
if err != nil {
	log.Fatal(err)
}
fmt.Println(generated)
Output:

type Package

type Package struct {
	// contains filtered or unexported fields
}

Package represents a code generator for `package` statement.

func NewPackage

func NewPackage(packageName string) *Package

NewPackage returns a new `Package`.

func (*Package) Generate

func (pg *Package) Generate(indentLevel int) (string, error)

Generate generates a package statement.

Example
generator := NewPackage("mypkg")

generated, err := generator.Generate(0)
if err != nil {
	log.Fatal(err)
}
fmt.Println(generated)
Output:

type RawStatement

type RawStatement struct {
	// contains filtered or unexported fields
}

RawStatement represents a code generator for `raw statement`. `raw statement` means plain text statement.

func NewRawStatement

func NewRawStatement(stmt string) *RawStatement

NewRawStatement returns a new `RawStatement`.

func NewRawStatementf

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

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

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

type ReturnStatement struct {
	// contains filtered or unexported fields
}

ReturnStatement represents a code generator for `return` statement.

func NewReturnStatement

func NewReturnStatement(returnItems ...string) *ReturnStatement

NewReturnStatement returns a new `ReturnStatement`.

func (*ReturnStatement) AddReturnItems

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

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

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

type Root struct {
	// contains filtered or unexported fields
}

Root is a code generator for the entry point.

func NewRoot

func NewRoot(statements ...Statement) *Root

NewRoot generates a new `Root`.

func (*Root) AddStatements

func (g *Root) AddStatements(statements ...Statement) *Root

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

func (g *Root) EnableSyntaxChecking() *Root

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

func (g *Root) Generate(indentLevel int) (string, error)

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

func (g *Root) Gofmt(gofmtOptions ...string) *Root

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

func (g *Root) Goimports() *Root

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

func (g *Root) Statements(statements ...Statement) *Root

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

type Statement interface {
	Generate(indentLevel int) (string, error)
}

Statement is an interface that has a responsibility to generate the golang code.

type Struct

type Struct struct {
	// contains filtered or unexported fields
}

Struct represents a code generator for `struct` notation.

func NewStruct

func NewStruct(name string) *Struct

NewStruct returns a new `Struct`.

func (*Struct) AddField

func (sg *Struct) AddField(name string, typ string, tag ...string) *Struct

AddField adds a struct field to `Struct`. This method returns a *new* `Struct`; it means this method acts as immutable.

func (*Struct) Generate

func (sg *Struct) Generate(indentLevel int) (string, error)

Generate generates `struct` block as golang code.

Example
generator := NewStruct("MyStruct")
generator = generator.
	AddField("foo", "string").
	AddField("bar", "int64", `custom:"tag"`)

generated, err := generator.Generate(0)
if err != nil {
	log.Fatal(err)
}
fmt.Println(generated)
Output:

type StructField

type StructField struct {
	// contains filtered or unexported fields
}

StructField represents a field of the struct.

type Switch

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 NewSwitch

func NewSwitch(condition string) *Switch

NewSwitch returns a new `Switch`.

func (*Switch) AddCase

func (s *Switch) AddCase(statements ...*Case) *Switch

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

func (s *Switch) Case(statements ...*Case) *Switch

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

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

func (s *Switch) Generate(indentLevel int) (string, error)

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: