lpcode



Go package for programmatic Go code generation that tries to keep it simple (KISS principle). The package is designed primarily to support the tserrgen code generator.
- Simple: A simple and fluent API with method chaining
- Formatted: Automatic gofmt integration for clean output
- Tested: Well-tested with unit tests with high code coverage
- Dependencies: Minimal dependencies and only depends on Go Standard Library, tsfio and tserr
Usage
In the Go app, the package is imported with
import "github.com/thorsphere/lpcode"
Codefile manages the lifecycle of a code file. StartFile initializes the file with a header template, WriteCode writes generated code content, and FinishFile appends a footer template and formats the result. Header and footer files (with suffixes ".header" and ".footer") are required and are searched for in the codefile directory or the current directory. Header and footer files allow you to include package declarations, imports, licenses, or other boilerplate that should appear at the beginning or end of generated files.
func NewCodefile(dn tsfio.Directory, fn tsfio.Filename) (*Codefile, error)
func (cf *Codefile) StartFile() error
func (cf *Codefile) WriteCode(c *Code) error
func (cf *Codefile) FinishFile() error
There are helper functions for formatting, retrieving the code file filepath and for retrieving its contents as a string.
func (cf *Codefile) Format() error
func (cf *Codefile) Filepath() tsfio.Filename
func (cf *Codefile) String() string
A Code contains source code as string. A builder for constructing Go source code strings using method chaining. The Code type offers a range of methods for generating common Go syntax. The stored source code is amended by calling its methods. The source code can be retrieved with String and formatted with Format.
func NewCode() *Code
func (code *Code) LineComment(c string) *Code
func (code *Code) FuncEnd() *Code
func (code *Code) BlockEnd() *Code
func (code *Code) Call(n string) *Code
func (code *Code) ParamEndln() *Code
func (code *Code) ParamEnd() *Code
func (code *Code) Func1(a *Func1Args) *Code
func (code *Code) TypeStruct(n string) *Code
func (code *Code) VarSpec(a *VarSpecArgs) *Code
func (code *Code) List() *Code
func (code *Code) Listln() *Code
func (code *Code) SelField(a *SelArgs) *Code
func (code *Code) SelMethod(a *SelArgs) *Code
func (code *Code) If(a *IfArgs) *Code
func (code *Code) IfErr(a *IfErrArgs) *Code
func (code *Code) Return() *Code
func (code *Code) Addr() *Code
func (code *Code) Ident(n string) *Code
func (code *Code) Assignment(a *AssignmentArgs) *Code
func (code *Code) CompositeLit(LiteralType string) *Code
func (code *Code) ShortVarDecl(a *ShortVarDeclArgs) *Code
func (code *Code) KeyedElement(a *KeyedElementArgs) *Code
func (code *Code) TestVarDecl(t *Testvars) *Code
func (code *Code) Format() error
func (code *Code) String() string
Example
package main
import (
"fmt"
"github.com/thorsphere/lpcode"
)
func main() {
c := lpcode.NewCode().
TypeStruct("foo").
VarSpec(&lpcode.VarSpecArgs{Ident: "A", Type: "int"}).
VarSpec(&lpcode.VarSpecArgs{Ident: "B", Type: "string"}).BlockEnd().
Func1(&lpcode.Func1Args{Name: "Example", Var: "x", Type: "int", Return: "error"}).
IfErr(&lpcode.IfErrArgs{Method: "doSomething()", Operator: "!="}).Return().Ident("err").BlockEnd().
If(&lpcode.IfArgs{ExprLeft: "x", Operator: ">", ExprRight: "10"}).
CompositeLit("foo").
KeyedElement(&lpcode.KeyedElementArgs{Key: "A", Elem: "1"}).
KeyedElement(&lpcode.KeyedElementArgs{Key: `B`, Elem: `"hello"`}).
BlockEnd().BlockEnd().Return().Ident("nil").FuncEnd()
c.Format()
fmt.Print(c)
}
Run in Go Playground
Output:
type foo struct {
A int
B string
}
func Example(x int) error {
if err := doSomething(); err != nil {
return err
}
if x > 10 {
foo{A: 1,
B: "hello",
}
}
return nil
}
Limitations
It does not perform syntax validation; the generated code should be validated by go/format or other tools.
Links
Godoc
Go Report Card
Open Source Insights