gowrtr: github.com/moznion/gowrtr/generator Index | Examples | Files

package generator

import "github.com/moznion/gowrtr/generator"

Index

Examples

Package 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 AnonymousFunc Uses

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

AnonymousFunc represents a code generator for anonymous func.

func NewAnonymousFunc Uses

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 Uses

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 Uses

func (ifg *AnonymousFunc) Generate(indentLevel int) (string, error)

Generate generates an anonymous func as golang code.

Code:

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)

func (*AnonymousFunc) Invocation Uses

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 Uses

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 Uses

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

AnonymousFuncSignature represents a code generator for signature of anonymous func.

func NewAnonymousFuncSignature Uses

func NewAnonymousFuncSignature() *AnonymousFuncSignature

NewAnonymousFuncSignature returns a new `AnonymousFuncSignature`.

func (*AnonymousFuncSignature) AddParameters Uses

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 Uses

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 Uses

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

Generate generates a signature of the anonymous func as golang code.

Code:

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)

func (*AnonymousFuncSignature) Parameters Uses

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 Uses

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 Uses

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 Uses

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

NewCase creates a new `Case`.

func (*Case) AddStatements Uses

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 Uses

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

Generate generates `case` statement as golang code.

Code:

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)

func (*Case) Statements Uses

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 Uses

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 Uses

func NewCodeBlock(statements ...Statement) *CodeBlock

NewCodeBlock returns a new `CodeBlock`.

func (*CodeBlock) AddStatements Uses

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 Uses

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

Generate generates plain code block as golang code.

Code:

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)

func (*CodeBlock) Statements Uses

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 Uses

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

Comment represents a code generator for one line comment.

func NewComment Uses

func NewComment(comment string) *Comment

NewComment returns a new `Comment`.

func NewCommentf Uses

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 Uses

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

Generate generates one line comment statement.

Code:

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

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

type CompositeLiteral Uses

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 Uses

func NewCompositeLiteral(typ string) *CompositeLiteral

NewCompositeLiteral returns a new `CompositeLiteral`.

func (*CompositeLiteral) AddField Uses

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 Uses

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 Uses

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 Uses

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

Generate generates composite literal block as golang code.

type DefaultCase Uses

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

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

func NewDefaultCase Uses

func NewDefaultCase(statements ...Statement) *DefaultCase

NewDefaultCase returns a new `DefaultCase`.

func (*DefaultCase) AddStatements Uses

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 Uses

func (d *DefaultCase) Generate(indentLevel int) (string, error)

Generate generates `default` block as golang code.

Code:

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)

func (*DefaultCase) Statements Uses

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 Uses

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

Else represents a code generator for `else` block.

func NewElse Uses

func NewElse(statements ...Statement) *Else

NewElse returns a new `Else`.

func (*Else) AddStatements Uses

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 Uses

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

Generate generates `else` block as golang code.

Code:

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)

func (*Else) Statements Uses

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 Uses

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

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

func NewElseIf Uses

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

NewElseIf returns a new `ElseIf`.

func (*ElseIf) AddStatements Uses

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 Uses

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

Generate generates `else-if` block as golang code.

Code:

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)

func (*ElseIf) Statements Uses

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 Uses

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

For represents a code generator for `for` block.

func NewFor Uses

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

NewFor returns a new `For`.

func (*For) AddStatements Uses

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 Uses

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

Generate generates a `for` block as golang code.

Code:

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)

func (*For) Statements Uses

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 Uses

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

Func represents a code generator for the func.

func NewFunc Uses

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

NewFunc returns a new `Func`.

func (*Func) AddStatements Uses

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 Uses

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

Generate generates a func block as golang code.

Code:

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)

func (*Func) Statements Uses

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 Uses

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

FuncInvocation represents a code generator for func invocation.

func NewFuncInvocation Uses

func NewFuncInvocation(parameters ...string) *FuncInvocation

NewFuncInvocation returns a new `FuncInvocation`.

func (*FuncInvocation) AddParameters Uses

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 Uses

func (fig *FuncInvocation) Generate(indentLevel int) (string, error)

Generate generates the func invocation as golang code.

Code:

generator := NewFuncInvocation("foo").AddParameters("bar")

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

func (*FuncInvocation) Parameters Uses

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 Uses

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

FuncParameter represents a parameter of the func.

func NewFuncParameter Uses

func NewFuncParameter(name string, typ string) *FuncParameter

NewFuncParameter returns a new `FuncSignature`.

type FuncReceiver Uses

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

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

func NewFuncReceiver Uses

func NewFuncReceiver(name string, typ string) *FuncReceiver

NewFuncReceiver returns a new `FuncReceiver`.

