codegen

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 15 Imported by: 10

README

Codegen

GoDoc Widget Build Status codecov Go Report Card

Helpers fo generating go codes

Example

package main

import (
	"github.com/go-liewhite/codegen"
)

func main()  {
	file := codegen.NewFile("main", "examples/range-and-close/range-and-close.go")
 
    file.WriteBlock(
        codegen.Func(codegen.Var(codegen.Int, "n"), codegen.Var(codegen.Chan(codegen.Int), "c"), ).Named("fibonacci").Do(
            file.Expr("x, y := 0, 1"),
            codegen.For(file.Expr("i := 0"), file.Expr("i < n"), file.Expr("i++")).Do(
                file.Expr("c <- x"),
                file.Expr("x, y = y, x+y"),
            ),
            codegen.Call("close", codegen.Id("c")),
        ),
        codegen.Func().Named("main").Do(
            codegen.Define(codegen.Id("c")).By(codegen.Call("make", codegen.Chan(codegen.Int), file.Val(10))),
            codegen.Call("fibonacci", codegen.Call("cap", codegen.Id("c")), codegen.Id("c")).AsGo(),
            codegen.ForRange(codegen.Id("c"), "i").Do(
                codegen.Call(file.Use("fmt", "Println"), codegen.Id("i")),
            ),
        ),
    )
    
    file.WriteFile()
}

will generate file examples/range-and-close/range-and-close.go

package main

import (
	fmt "fmt"
)

