Documentation
¶
Index ¶
- Variables
- func Array(size int, elemType any, elements ...any) *iarray
- func Call(name string) *icall
- func Const() *iconst
- func For(judge interface{}) *ifor
- func Func(name string) *ifunction
- func Function(name string) *ifunction
- func If(judge interface{}) *iif
- func Import() *iimport
- func Interface(name string) *iinterface
- func LineComment(content string, args ...interface{}) *istring
- func Lit(value interface{}) *lit
- func Package(name string) *ipackage
- func Return(node ...interface{}) *ireturn
- func Slice(elemType any, elements ...any) *islice
- func String(format string, args ...interface{}) *istring
- func Struct(name string) *istruct
- func Switch(judge interface{}) *iswitch
- func Type(name string, typ interface{}) *itype
- func TypeAlias(name string, typ interface{}) *itype
- func Value(typ any) *ivalue
- func Var() *ivar
- type Generator
- func (g *Generator) AppendFile(path string) error
- func (g *Generator) Body() *Group
- func (g *Generator) Bytes() []byte
- func (g *Generator) Imports() []string
- func (g *Generator) Merge(other *Generator) *Generator
- func (g *Generator) NewGroup() (ng *Group)
- func (g *Generator) P(importPath string) *PackageRef
- func (g *Generator) PAlias(importPath, alias string) *PackageRef
- func (g *Generator) PackageName() string
- func (g *Generator) SetHeader(format string, args ...any) *Generator
- func (g *Generator) SetPackage(name string) *Generator
- func (g *Generator) String() string
- func (g *Generator) Write(w io.Writer)
- func (g *Generator) WriteFile(path string) error
- type Group
- func (g *Group) AddLine() *Group
- func (g *Group) AddLineComment(content string, args ...interface{}) *Group
- func (g *Group) AddPackage(name string) *Group
- func (g *Group) AddString(content string, args ...interface{}) *Group
- func (g *Group) AddType(name string, typ interface{}) *Group
- func (g *Group) AddTypeAlias(name string, typ interface{}) *Group
- func (g *Group) Append(node ...any) *Group
- func (g *Group) AppendFile(path string) errordeprecated
- func (g *Group) NewConst() *iconst
- func (g *Group) NewFor(judge interface{}) *ifor
- func (g *Group) NewFunction(name string) *ifunction
- func (g *Group) NewIf(judge interface{}) *iif
- func (g *Group) NewImport() *iimport
- func (g *Group) NewInterface(name string) *iinterface
- func (g *Group) NewStruct(name string) *istruct
- func (g *Group) NewSwitch(judge interface{}) *iswitch
- func (g *Group) NewVar() *ivar
- func (g *Group) String() string
- func (g *Group) Write(w io.Writer)deprecated
- func (g *Group) WriteFile(path string) errordeprecated
- type Node
- type PackageRef
- func (p *PackageRef) Alias() string
- func (p *PackageRef) Call(name string, args ...any) *icall
- func (p *PackageRef) Chan(name string) Node
- func (p *PackageRef) ChanRecv(name string) Node
- func (p *PackageRef) ChanSend(name string) Node
- func (p *PackageRef) Dot(name string) Node
- func (p *PackageRef) Func(name string) Node
- func (p *PackageRef) Generic(name string, typeArgs ...any) Node
- func (p *PackageRef) ImportPath() string
- func (p *PackageRef) Map(keyType any, valueType any) Node
- func (p *PackageRef) Ptr(name string) Node
- func (p *PackageRef) Slice(name string) Node
- func (p *PackageRef) Type(name string) Node
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var S = String
S is an alias to String. TODO: can we find a new name for this?
Functions ¶
func Array ¶
Array creates an array literal with the given size, element type and elements. elemType can be a string (e.g., "int", "string") or a Node (e.g., pkg.Type("User")). elements are the items to include in the array.
Example:
Array(3, "int", Lit(1), Lit(2), Lit(3)) // [3]int{1, 2, 3}
Array(2, "string", Lit("a"), Lit("b")) // [2]string{"a", "b"}
Array(5, types.Type("User"), S("User{ID: 1}")) // [5]types.User{User{ID: 1}}
func Function ¶
func Function(name string) *ifunction
Function represent both method and function in Go.
NOTES ¶
If `WithReceiver`, we will generate a method:
func (t test) Test()
If `WithCall`, we will generate a function call:
func Test(){}()
If `AddBody`, we will generate like a function definition without body:
func Test() {
println("Hello, World!")
}
func LineComment ¶
func LineComment(content string, args ...interface{}) *istring
func Slice ¶
Slice creates a slice literal with the given element type and elements. elemType can be a string (e.g., "int", "string") or a Node (e.g., pkg.Type("User")). elements are the items to include in the slice.
Example:
Slice("int", Lit(1), Lit(2), Lit(3)) // []int{1, 2, 3}
Slice("string", Lit("a"), Lit("b")) // []string{"a", "b"}
Slice(types.Type("User"), S("User{ID: 1}")) // []types.User{User{ID: 1}}
Slice(types.Ptr("Config"), S("&Config{}")) // []*types.Config{&Config{}}
func String ¶
func String(format string, args ...interface{}) *istring
String will add a format string in NewGroup, just like fmt.Printf.
func Value ¶
func Value(typ any) *ivalue
Value creates a composite literal for a given type. It can be used for struct literals or custom type literals (like type aliases).
For struct literals, use AddField:
Value("User").AddField("ID", "1").AddField("Name", Lit("Alice"))
// => User{ID: 1, Name: "Alice"}
For custom types (like slice/array type aliases), use AddElement:
Value("UserList").AddElement(S("User{ID: 1}"), S("User{ID: 2}"))
// => UserList{User{ID: 1}, User{ID: 2}}
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is the main entry point for code generation. It manages packages, imports, and provides methods for generating Go code.
func (*Generator) AppendFile ¶
AppendFile will append the group after the given path. Note: This only appends the body, not package/imports.
func (*Generator) Body ¶
Body returns the body group for adding code elements. This provides direct access to the internal group for adding functions, structs, etc.
func (*Generator) Merge ¶
Merge merges another Generator's body and imports into this one. This is useful when multiple generators need to output to the same file. The other generator's body is appended to this generator's body. Import paths are merged, with alias conflicts resolved automatically. All PackageRef in the merged generator will be updated to use the correct aliases.
func (*Generator) P ¶
func (g *Generator) P(importPath string) *PackageRef
P returns a PackageRef for the given import path. If the package was already registered, it returns the existing reference. Otherwise, it creates a new reference with an automatically resolved alias.
Example:
types := gen.P("github.com/example/types")
other := gen.P("github.com/other/types") // auto-aliased to "types2"
types.Type("User") // => types.User
other.Type("User") // => types2.User
func (*Generator) PAlias ¶
func (g *Generator) PAlias(importPath, alias string) *PackageRef
PAlias returns a PackageRef with a specific alias. Use this when you want to control the alias name explicitly.
Example:
ctx := gen.PAlias("context", "ctx")
ctx.Type("Context") // => ctx.Context
func (*Generator) PackageName ¶
PackageName returns the current package name.
func (*Generator) SetHeader ¶
SetHeader sets a header comment that appears before the package declaration. Typically used for "Code generated by X. DO NOT EDIT." comments.
func (*Generator) SetPackage ¶
SetPackage sets the package name for the generated file.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
func NewGroup ¶
func NewGroup() *Group
Example ¶
f := NewGroup()
f.AddPackage("main")
f.NewImport().AddPath("fmt")
f.NewFunction("main").AddBody(
String(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
Output: package main import "fmt" func main(){ fmt.Println("Hello, World!")}
func NewInlineGroup ¶
func NewInlineGroup() *Group
NewInlineGroup creates a group with no separators between elements. Use this when you need to combine multiple nodes into a single line.
Example:
gg.NewInlineGroup().Append(
gg.S("tn := "),
gsqlPkg.Call("TableName", gg.Lit("tableName")),
)
// => tn := gsqlPkg.TableName("tableName")
func (*Group) AddLineComment ¶
func (*Group) AddPackage ¶
func (*Group) AddTypeAlias ¶
func (*Group) AppendFile
deprecated
func (*Group) NewFunction ¶
func (*Group) NewInterface ¶
type PackageRef ¶
type PackageRef struct {
// contains filtered or unexported fields
}
PackageRef represents a reference to an external package. It provides methods to generate qualified identifiers and automatically manages import aliases to avoid conflicts.
func (*PackageRef) Alias ¶
func (p *PackageRef) Alias() string
Alias returns the resolved alias for this package.
func (*PackageRef) Call ¶
func (p *PackageRef) Call(name string, args ...any) *icall
Call returns a qualified function call. Example: types.NewUser("name")
func (*PackageRef) Chan ¶
func (p *PackageRef) Chan(name string) Node
Chan returns a channel type of the given type name. Example: chan types.Event
func (*PackageRef) ChanRecv ¶
func (p *PackageRef) ChanRecv(name string) Node
ChanRecv returns a receive-only channel type. Example: <-chan types.Event
func (*PackageRef) ChanSend ¶
func (p *PackageRef) ChanSend(name string) Node
ChanSend returns a send-only channel type. Example: chan<- types.Event
func (*PackageRef) Dot ¶
func (p *PackageRef) Dot(name string) Node
Dot returns a qualified identifier node (for constants, variables, etc.). Example: types.DefaultTimeout
func (*PackageRef) Func ¶
func (p *PackageRef) Func(name string) Node
Func returns a qualified function reference (without calling it). Example: types.ParseConfig
func (*PackageRef) Generic ¶
func (p *PackageRef) Generic(name string, typeArgs ...any) Node
Generic returns a generic type instantiation. Example:
types.Generic("List", "string") // types.List[string]
types.Generic("Map", "string", types.Type("User")) // types.Map[string, types.User]
func (*PackageRef) ImportPath ¶
func (p *PackageRef) ImportPath() string
ImportPath returns the full import path of the package.
func (*PackageRef) Map ¶
func (p *PackageRef) Map(keyType any, valueType any) Node
Map returns a map type with the given key and value types. Both key and value can be: string (type name), Node, or *PackageRef method result. Example:
types.Map("string", "User") // map[string]types.User
types.Map(other.Type("Key"), "Value") // map[other.Key]types.Value
Map(types.Type("Key"), types.Type("Value")) // map[types.Key]types.Value (use package-level Map)
func (*PackageRef) Ptr ¶
func (p *PackageRef) Ptr(name string) Node
Ptr returns a pointer type of the given type name. Example: *types.User
func (*PackageRef) Slice ¶
func (p *PackageRef) Slice(name string) Node
Slice returns a slice type of the given type name. Example: []types.User
func (*PackageRef) Type ¶
func (p *PackageRef) Type(name string) Node
Type returns a qualified type name node. Example: types.User