Documentation
¶
Index ¶
- Variables
- func ExprWithAlias(alias FnAlaise) func(string, ...interface{}) SnippetExpr
- func GenerateFileSuffix(fn string) string
- func IsBuiltinFunc(s string) bool
- func IsEmptyValue(rv reflect.Value) bool
- func IsGoFile(filename string) bool
- func IsGoTestFile(filename string) bool
- func IsReserved(s string) bool
- func IsValidIdent(s string) bool
- func SetPkgNaming(fn func(string) string)
- func Stringify(s Snippet) string
- func TypeWithAlias(aliase FnAlaise) func(reflect.Type) SnippetType
- func ValueWithAlias(alias FnAlaise) func(interface{}) Snippet
- func WriteOptionMustFormat(v *WriteOption)
- func WriteOptionWithCommit(v *WriteOption)
- func WriteOptionWithTimestamp(v *WriteOption)
- func WriteOptionWithToolVersion(v *WriteOption)
- type ArrayType
- type BuiltInType
- type ChanMode
- type ChanType
- type EllipsisType
- type File
- func (f *File) Bytes() []byte
- func (f *File) Expr(format string, args ...interface{}) SnippetExpr
- func (f File) Formatted() []byte
- func (f File) Raw() []byte
- func (f *File) Type(t reflect.Type) SnippetType
- func (f *File) Use(pkg, name string) string
- func (f *File) Value(v interface{}) Snippet
- func (f *File) Write(opts ...WriterOptionSetter) (int, error)
- func (f *File) WriteSnippet(ss ...Snippet)
- type FnAlaise
- type FuncType
- type IfCanAddr
- type IfCanBeIfMethod
- type IfSpec
- type IfType
- type InterfaceType
- type MapType
- type NamedType
- type SliceType
- type Snippet
- type SnippetAccessExpr
- type SnippetAccessValueExpr
- type SnippetAddrExpr
- type SnippetArrowExpr
- type SnippetAssignStmt
- type SnippetBlock
- type SnippetBlockWithBrace
- type SnippetBuiltIn
- type SnippetCallExpr
- type SnippetCanAddr
- type SnippetCaseClause
- type SnippetComments
- type SnippetDecExpr
- type SnippetExpr
- type SnippetField
- func (s SnippetField) AsAlias() *SnippetField
- func (s *SnippetField) Bytes() []byte
- func (s SnippetField) WithComments(cmt ...string) *SnippetField
- func (s SnippetField) WithOneLineComment(cmt string) *SnippetField
- func (s SnippetField) WithTag(tag string) *SnippetField
- func (s SnippetField) WithTags(tags map[string][]string) *SnippetField
- func (s SnippetField) WithoutTag() *SnippetField
- type SnippetForRangeStmt
- type SnippetForStmt
- type SnippetIdent
- type SnippetIfStmt
- type SnippetIncExpr
- type SnippetKVExpr
- type SnippetLiteral
- type SnippetLiteralCompose
- type SnippetParenExpr
- type SnippetRefExpr
- type SnippetReturnStmt
- type SnippetSelectStmt
- type SnippetSpec
- type SnippetStarExpr
- type SnippetSwitchStmt
- type SnippetType
- type SnippetTypeAssertExpr
- type SnippetTypeDecl
- type StructType
- type WriteOption
- type WriterOptionSetter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ReservedKeys = map[string]bool{ "bool": true, "int": true, "int8": true, "int16": true, "int32": true, "int64": true, "uint": true, "uint8": true, "uint16": true, "uint32": true, "uint64": true, "uintptr": true, "float32": true, "float64": true, "complex64": true, "complex128": true, "string": true, "byte": true, "rune": true, "error": true, "iota": true, "true": true, "false": true, "nil": true, "break": true, "continue": true, "fallthrough": true, } BuiltinFuncs = map[string]bool{ "append": true, "complex": true, "cap": true, "close": true, "copy": true, "delete": true, "imag": true, "len": true, "make": true, "new": true, "panic": true, "print": true, "println": true, "real": true, "recover": true, } )
View Source
var ( Valuer = ValueWithAlias(naming) Typer = TypeWithAlias(naming) Exprer = ExprWithAlias(naming) )
View Source
var Version = "0.0.1"
Functions ¶
func ExprWithAlias ¶
func ExprWithAlias(alias FnAlaise) func(string, ...interface{}) SnippetExpr
func GenerateFileSuffix ¶
func IsBuiltinFunc ¶
func IsEmptyValue ¶
func IsGoTestFile ¶
func IsReserved ¶
func IsValidIdent ¶
func SetPkgNaming ¶
func TypeWithAlias ¶
func TypeWithAlias(aliase FnAlaise) func(reflect.Type) SnippetType
func ValueWithAlias ¶
func WriteOptionMustFormat ¶
func WriteOptionMustFormat(v *WriteOption)
func WriteOptionWithCommit ¶
func WriteOptionWithCommit(v *WriteOption)
func WriteOptionWithTimestamp ¶
func WriteOptionWithTimestamp(v *WriteOption)
func WriteOptionWithToolVersion ¶
func WriteOptionWithToolVersion(v *WriteOption)
Types ¶
type ArrayType ¶
type ArrayType struct {
SnippetType
T SnippetType
Len int
}
func Array ¶
func Array(t SnippetType, l 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 (t BuiltInType) Bytes() []byte
type ChanType ¶
type ChanType struct {
SnippetType
T SnippetType
Mode ChanMode
}
func Chan ¶
func Chan(t SnippetType) *ChanType
func ChanRO ¶
func ChanRO(t SnippetType) *ChanType
func ChanWO ¶
func ChanWO(t SnippetType) *ChanType
type EllipsisType ¶
type EllipsisType struct {
SnippetType
T SnippetType
}
func Ellipsis ¶
func Ellipsis(t SnippetType) *EllipsisType
func (*EllipsisType) Bytes ¶
func (s *EllipsisType) Bytes() []byte
type File ¶
type File struct {
Pkg string
Name string
Imps map[string]string
Pkgs map[string][]string
OrderedImps [][2]string
bytes.Buffer
// contains filtered or unexported fields
}
func NewFile ¶
Example ¶
filename := "examples/hello/hello.go"
f := NewFile("main", filename)
f.WriteSnippet(
DeclVar(Var(Slice(String), "lines")),
Func().Named("main").Do(
DeclVar(
Assign(Var(nil, "ch")).By(Call("make", String, Valuer(10))),
),
Call("close", Ident("ch")).AsDefer(),
Call("PipeReadAndPrint", Ident("ch")).AsRoutine(),
Call("PipePrintAndWrite", Ident("ch"), Ident("lines")),
),
Func(Var(ChanRO(String), "ch"), Var(Ellipsis(String), "v")).
Named("PipePrintAndWrite").
Do(
ForRange(Ident("v"), Ident("i"), Ident("_")).Do(
Call(f.Use("fmt", "Println"), AccessWith(Ident("v"), Ident("i"))),
Exprer("? <- ?", Ident("ch"), AccessWith(Ident("v"), Ident("i"))),
),
),
Func(Var(ChanRO(String), "ch")).Named("PipeReadAndPrint").Do(
For(nil, nil, nil).Do(
Select(
CaseClause(Define(Ident("s"), Ident("ok")).By(Arrow(Ident("ch")))).Do(
If(Ident("ok")).Do(
Call(f.Use("fmt", "Println"), Ident("s")),
).Else(
If(Exprer("!ok")).Do(
Return(),
),
),
),
),
),
),
)
fmt.Println(string(f.Formatted()))
Output: // This is a generated source file. DO NOT EDIT // Source: main/hello.go package main import "fmt" var lines []string func main() { var ch = make(string, 10) defer close(ch) go PipeReadAndPrint(ch) PipePrintAndWrite(ch, lines) } func PipePrintAndWrite(ch <-chan string, v ...string) { for i := range v { fmt.Println(v[i]) ch <- v[i] } } func PipeReadAndPrint(ch <-chan string) { for { select { case s, ok := <-ch: if ok { fmt.Println(s) } else if !ok { return } } } }
func (*File) Expr ¶
func (f *File) Expr(format string, args ...interface{}) SnippetExpr
func (*File) WriteSnippet ¶
type FuncType ¶
type FuncType struct {
SnippetType
IfCanBeIfMethod
Name *SnippetIdent
Recv *SnippetField
Args []*SnippetField
Rets []*SnippetField
Blk SnippetBlockWithBrace
// contains filtered or unexported fields
}
func Func ¶
func Func(args ...*SnippetField) *FuncType
func (FuncType) MethodOf ¶
func (f FuncType) MethodOf(rcv *SnippetField) *FuncType
func (FuncType) Return ¶
func (f FuncType) Return(rets ...*SnippetField) *FuncType
func (FuncType) WithoutToken ¶
type IfCanBeIfMethod ¶
type IfCanBeIfMethod interface {
// contains filtered or unexported methods
}
type InterfaceType ¶
type InterfaceType struct {
SnippetType
Methods []IfCanBeIfMethod
}
func Interface ¶
func Interface(methods ...IfCanBeIfMethod) *InterfaceType
func (*InterfaceType) Bytes ¶
func (s *InterfaceType) Bytes() []byte
type MapType ¶
type MapType struct {
SnippetType
Tk, Tv SnippetType
}
func Map ¶
func Map(k, v SnippetType) *MapType
type NamedType ¶
type NamedType struct {
SnippetType
IfCanBeIfMethod
IfCanAddr
Name *SnippetIdent
}
func Type ¶
Example ¶
var t = Type("Name")
fmt.Println(string(t.Bytes()))
fmt.Println(string(Chan(t).Bytes()))
fmt.Println(string(Chan(Star(t)).Bytes()))
fmt.Println(string(Array(t, 10).Bytes()))
fmt.Println(string(Array(Star(t), 10).Bytes()))
fmt.Println(string(Slice(t).Bytes()))
fmt.Println(string(Map(Bool, Star(t)).Bytes()))
Output: Name chan Name chan *Name [10]Name [10]*Name []Name map[bool]*Name
type SliceType ¶
type SliceType struct {
SnippetType
T SnippetType
}
func Slice ¶
func Slice(t SnippetType) *SliceType
type SnippetAccessExpr ¶
func Access ¶
func Access(v SnippetCanAddr, index int) *SnippetAccessExpr
Example ¶
output(Access(Ident("array"), 10))
output(AccessWith(Ident("array"), Exprer("?-?", Call("len", Ident("array")), Valuer(1))))
output(AccessWith(Ident("array"), Exprer("len(array)-1")))
Output: array[10] array[len(array)-1] array[len(array)-1]
func AccessWith ¶
func AccessWith(v SnippetCanAddr, index Snippet) *SnippetAccessExpr
func (*SnippetAccessExpr) Bytes ¶
func (s *SnippetAccessExpr) Bytes() []byte
type SnippetAccessValueExpr ¶
type SnippetAccessValueExpr struct {
V SnippetCanAddr
IfCanAddr
}
func AccessValue ¶
func AccessValue(v SnippetCanAddr) *SnippetAccessValueExpr
func (*SnippetAccessValueExpr) Bytes ¶
func (s *SnippetAccessValueExpr) Bytes() []byte
type SnippetAddrExpr ¶
type SnippetAddrExpr struct {
V SnippetCanAddr
}
func Addr ¶
func Addr(val SnippetCanAddr) *SnippetAddrExpr
Example ¶
fmt.Println(Stringify(Addr(Ident("i"))))
Output: &i
func (*SnippetAddrExpr) Bytes ¶
func (s *SnippetAddrExpr) Bytes() []byte
type SnippetArrowExpr ¶
type SnippetArrowExpr struct {
SnippetCanAddr
Chan Snippet
}
func Arrow ¶
func Arrow(ch Snippet) *SnippetArrowExpr
Example ¶
fmt.Println(Stringify(Arrow(Ident("ch"))))
fmt.Println(Stringify(Define(Ident("val")).By(Arrow(Ident("ch")))))
Output: <-ch val := <-ch
func (*SnippetArrowExpr) Bytes ¶
func (s *SnippetArrowExpr) Bytes() []byte
type SnippetAssignStmt ¶
type SnippetAssignStmt struct {
IfSpec
Token token.Token
Ls []SnippetCanAddr
Rs []Snippet
}
func Assign ¶
func Assign(ls ...SnippetCanAddr) *SnippetAssignStmt
func AssignWith ¶
func AssignWith(tok token.Token, ls ...SnippetCanAddr) *SnippetAssignStmt
func Define ¶
func Define(ls ...SnippetCanAddr) *SnippetAssignStmt
Example ¶
fmt.Println(Stringify(
Define(
Ident("a"),
Ident("b"),
).By(
Ident("c"),
Call("Fn", Valuer(1)),
)),
)
fmt.Println(Stringify(
Define(
Ident("a"),
Ident("b"),
).By(
Ident("c"),
Call("Fn", Valuer("abc")),
)),
)
fmt.Println(Stringify(
DeclVar(
Var(Int, "a"),
Assign(Var(String, "a", "b")).By(Call("Fn", Valuer(1))),
),
))
fmt.Println(Stringify(
DeclConst(
Assign(Var(Int, "a")).By(Iota),
Assign(Ident("b")),
Assign(Ident("c")),
),
))
fmt.Println(Stringify(
DeclType(
Var(Type("time.Time"), "aliasTime").AsAlias(),
),
))
Output: a, b := c, Fn(1) a, b := c, Fn("abc") var ( a int a, b string = Fn(1) ) const ( a int = iota b c ) type aliasTime = time.Time
func (SnippetAssignStmt) By ¶
func (s SnippetAssignStmt) By(rs ...Snippet) *SnippetAssignStmt
func (*SnippetAssignStmt) Bytes ¶
func (s *SnippetAssignStmt) Bytes() []byte
type SnippetBlock ¶
type SnippetBlock []Snippet
SnippetBlock code block, like ```go
var a = "Hello CodeGen" fmt.Print(a)
```
func (SnippetBlock) Bytes ¶
func (s SnippetBlock) Bytes() []byte
type SnippetBlockWithBrace ¶
type SnippetBlockWithBrace []Snippet
SnippetBlockWithBrace code block quote with '{' and '}', like ```go
{
var a = "Hello CodeGen"
fmt.Print(a)
}
```
func (SnippetBlockWithBrace) Bytes ¶
func (s SnippetBlockWithBrace) Bytes() []byte
type SnippetBuiltIn ¶
type SnippetBuiltIn string
SnippetBuiltIn built-in symbols, like `int`, `println`
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 (s SnippetBuiltIn) Bytes() []byte
type SnippetCallExpr ¶
func Call ¶
func Call(name string, args ...Snippet) *SnippetCallExpr
Example ¶
fmt.Println(Stringify(Call("NewBuffer", Nil)))
Output: NewBuffer(nil)
func CallMakeChan ¶
func CallMakeChan(t SnippetType, length int) *SnippetCallExpr
func CallWith ¶
func CallWith(callee Snippet, args ...Snippet) *SnippetCallExpr
Example ¶
fmt.Println(Stringify(Ref(Ident("bytes"), CallWith(Ident("NewBuffer"), Nil))))
Output: bytes.NewBuffer(nil)
func Casting ¶
func Casting(ori Snippet, tar Snippet) *SnippetCallExpr
Example ¶
fmt.Println(Stringify(DeclVar(
Assign(Ident("a")).By(Casting(Float64, Valuer(0.1))),
Assign(Ident("b")).By(Casting(Float32, Ident("a"))),
)))
Output: var ( a = float64(0.1) b = float32(a) )
func (SnippetCallExpr) AsDefer ¶
func (s SnippetCallExpr) AsDefer() *SnippetCallExpr
func (SnippetCallExpr) AsRoutine ¶
func (s SnippetCallExpr) AsRoutine() *SnippetCallExpr
func (*SnippetCallExpr) Bytes ¶
func (s *SnippetCallExpr) Bytes() []byte
func (SnippetCallExpr) WithEllipsis ¶
func (s SnippetCallExpr) WithEllipsis() *SnippetCallExpr
type SnippetCanAddr ¶
type SnippetCaseClause ¶
type SnippetCaseClause struct {
Case []Snippet
Blk SnippetBlock
}
func CaseClause ¶
func CaseClause(s ...Snippet) *SnippetCaseClause
Example ¶
clauses = append(clauses,
CaseClause(Valuer(1), Valuer(2)),
CaseClause(Arrow(Ref(Ident("time"), Call("After")))),
CaseClause().Do(Return(Ident("a"), Call("Fn", Valuer(1)))),
)
for _, c := range clauses {
fmt.Println(Stringify(c))
}
Output: case 1, 2: case <-time.After(): default: return a, Fn(1)
func (*SnippetCaseClause) Bytes ¶
func (s *SnippetCaseClause) Bytes() []byte
func (SnippetCaseClause) Do ¶
func (s SnippetCaseClause) Do(bodies ...Snippet) *SnippetCaseClause
type SnippetComments ¶
SnippetComments comment code
func Comments ¶
func Comments(cmt ...string) *SnippetComments
Example ¶
fmt.Println(Stringify(DeclVar(
Var(Int, "a").WithOneLineComment("this is a int var"),
)))
Output: var a int // this is a int var
func (SnippetComments) Append ¶
func (s SnippetComments) Append(cmt ...string) SnippetComments
func (SnippetComments) AsOneLine ¶
func (s SnippetComments) AsOneLine() *SnippetComments
func (*SnippetComments) Bytes ¶
func (s *SnippetComments) Bytes() []byte
func (SnippetComments) IsOneLine ¶
func (s SnippetComments) IsOneLine() bool
type SnippetDecExpr ¶
type SnippetDecExpr struct {
Value SnippetCanAddr
}
func Dec ¶
func Dec(v SnippetCanAddr) *SnippetDecExpr
Example ¶
fmt.Println(string(Dec(Ident("i")).Bytes()))
Output: i--
func (*SnippetDecExpr) Bytes ¶
func (s *SnippetDecExpr) Bytes() []byte
type SnippetExpr ¶
type SnippetExpr string
SnippetExpr expression `a == 0 // a == 0 is a expression`
func (SnippetExpr) Bytes ¶
func (s SnippetExpr) Bytes() []byte
type SnippetField ¶
type SnippetField struct {
SnippetSpec
SnippetCanAddr
Type SnippetType
Names []*SnippetIdent
Tag string
Alias bool
*SnippetComments
}
SnippetField define a field or var eg: a int a, b int AliasString = string
func Var ¶
func Var(t SnippetType, names ...string) *SnippetField
Example ¶
var v = Var(String, "a")
fmt.Println(string(v.Bytes()))
fmt.Println(string(v.WithComments("A is a string").Bytes()))
v = v.WithTag(`json:"a"`)
fmt.Println(string(v.Bytes()))
v = v.WithoutTag()
fmt.Println(string(v.Bytes()))
v = Var(String, "AliasStringType").AsAlias().
WithComments("AliasStringType is a Alias of string")
fmt.Println(string(v.Bytes()))
Output: a string // A is a string a string a string `json:"a"` a string // AliasStringType is a Alias of string AliasStringType = string
func (SnippetField) AsAlias ¶
func (s SnippetField) AsAlias() *SnippetField
func (*SnippetField) Bytes ¶
func (s *SnippetField) Bytes() []byte
func (SnippetField) WithComments ¶
func (s SnippetField) WithComments(cmt ...string) *SnippetField
func (SnippetField) WithOneLineComment ¶
func (s SnippetField) WithOneLineComment(cmt string) *SnippetField
func (SnippetField) WithTag ¶
func (s SnippetField) WithTag(tag string) *SnippetField
func (SnippetField) WithTags ¶
func (s SnippetField) WithTags(tags map[string][]string) *SnippetField
func (SnippetField) WithoutTag ¶
func (s SnippetField) WithoutTag() *SnippetField
type SnippetForRangeStmt ¶
type SnippetForRangeStmt struct {
K, V SnippetIdent
Ranger Snippet
Blk SnippetBlockWithBrace
}
func ForRange ¶
func ForRange(ranger Snippet, k, v *SnippetIdent) *SnippetForRangeStmt
Example ¶
k, v, ranger := Ident("k"), Ident("v"), Ident("ranger")
fmt.Println(
Stringify(
ForRange(ranger, k, v).Do(),
),
)
Output: for k, v := range ranger { }
func (*SnippetForRangeStmt) Bytes ¶
func (s *SnippetForRangeStmt) Bytes() []byte
func (SnippetForRangeStmt) Do ¶
func (s SnippetForRangeStmt) Do(blk ...Snippet) *SnippetForRangeStmt
type SnippetForStmt ¶
type SnippetForStmt struct {
Init, Cond, Post Snippet
Blk SnippetBlockWithBrace
}
func For ¶
func For(init, cond, post Snippet) *SnippetForStmt
Example ¶
i := Ident("i")
fmt.Println(
Stringify(
For(
Define(i).By(Valuer(0)),
AssignWith(token.LSS, i).By(Valuer(10)),
Inc(i),
).Do(),
),
)
Output: for i := 0; i < 10; i++ { }
func (*SnippetForStmt) Bytes ¶
func (s *SnippetForStmt) Bytes() []byte
func (SnippetForStmt) Do ¶
func (s SnippetForStmt) Do(blk ...Snippet) *SnippetForStmt
type SnippetIdent ¶
type SnippetIdent string
SnippetIdent idenifier `a := 0 // a is a identifier`
const AnonymousIdent SnippetIdent = "_"
func Ident ¶
func Ident(s string) *SnippetIdent
Example ¶
ids := Idents("a", "b", "C", "Type.Field")
for _, id := range ids {
fmt.Println(string(id.Bytes()))
}
Output: a b C Type.Field
func Idents ¶
func Idents(s ...string) []*SnippetIdent
func (SnippetIdent) Bytes ¶
func (s SnippetIdent) Bytes() []byte
func (SnippetIdent) LowerCamelCase ¶
func (s SnippetIdent) LowerCamelCase() *SnippetIdent
func (SnippetIdent) LowerSnakeCase ¶
func (s SnippetIdent) LowerSnakeCase() *SnippetIdent
func (SnippetIdent) UpperCamelCase ¶
func (s SnippetIdent) UpperCamelCase() *SnippetIdent
func (SnippetIdent) UpperSnakeCase ¶
func (s SnippetIdent) UpperSnakeCase() *SnippetIdent
type SnippetIfStmt ¶
type SnippetIfStmt struct {
Init, Cond Snippet
Blk SnippetBlockWithBrace
ElseList []*SnippetIfStmt
}
func If ¶
func If(cond Snippet) *SnippetIfStmt
func (*SnippetIfStmt) Bytes ¶
func (s *SnippetIfStmt) Bytes() []byte
func (SnippetIfStmt) Do ¶
func (s SnippetIfStmt) Do(ss ...Snippet) *SnippetIfStmt
func (SnippetIfStmt) Else ¶
func (s SnippetIfStmt) Else(sub *SnippetIfStmt) *SnippetIfStmt
func (SnippetIfStmt) InitWith ¶
func (s SnippetIfStmt) InitWith(init Snippet) *SnippetIfStmt
type SnippetIncExpr ¶
type SnippetIncExpr struct {
Value SnippetCanAddr
}
func Inc ¶
func Inc(v SnippetCanAddr) *SnippetIncExpr
Example ¶
fmt.Println(string(Inc(Ident("i")).Bytes()))
Output: i++
func (*SnippetIncExpr) Bytes ¶
func (s *SnippetIncExpr) Bytes() []byte
type SnippetKVExpr ¶
func KeyValue ¶
func KeyValue(k, v Snippet) *SnippetKVExpr
Example ¶
fmt.Println(string(KeyValue(Valuer("key"), Valuer("value")).Bytes()))
Output: "key": "value"
func (*SnippetKVExpr) Bytes ¶
func (s *SnippetKVExpr) Bytes() []byte
type SnippetLiteral ¶
type SnippetLiteral string
func Literal ¶
func Literal(s string) *SnippetLiteral
func (SnippetLiteral) Bytes ¶
func (s SnippetLiteral) Bytes() []byte
type SnippetLiteralCompose ¶
type SnippetLiteralCompose struct {
Type SnippetType
Elements []Snippet
}
func Compose ¶
func Compose(typ SnippetType, elements ...Snippet) *SnippetLiteralCompose
func (*SnippetLiteralCompose) Bytes ¶
func (s *SnippetLiteralCompose) Bytes() []byte
type SnippetParenExpr ¶
type SnippetParenExpr struct {
SnippetCanAddr
V Snippet
}
func Paren ¶
func Paren(s Snippet) *SnippetParenExpr
Example ¶
fmt.Println(Stringify(Paren(Valuer(1))))
Output: (1)
func (*SnippetParenExpr) Bytes ¶
func (s *SnippetParenExpr) Bytes() []byte
type SnippetRefExpr ¶
func Ref ¶
func Ref(lead Snippet, refs ...Snippet) *SnippetRefExpr
func (*SnippetRefExpr) Bytes ¶
func (s *SnippetRefExpr) Bytes() []byte
type SnippetReturnStmt ¶
type SnippetReturnStmt struct {
Res []Snippet
}
func Return ¶
func Return(s ...Snippet) *SnippetReturnStmt
func (*SnippetReturnStmt) Bytes ¶
func (s *SnippetReturnStmt) Bytes() []byte
type SnippetSelectStmt ¶
type SnippetSelectStmt struct {
Clauses []*SnippetCaseClause
}
func Select ¶
func Select(clauses ...*SnippetCaseClause) *SnippetSelectStmt
Example ¶
ch1, ch2 := "ch1", "ch2"
fmt.Println(Stringify(DeclVar(
Assign(Var(nil, ch1)).By(CallMakeChan(Int, 10)),
Assign(Var(nil, ch2)).By(CallMakeChan(String, 10)),
Var(String, "ret"),
)))
fmt.Println(Stringify(
Select(
CaseClause(Define(Ident("i")).By(Arrow(Ident(ch1)))).Do(
Assign(Ident("ret")).By(
Ref(Ident("fmt"), Call("Sprintf", Ident("i")))),
),
CaseClause(Define(Ident("s")).By(Arrow(Ident(ch2)))).Do(
Assign(Ident("ret")).By(Ident("s")),
),
CaseClause().Do(),
),
))
fmt.Println(Stringify(Return(Ident("ret"))))
Output: var ( ch1 = make(chan int, 10) ch2 = make(chan string, 10) ret string ) select { case i := <-ch1: ret = fmt.Sprintf(i) case s := <-ch2: ret = s default: } return ret
func (*SnippetSelectStmt) Bytes ¶
func (s *SnippetSelectStmt) Bytes() []byte
type SnippetSpec ¶
type SnippetStarExpr ¶
type SnippetStarExpr struct {
SnippetCanAddr
SnippetType
T SnippetType
}
func Star ¶
func Star(typ SnippetType) *SnippetStarExpr
Example ¶
fmt.Println(Stringify(Star(Int)))
Output: *int
func (*SnippetStarExpr) Bytes ¶
func (s *SnippetStarExpr) Bytes() []byte
type SnippetSwitchStmt ¶
type SnippetSwitchStmt struct {
Init, Cond Snippet
Clauses []*SnippetCaseClause
}
func Switch ¶
func Switch(cond Snippet) *SnippetSwitchStmt
Example ¶
fmt.Println(Stringify(Switch(nil).When(clauses...)))
os := Ident("os")
tar := Ident("tar")
_ = runtime.GOOS
fmt.Print(Stringify(
Switch(os).
InitWith(Define(os).By(Ref(Ident("runtime"), Ident("GOOS")))).
When(
CaseClause(Valuer("darwin")).Do(
Assign(tar).By(Ident("OS_DARWIN")),
),
CaseClause(Valuer("linux")).Do(
Assign(tar).By(Ident("OS_LINUX")),
),
CaseClause(Valuer("windows")).Do(
Assign(tar).By(Ident("OS_WINDOWS")),
),
CaseClause().Do(
Assign(tar).By(Ident("OS_UNKNOWN")),
),
CaseClause(Valuer("doNothing")),
),
))
Output: switch { case 1, 2: case <-time.After(): default: return a, Fn(1) } switch os := runtime.GOOS; os { case "darwin": tar = OS_DARWIN case "linux": tar = OS_LINUX case "windows": tar = OS_WINDOWS default: tar = OS_UNKNOWN case "doNothing": }
func (*SnippetSwitchStmt) Bytes ¶
func (s *SnippetSwitchStmt) Bytes() []byte
func (SnippetSwitchStmt) InitWith ¶
func (s SnippetSwitchStmt) InitWith(init Snippet) *SnippetSwitchStmt
func (SnippetSwitchStmt) When ¶
func (s SnippetSwitchStmt) When(clauses ...*SnippetCaseClause) *SnippetSwitchStmt
type SnippetType ¶
type SnippetTypeAssertExpr ¶
type SnippetTypeAssertExpr struct {
Asserter Snippet
Type SnippetType
}
func TypeAssert ¶
func TypeAssert(typ SnippetType, z Snippet) *SnippetTypeAssertExpr
Example ¶
ta := TypeAssert(String, Ident("value"))
fmt.Println(string(ta.Bytes()))
ta = TypeAssert(Type("Name"), Ident("value"))
fmt.Println(string(ta.Bytes()))
ta = TypeAssert(Star(Type("Name")), Ident("value"))
fmt.Println(string(ta.Bytes()))
Output: value.(string) value.(Name) value.(*Name)
func (*SnippetTypeAssertExpr) Bytes ¶
func (s *SnippetTypeAssertExpr) Bytes() []byte
type SnippetTypeDecl ¶
type SnippetTypeDecl struct {
Token token.Token
Specs []SnippetSpec
*SnippetComments
}
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 (s *SnippetTypeDecl) Bytes() []byte
func (SnippetTypeDecl) WithComments ¶
func (s SnippetTypeDecl) WithComments(comments ...string) *SnippetTypeDecl
type StructType ¶
type StructType struct {
SnippetType
Fields []*SnippetField
}
func Struct ¶
func Struct(fields ...*SnippetField) *StructType
func (*StructType) Bytes ¶
func (s *StructType) Bytes() []byte
type WriteOption ¶
type WriterOptionSetter ¶
type WriterOptionSetter func(v *WriteOption)
Source Files
¶
Click to show internal directories.
Click to hide internal directories.