func fibonacci(n int, c chan int) {
	x, y := 0, 1
	for i := 0; i < n; i++ {
		c <- x
		x, y = y, x+y
	}
	close(c)
}
func main() {
	c := make(chan int, 10)
	go fibonacci(cap(c), c)
	for i := range c {
		fmt.Println(i)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Expr = createExpr(LowerSnakeCase)
View Source
var TypeOf = createTypeOf(LowerSnakeCase)
View Source
var Val = createVal(LowerSnakeCase)

Functions

func GeneratedFileSuffix

func GeneratedFileSuffix(filename string) string

func IsEmptyValue

func IsEmptyValue(rv reflect.Value) bool

func IsGoFile

func IsGoFile(filename string) bool

func IsGoTestFile

func IsGoTestFile(filename string) bool

func IsValidIdent

func IsValidIdent(s string) bool

func LowerCamelCase

func LowerCamelCase(s string) string

func LowerSnakeCase

func LowerSnakeCase(s string) string

func Stringify

func Stringify(snippet Snippet) string

func TryCatch

func TryCatch(f func()) (err error)

func UpperCamelCase

func UpperCamelCase(s string) string

func UpperSnakeCase

func UpperSnakeCase(s string) string

Types

type ArrayType

type ArrayType struct {
	SnippetType
	Elem SnippetType
	Len  int
}

func Array

func Array(tpe SnippetType, len int) *ArrayType

func (*ArrayType) Bytes

func (tpe *ArrayType) Bytes() []byte

type Body

type Body []Snippet

func Block

func Block(bodies ...Snippet) Body

func (Body) Bytes

func (body Body) Bytes() []byte

type BuiltInType

type BuiltInType string
const (
	Bool BuiltInType = "bool"

	Int   BuiltInType = "int"
	Int8  BuiltInType = "int8"
	Int16 BuiltInType = "int16"
	Int32 BuiltInType = "int32"
	Int64 BuiltInType = "int64"

	Uint    BuiltInType = "uint"
	Uint8   BuiltInType = "uint8"
	Uint16  BuiltInType = "uint16"
	Uint32  BuiltInType = "uint32"
	Uint64  BuiltInType = "uint64"
	Uintptr BuiltInType = "uintptr"

	Float32    BuiltInType = "float32"
	Float64    BuiltInType = "float64"
	Complex64  BuiltInType = "complex64"
	Complex128 BuiltInType = "complex128"

	String BuiltInType = "string"
	Byte   BuiltInType = "byte"
	Rune   BuiltInType = "rune"

	Error BuiltInType = "error"
)

func (BuiltInType) Bytes

func (tpe BuiltInType) Bytes() []byte

type ChanType

type ChanType struct {
	SnippetType
	Elem SnippetType
}

func Chan

func Chan(tpe SnippetType) *ChanType

func (*ChanType) Bytes

func (tpe *ChanType) Bytes() []byte

type EllipsisType

type EllipsisType struct {
	SnippetType
	Elem SnippetType
}

func Ellipsis

func Ellipsis(tpe SnippetType) *EllipsisType

func (*EllipsisType) Bytes

func (tpe *EllipsisType) Bytes() []byte

type File

type File struct {
	PkgName string

	bytes.Buffer
	// contains filtered or unexported fields
}

func NewFile

func NewFile(pkgName string, filename string) *File
Example (Hello)
file := NewFile("main", "examples/hello/hello_test.go")

file.WriteBlock(
	Func().Named("main").Do(
		Call(file.Use("fmt", "Println"), file.Val("Hello, 世界")),
	),
)

fmt.Println(string(file.Bytes()))
Output:

package main

import (
	fmt "fmt"
)

func main() {
	fmt.Println("Hello, 世界")
}
Example (Main)
file := NewFile("main", "examples/range-and-close/range-and-close.go")

file.WriteBlock(
	Func(Var(Int, "n"), Var(Chan(Int), "c")).Named("fibonacci").Do(
		Expr("x, y := 0, 1"),
		For(Expr("i := 0"), Expr("i < n"), Expr("i++")).Do(
			file.Expr("c <- x"),
			file.Expr("x, y = y, x+y"),
		),
		Call("close", Id("c")),
	),
	Func().Named("main").Do(
		Define(Id("c")).By(Call("make", Chan(Int), Val(10))),
		Call("fibonacci", Call("cap", Id("c")), Id("c")).AsGo(),
		ForRange(Id("c"), "i").Do(
			Call(file.Use("fmt", "Println"), Id("i")),
		),
	),
)

fmt.Println(string(file.Bytes()))
Output:

package main

import (
	fmt "fmt"
)

func fibonacci(n int, c chan int) {
	x, y := 0, 1
	for i := 0; i < n; i++ {
		c <- x
		x, y = y, x+y
	}
	close(c)
}

func main() {
	c := make(chan int, 10)
	go fibonacci(cap(c), c)
	for i := range c {
		fmt.Println(i)
	}
}

func (*File) Bytes

func (file *File) Bytes() []byte

func (*File) Expr

func (file *File) Expr(f string, args ...interface{}) SnippetExpr

func (*File) TypeOf

func (file *File) TypeOf(tpe reflect.Type) SnippetType

func (*File) Use

func (file *File) Use(importPath string, exposedName string) string

func (*File) Val

func (file *File) Val(v interface{}) Snippet

func (*File) WriteBlock

func (file *File) WriteBlock(ss ...Snippet)

func (*File) WriteFile

func (file *File) WriteFile() (int, error)

type FuncType

type FuncType struct {
	SnippetType
	SnippetCanBeInterfaceMethod
	Name    *SnippetIdent
	Recv    *SnippetField
	Params  []*SnippetField
	Results []*SnippetField
	Body    []Snippet
	// contains filtered or unexported fields
}

func Func

func Func(params ...*SnippetField) *FuncType

func (*FuncType) Bytes

func (f *FuncType) Bytes() []byte

func (FuncType) Do

func (f FuncType) Do(bodies ...Snippet) *FuncType

func (FuncType) MethodOf

func (f FuncType) MethodOf(recv *SnippetField) *FuncType

func (FuncType) Named

func (f FuncType) Named(name string) *FuncType

func (FuncType) Return

func (f FuncType) Return(results ...*SnippetField) *FuncType

type ImportPathAliaser

type ImportPathAliaser func(importPath string) string

type InterfaceType

type InterfaceType struct {
	SnippetType
	Methods []SnippetCanBeInterfaceMethod
}

func Interface

func Interface(methods ...SnippetCanBeInterfaceMethod) *InterfaceType

func (*InterfaceType) Bytes

func (tpe *InterfaceType) Bytes() []byte

type MapType

type MapType struct {
	SnippetType
	Key   SnippetType
	Value SnippetType
}

func Map

func Map(key SnippetType, value SnippetType) *MapType

func (*MapType) Bytes

func (tpe *MapType) Bytes() []byte

type NamedType

func Type

func Type(name string) *NamedType

func (*NamedType) Bytes

func (tpe *NamedType) Bytes() []byte

type SliceType

type SliceType struct {
	SnippetType
	Elem SnippetType
}

func Slice

func Slice(tpe SnippetType) *SliceType

func (*SliceType) Bytes

func (tpe *SliceType) Bytes() []byte

type Snippet

type Snippet interface {
	Bytes() []byte
}

type SnippetAssignStmt

type SnippetAssignStmt struct {
	SnippetSpec
	Token token.Token
	Lhs   []SnippetCanAddr
	Rhs   []Snippet
}

func Assign

func Assign(lhs ...SnippetCanAddr) *SnippetAssignStmt

func AssignWith

func AssignWith(tok token.Token, lhs ...SnippetCanAddr) *SnippetAssignStmt

func Define

func Define(lhs ...SnippetCanAddr) *SnippetAssignStmt

func (SnippetAssignStmt) By

func (stmt SnippetAssignStmt) By(rhs ...Snippet) *SnippetAssignStmt

func (*SnippetAssignStmt) Bytes

func (stmt *SnippetAssignStmt) Bytes() []byte

type SnippetBuiltIn

type SnippetBuiltIn string
const (
	Iota        SnippetBuiltIn = "iota"
	True        SnippetBuiltIn = "true"
	False       SnippetBuiltIn = "false"
	Nil         SnippetBuiltIn = "nil"
	Break       SnippetBuiltIn = "break"
	Continue    SnippetBuiltIn = "continue"
	Fallthrough SnippetBuiltIn = "fallthrough"
)

func (SnippetBuiltIn) Bytes

func (tpe SnippetBuiltIn) Bytes() []byte

type SnippetCallExpr

type SnippetCallExpr struct {
	X        Snippet
	Params   []Snippet
	Ellipsis bool
	Modifier token.Token
}

func Call

func Call(name string, params ...Snippet) *SnippetCallExpr

func CallWith

func CallWith(s Snippet, params ...Snippet) *SnippetCallExpr

func Convert

func Convert(tpe SnippetType, target Snippet) *SnippetCallExpr

func (SnippetCallExpr) AsDefer

func (expr SnippetCallExpr) AsDefer() *SnippetCallExpr

func (SnippetCallExpr) AsGo

func (expr SnippetCallExpr) AsGo() *SnippetCallExpr

func (*SnippetCallExpr) Bytes

func (expr *SnippetCallExpr) Bytes() []byte

func (SnippetCallExpr) WithEllipsis

func (expr SnippetCallExpr) WithEllipsis() *SnippetCallExpr

type SnippetCanAddr

type SnippetCanAddr interface {
	Snippet
	// contains filtered or unexported methods
}

type SnippetCanBeInterfaceMethod

type SnippetCanBeInterfaceMethod interface {
	// contains filtered or unexported methods
}

type SnippetClause

type SnippetClause struct {
	List []Snippet
	Body []Snippet
}

func Clause

func Clause(ss ...Snippet) *SnippetClause

func (*SnippetClause) Bytes

func (stmt *SnippetClause) Bytes() []byte

func (SnippetClause) Do

func (stmt SnippetClause) Do(bodies ...Snippet) *SnippetClause

type SnippetComments

type SnippetComments []string

func Comments

func Comments(lines ...string) SnippetComments

func (SnippetComments) Bytes

func (comments SnippetComments) Bytes() []byte

type SnippetCompositeLit

type SnippetCompositeLit struct {
	Type SnippetType
	Elts []Snippet
}

func Compose

func Compose(tpe SnippetType, elts ...Snippet) *SnippetCompositeLit

func (*SnippetCompositeLit) Bytes

func (lit *SnippetCompositeLit) Bytes() []byte

type SnippetExpr

type SnippetExpr string

func (SnippetExpr) Bytes

func (tpe SnippetExpr) Bytes() []byte

type SnippetField

type SnippetField struct {
	SnippetSpec
	SnippetCanAddr
	Type  SnippetType
	Names []*SnippetIdent
	Tag   string
	Alias bool
	SnippetComments
}

func Var

func Var(tpe SnippetType, names ...string) *SnippetField

func (SnippetField) AsAlias

func (f SnippetField) AsAlias() *SnippetField

func (*SnippetField) Bytes

func (f *SnippetField) Bytes() []byte

func (SnippetField) WithComments

func (f SnippetField) WithComments(comments ...string) *SnippetField

func (SnippetField) WithTag

func (f SnippetField) WithTag(tag string) *SnippetField

func (SnippetField) WithTags

func (f SnippetField) WithTags(tags map[string][]string) *SnippetField

func (SnippetField) WithoutTag

func (f SnippetField) WithoutTag() *SnippetField

type SnippetForStmt

type SnippetForStmt struct {
	Init Snippet
	Cond Snippet
	Post Snippet
	Body []Snippet
}

func For

func For(init Snippet, cond Snippet, post Snippet) *SnippetForStmt

func (*SnippetForStmt) Bytes

func (stmt *SnippetForStmt) Bytes() []byte

func (SnippetForStmt) Do

func (stmt SnippetForStmt) Do(bodies ...Snippet) *SnippetForStmt

type SnippetIdent

type SnippetIdent string

func Id

func Id(n string) *SnippetIdent

func IdsFromNames

func IdsFromNames(names ...string) []*SnippetIdent

func (SnippetIdent) Bytes

func (id SnippetIdent) Bytes() []byte

func (SnippetIdent) LowerCamelCase

func (id SnippetIdent) LowerCamelCase() *SnippetIdent

func (SnippetIdent) LowerSnakeCase

func (id SnippetIdent) LowerSnakeCase() *SnippetIdent

func (SnippetIdent) UpperCamelCase

func (id SnippetIdent) UpperCamelCase() *SnippetIdent

func (SnippetIdent) UpperSnakeCase

func (id SnippetIdent) UpperSnakeCase() *SnippetIdent

type SnippetIfStmt

type SnippetIfStmt struct {
	Init     Snippet
	Cond     Snippet
	Body     []Snippet
	ElseList []*SnippetIfStmt
	AsElse   bool
}

func If

func If(cond Snippet) *SnippetIfStmt

func (*SnippetIfStmt) Bytes

func (stmt *SnippetIfStmt) Bytes() []byte

func (SnippetIfStmt) Do

func (stmt SnippetIfStmt) Do(bodies ...Snippet) *SnippetIfStmt

func (SnippetIfStmt) Else

func (stmt SnippetIfStmt) Else(ifStmt *SnippetIfStmt) *SnippetIfStmt

func (SnippetIfStmt) InitWith

func (stmt SnippetIfStmt) InitWith(init Snippet) *SnippetIfStmt

func (SnippetIfStmt) WithoutInit

func (stmt SnippetIfStmt) WithoutInit() *SnippetIfStmt

type SnippetKeyValueExpr

type SnippetKeyValueExpr struct {
	Snippet
	Key   Snippet
	Value Snippet
}

func KeyValue

func KeyValue(key Snippet, value Snippet) *SnippetKeyValueExpr

func (*SnippetKeyValueExpr) Bytes

func (tpe *SnippetKeyValueExpr) Bytes() []byte

type SnippetLit

type SnippetLit string

func Lit

func Lit(s string) *SnippetLit

func (SnippetLit) Bytes

func (lit SnippetLit) Bytes() []byte

type SnippetParenExpr

type SnippetParenExpr struct {
	SnippetCanAddr
	Elem Snippet
}

func Paren

func Paren(s Snippet) *SnippetParenExpr

func (*SnippetParenExpr) Bytes

func (tpe *SnippetParenExpr) Bytes() []byte

type SnippetRangeStmt

type SnippetRangeStmt struct {
	Key   *SnippetIdent
	Value *SnippetIdent
	X     Snippet
	Body  []Snippet
}

func ForRange

func ForRange(x Snippet, keyAndValue ...string) *SnippetRangeStmt

func (*SnippetRangeStmt) Bytes

func (stmt *SnippetRangeStmt) Bytes() []byte

func (SnippetRangeStmt) Do

func (stmt SnippetRangeStmt) Do(bodies ...Snippet) *SnippetRangeStmt

type SnippetReturnStmt

type SnippetReturnStmt struct {
	Results []Snippet
}

func Return

func Return(snippets ...Snippet) *SnippetReturnStmt

func (*SnippetReturnStmt) Bytes

func (stmt *SnippetReturnStmt) Bytes() []byte

type SnippetSelectStmt

type SnippetSelectStmt struct {
	Clauses []*SnippetClause
}

func Select

func Select(clauses ...*SnippetClause) *SnippetSelectStmt

func (*SnippetSelectStmt) Bytes

func (stmt *SnippetSelectStmt) Bytes() []byte

type SnippetSelectorExpr

type SnippetSelectorExpr struct {
	X         Snippet
	Selectors []Snippet
}

func Sel

func Sel(x Snippet, selectors ...Snippet) *SnippetSelectorExpr

func (*SnippetSelectorExpr) Bytes

func (expr *SnippetSelectorExpr) Bytes() []byte

type SnippetSpec

type SnippetSpec interface {
	Snippet
	// contains filtered or unexported methods
}

type SnippetStarExpr

type SnippetStarExpr struct {
	SnippetCanAddr
	SnippetType
	X SnippetType
}

func Star

func Star(tpe SnippetType) *SnippetStarExpr

func (*SnippetStarExpr) Bytes

func (expr *SnippetStarExpr) Bytes() []byte

type SnippetSwitchStmt

type SnippetSwitchStmt struct {
	Init    Snippet
	Cond    Snippet
	Clauses []*SnippetClause
}

func Switch

func Switch(cond Snippet) *SnippetSwitchStmt

func (*SnippetSwitchStmt) Bytes

func (stmt *SnippetSwitchStmt) Bytes() []byte

func (SnippetSwitchStmt) InitWith

func (stmt SnippetSwitchStmt) InitWith(init Snippet) *SnippetSwitchStmt

func (SnippetSwitchStmt) When

func (stmt SnippetSwitchStmt) When(clauses ...*SnippetClause) *SnippetSwitchStmt

type SnippetType

type SnippetType interface {
	Snippet
	// contains filtered or unexported methods
}

type SnippetTypeAssertExpr

type SnippetTypeAssertExpr struct {
	X    Snippet
	Type SnippetType
}

func TypeAssert

func TypeAssert(tpe SnippetType, x Snippet) *SnippetTypeAssertExpr

func (*SnippetTypeAssertExpr) Bytes

func (expr *SnippetTypeAssertExpr) Bytes() []byte

type SnippetTypeDecl

type SnippetTypeDecl struct {
	Token token.Token
	Specs []SnippetSpec
}

func DeclConst

func DeclConst(specs ...SnippetSpec) *SnippetTypeDecl

func DeclType

func DeclType(specs ...SnippetSpec) *SnippetTypeDecl

func DeclVar

func DeclVar(specs ...SnippetSpec) *SnippetTypeDecl

func (*SnippetTypeDecl) Bytes

func (decl *SnippetTypeDecl) Bytes() []byte

type SnippetUnaryExpr

type SnippetUnaryExpr struct {
	Elem SnippetCanAddr
}

func Unary

func Unary(addr SnippetCanAddr) *SnippetUnaryExpr

func (*SnippetUnaryExpr) Bytes

func (tpe *SnippetUnaryExpr) Bytes() []byte

type StructType

type StructType struct {
	SnippetType
	Fields []*SnippetField
}

func Struct

func Struct(fields ...*SnippetField) *StructType

func (*StructType) Bytes

func (tpe *StructType) Bytes() []byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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