codegen

package
v1.2.0-rc1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

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 GenerateFileSuffix(fn string) string

func IsBuiltinFunc

func IsBuiltinFunc(s string) bool

func IsEmptyValue

func IsEmptyValue(rv reflect.Value) bool

func IsGoFile

func IsGoFile(filename string) bool

func IsGoTestFile

func IsGoTestFile(filename string) bool

func IsReserved

func IsReserved(s string) bool

func IsValidIdent

func IsValidIdent(s string) bool

func SetPkgNaming

func SetPkgNaming(fn func(string) string)

func Stringify

func Stringify(s Snippet) string

func TypeWithAlias

func TypeWithAlias(aliase FnAlaise) func(reflect.Type) SnippetType

func ValueWithAlias

func ValueWithAlias(alias FnAlaise) func(interface{}) Snippet

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

func (*ArrayType) Bytes

func (s *ArrayType) 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 (t BuiltInType) Bytes() []byte

type ChanMode

type ChanMode uint8
const (
	ChanModeRO ChanMode = 0x01
	ChanModeWO ChanMode = 0x10
	ChanModeRW ChanMode = 0x11
)

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

func (*ChanType) Bytes

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

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

func NewFile(pkg, name string) *File
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) Bytes

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

func (*File) Expr

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

func (File) Formatted

func (f File) Formatted() []byte

Formatted test only

func (File) Raw

func (f File) Raw() []byte

Raw test only

func (*File) Type

func (f *File) Type(t reflect.Type) SnippetType

func (*File) Use

func (f *File) Use(pkg, name string) string

func (*File) Value

func (f *File) Value(v interface{}) Snippet

func (*File) Write

func (f *File) Write(opts ...WriterOptionSetter) (int, error)

func (*File) WriteSnippet

func (f *File) WriteSnippet(ss ...Snippet)

type FnAlaise

type FnAlaise func(string) string

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

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

func (FuncType) Do

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

func (FuncType) MethodOf

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

func (FuncType) Named

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

func (FuncType) Return

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

func (FuncType) WithoutToken

func (f FuncType) WithoutToken() *FuncType

type IfCanAddr

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

type IfCanBeIfMethod

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

type IfSpec

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

type IfType

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

func (*MapType) Bytes

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

type NamedType

type NamedType struct {
	SnippetType
	IfCanBeIfMethod
	IfCanAddr
	Name *SnippetIdent
}

func Type

func Type(name string) *NamedType
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

func (*NamedType) Bytes

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

type SliceType

type SliceType struct {
	SnippetType
	T SnippetType
}

func Slice

func Slice(t SnippetType) *SliceType

func (*SliceType) Bytes

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

type Snippet

type Snippet interface{ Bytes() []byte }

type SnippetAccessExpr

type SnippetAccessExpr struct {
	V     Snippet
	Index Snippet
}

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 (*SnippetAccessValueExpr) Bytes

func (s *SnippetAccessValueExpr) Bytes() []byte

type SnippetAddrExpr

type SnippetAddrExpr struct {
	V SnippetCanAddr
}

func Addr

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

type SnippetCallExpr struct {
	Callee   Snippet
	Args     []Snippet
	Ellipsis bool
	Modifier token.Token
}

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 SnippetCanAddr interface {
	Snippet
	IfCanAddr
}

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

type SnippetComments struct {
	OneLine  bool
	Comments []string
}

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

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

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 (SnippetIfStmt) InitWith

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

type SnippetIncExpr

type SnippetIncExpr struct {
	Value SnippetCanAddr
}

func Inc

Example
fmt.Println(string(Inc(Ident("i")).Bytes()))
Output:

i++

func (*SnippetIncExpr) Bytes

func (s *SnippetIncExpr) Bytes() []byte

type SnippetKVExpr

type SnippetKVExpr struct {
	Snippet
	K, V Snippet
}

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

type SnippetRefExpr struct {
	Lead Snippet
	Refs []Snippet
}

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 SnippetSpec interface {
	Snippet
	IfSpec
}

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

type SnippetType

type SnippetType interface {
	Snippet
	IfType
}

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 WriteOption struct {
	WithCommit      bool
	WithTimestamp   bool
	WithToolVersion bool
	MustFormat      bool
}

type WriterOptionSetter

type WriterOptionSetter func(v *WriteOption)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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