func (*FuncReceiver) Generate Uses

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

Generate generates a receiver of the func as golang code.

Code:

funcReceiver := NewFuncReceiver("f", "*Foo")

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

type FuncReturnType Uses

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

FuncReturnType represents a return type of the func.

func NewFuncReturnType Uses

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 Uses

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

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

type FuncSignature Uses

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

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

func NewFuncSignature Uses

func NewFuncSignature(funcName string) *FuncSignature

NewFuncSignature returns a new `FuncSignature`.

func (*FuncSignature) AddParameters Uses

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) AddReturnTypeStructs Uses

func (f *FuncSignature) AddReturnTypeStructs(returnTypes ...*FuncReturnType) *FuncSignature

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

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 `AddReturnTypeStructs()` 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 Uses

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

Generate generates a signature of the func as golang code.

Code:

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)

func (*FuncSignature) Parameters Uses

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) ReturnTypeStructs Uses

func (f *FuncSignature) ReturnTypeStructs(returnTypes ...*FuncReturnType) *FuncSignature

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

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 `ReturnTypeStructs()` 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 Uses

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

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

func NewIf Uses

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

NewIf returns a new `If`.

func (*If) AddElseIf Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

Generate generates `if` block as golang code.

Code:

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)

func (*If) Statements Uses

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 Uses

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

Import represents a code generator for `import` statement.

func NewImport Uses

func NewImport(names ...string) *Import

NewImport returns a new `Import`.

func (*Import) AddImports Uses

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 Uses

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

Generate generates `import` statement as golang code.

Code:

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

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

func (*Import) Imports Uses

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 Uses

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

Interface represents a code generator for `interface` block.

func NewInterface Uses

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

NewInterface returns a new `Interface`.

func (*Interface) AddSignatures Uses

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 Uses

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

Generate generates `interface` block as golang code.

Code:

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)

func (*Interface) Signatures Uses

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 Uses

type Newline struct {
}

Newline represents a code generator for newline character.

func NewNewline Uses

func NewNewline() *Newline

NewNewline returns a new `Newline`.

func (*Newline) Generate Uses

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

Generate generates a newline statement as golang code.

Code:

generator := NewNewline()

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

type Package Uses

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

Package represents a code generator for `package` statement.

func NewPackage Uses

func NewPackage(packageName string) *Package

NewPackage returns a new `Package`.

func (*Package) Generate Uses

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

Generate generates a package statement.

Code:

generator := NewPackage("mypkg")

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

type RawStatement Uses

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

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

func NewRawStatement Uses

func NewRawStatement(stmt string) *RawStatement

NewRawStatement returns a new `RawStatement`.

func NewRawStatementf Uses

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 Uses

func (r *RawStatement) Generate(indentLevel int) (string, error)

Generate generates a raw statement.

Code:

generator := NewRawStatement("i := 1 + 1")
generated, err := generator.Generate(0)
if err != nil {
    log.Fatal(err)
}
fmt.Println(generated)

func (*RawStatement) WithNewline Uses

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 Uses

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

ReturnStatement represents a code generator for `return` statement.

func NewReturnStatement Uses

func NewReturnStatement(returnItems ...string) *ReturnStatement

NewReturnStatement returns a new `ReturnStatement`.

func (*ReturnStatement) AddReturnItems Uses

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 Uses

func (r *ReturnStatement) Generate(indentLevel int) (string, error)

Generate generates `return` statement as golang code.

Code:

generator := NewReturnStatement("foo")
generator = generator.AddReturnItems("err")

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

func (*ReturnStatement) ReturnItems Uses

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 Uses

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

Root is a code generator for the entry point.

func NewRoot Uses

func NewRoot(statements ...Statement) *Root

NewRoot generates a new `Root`.

func (*Root) AddStatements Uses

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 Uses

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 Uses

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

Generate generates golang code according to registered statements.

Code:

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)

func (*Root) Gofmt Uses

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 Uses

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 Uses

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 Uses

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

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

type Struct Uses

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

Struct represents a code generator for `struct` notation.

func NewStruct Uses

func NewStruct(name string) *Struct

NewStruct returns a new `Struct`.

func (*Struct) AddField Uses

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 Uses

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

Generate generates `struct` block as golang code.

Code:

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)

type StructField Uses

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

StructField represents a field of the struct.

type Switch Uses

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 Uses

func NewSwitch(condition string) *Switch

NewSwitch returns a new `Switch`.

func (*Switch) AddCase Uses

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 Uses

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 Uses

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 Uses

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

Generate generates `switch` statement as golang code.

Code:

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)

Package generator imports 7 packages (graph) and is imported by 1 packages. Updated 2019-04-10. Refresh now. Tools for package owners.