Skip to content

thorsphere/lpcode

lpcode

Go Report Card CodeFactor OSS Lifecycle

PkgGoDev GitHub go.mod Go version Libraries.io dependency status for GitHub repo

GitHub release (latest by date) GitHub last commit GitHub commit activity GitHub code size in bytes GitHub Top Language GitHub

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"

A Codefile represents a code file with an interface that provides methods to start, write and finish the code file. A utility for managing the lifecycle of code 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 the 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 using 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) Testvariables(t *Testvars) *Code
func (code *Code) Format() error
func (code *Code) String() string

Example

Links

Godoc

Go Report Card

Open Source Insights

About

Go package to generate Go code

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors

Languages