generator

package
Version: v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2022 License: MIT Imports: 7 Imported by: 8

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildIndent added in v1.5.0

func BuildIndent(indentLevel int) string

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 NewCase added in v1.0.0

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

NewCase creates a new `Case`.

func (*Case) AddStatements added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 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

func NewCodeBlock(statements ...Statement) *CodeBlock

NewCodeBlock returns a new `CodeBlock`.

func (*CodeBlock) AddStatements added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 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

func NewComment(comment string) *Comment

NewComment returns a new `Comment`.

func NewCommentf added in v1.1.0

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 added in v1.0.0

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 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.

func (*CompositeLiteral) Generate added in v1.2.0

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

Generate generates composite literal block as golang code.

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 NewElse added in v1.0.0

func NewElse(statements ...Statement) *Else

NewElse returns a new `Else`.

func (*Else) AddStatements added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

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

func NewElseIf added in v1.0.0

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

NewElseIf returns a new `ElseIf`.

func (*ElseIf) AddStatements added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

For represents a code generator for `for` block.

func NewFor added in v1.0.0

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

NewFor returns a new `For`.

func (*For) AddStatements added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 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

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

FuncInvocation represents a code generator for func invocation. FIXME add function name and type-parameters, this would be a breaking change.

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:

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 (*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:

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 (*FuncReturnType) Generate added in v1.4.0

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

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

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([]*TypeParameter{
	NewTypeParameter("T", "string"),
}).AddParameters(
	NewFuncParameter("foo", "T"),
	NewFuncParameter("bar", "int"),
).AddReturnTypes("T", "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 NewIf added in v1.0.0

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

NewIf returns a new `If`.

func (*If) AddElseIf added in v1.1.0

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 added in v1.0.0

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 added in v1.1.0

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 added in v1.1.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

Import represents a code generator for `import` statement.

func NewImport added in v1.0.0

func NewImport(names ...string) *Import

NewImport returns a new `Import`.

func (*Import) AddImports added in v1.0.0

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 added in v1.0.0

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 added in v1.1.0

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 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

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 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.

type Newline added in v1.0.0

type Newline struct {
}

Newline represents a code generator for newline character.

func NewNewline added in v1.0.0

func NewNewline() *Newline

NewNewline returns a new `Newline`.

func (*Newline) Generate added in v1.0.0

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 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

func NewPackage(packageName string) *Package

NewPackage returns a new `Package`.

func (*Package) Generate added in v1.0.0

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 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 NewRoot added in v1.0.0

func NewRoot(statements ...Statement) *Root

NewRoot generates a new `Root`.

func (*Root) AddStatements added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

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 NewStruct added in v1.0.0

func NewStruct(name string) *Struct

NewStruct returns a new `Struct`.

func (*Struct) AddField added in v1.0.0

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 added in v1.0.0

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

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 NewSwitch added in v1.0.0

func NewSwitch(condition string) *Switch

NewSwitch returns a new `Switch`.

func (*Switch) AddCase added in v1.1.0

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 added in v1.1.0

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 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

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:

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.

type TypeParameters added in v1.6.0

type TypeParameters []*TypeParameter

TypeParameters is an array of TypeParameter pointers for go generics.

func (TypeParameters) Generate added in v1.6.0

func (tps TypeParameters) Generate(indentLevel int) (string, error)

Generate generates the type-parameters as golang code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL