Documentation
¶
Index ¶
- Variables
- func GeneratedFileSuffix(filename string) string
- func IsEmptyValue(rv reflect.Value) bool
- func IsGoFile(filename string) bool
- func IsGoTestFile(filename string) bool
- func IsValidIdent(s string) bool
- func LowerCamelCase(s string) string
- func LowerSnakeCase(s string) string
- func Stringify(snippet Snippet) string
- func TryCatch(f func()) (err error)
- func UpperCamelCase(s string) string
- func UpperSnakeCase(s string) string
- type ArrayType
- type Body
- type BuiltInType
- type ChanType
- type EllipsisType
- type File
- func (file *File) Bytes() []byte
- func (file *File) Expr(f string, args ...interface{}) SnippetExpr
- func (file *File) TypeOf(tpe reflect.Type) SnippetType
- func (file *File) Use(importPath string, exposedName string) string
- func (file *File) Val(v interface{}) Snippet
- func (file *File) WriteBlock(ss ...Snippet)
- func (file *File) WriteFile() (int, error)
- type FuncType
- type GenericType
- type ImportPathAliaser
- type InterfaceType
- type MapType
- type NamedType
- type SliceType
- type Snippet
- type SnippetAssignStmt
- type SnippetBuiltIn
- type SnippetCallExpr
- type SnippetCanAddr
- type SnippetCanBeInterfaceMethod
- type SnippetClause
- type SnippetComments
- type SnippetCompositeLit
- type SnippetExpr
- type SnippetField
- func (f SnippetField) AsAlias() *SnippetField
- func (f *SnippetField) Bytes() []byte
- func (f SnippetField) WithComments(comments ...string) *SnippetField
- func (f SnippetField) WithTag(tag string) *SnippetField
- func (f SnippetField) WithTags(tags map[string][]string) *SnippetField
- func (f SnippetField) WithoutTag() *SnippetField
- type SnippetForStmt
- type SnippetIdent
- type SnippetIfStmt
- type SnippetKeyValueExpr
- type SnippetLit
- type SnippetParenExpr
- type SnippetRangeStmt
- type SnippetReturnStmt
- type SnippetSelectStmt
- type SnippetSelectorExpr
- type SnippetSpec
- type SnippetStarExpr
- type SnippetSwitchStmt
- type SnippetType
- type SnippetTypeAssertExpr
- type SnippetTypeDecl
- type SnippetUnaryExpr
- type StructType
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 IsEmptyValue ¶
func IsGoTestFile ¶
func IsValidIdent ¶
func LowerCamelCase ¶
func LowerSnakeCase ¶
func UpperCamelCase ¶
func UpperSnakeCase ¶
Types ¶
type ArrayType ¶
type ArrayType struct {
SnippetType
Elem SnippetType
Len int
}
func Array ¶
func Array(tpe SnippetType, len int) *ArrayType
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
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 ¶
func NewFile ¶
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) Expr ¶
func (file *File) Expr(f string, args ...interface{}) SnippetExpr
func (*File) WriteBlock ¶
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) MethodOf ¶
func (f FuncType) MethodOf(recv *SnippetField) *FuncType
func (FuncType) Return ¶
func (f FuncType) Return(results ...*SnippetField) *FuncType
type GenericType ¶ added in v1.0.2
type GenericType struct {
SnippetType
SnippetCanBeInterfaceMethod
SnippetCanAddr
BaseType SnippetType
TypeArgs []SnippetType
}
func Generic ¶ added in v1.0.2
func Generic(baseType SnippetType, typeArgs ...SnippetType) *GenericType
Generic 创建泛型类型
func (*GenericType) Bytes ¶ added in v1.0.2
func (tpe *GenericType) Bytes() []byte
type ImportPathAliaser ¶
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
type NamedType ¶
type NamedType struct {
SnippetType
SnippetCanBeInterfaceMethod
SnippetCanAddr
Name *SnippetIdent
}
type SliceType ¶
type SliceType struct {
SnippetType
Elem SnippetType
}
func Slice ¶
func Slice(tpe SnippetType) *SliceType
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
Source Files
¶
Click to show internal directories.
Click to hide internal directories.