lpcode

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

README

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

Godoc

Go Report Card

Open Source Insights

Documentation

Overview

Package lpcode provides a simple and fluent API for programmatic Go code generation. It consists of two main components:

  • Code: A builder for constructing Go source code strings using method chaining. The Code type offers a wide range of methods for generating common Go syntax including function declarations, struct types, control flow statements, variable declarations, composite literals, and more. It supports formatting via go/format.

  • Codefile: A utility for managing the lifecycle of code files. It handles creating new files, appending header/footer templates, writing Code content, and formatting the final output. It integrates with the tsfio package for file operations and tserr for consistent error handling.

The package is designed primarily to support the tserrgen code generator but can be used for other code generation tasks. It does not perform syntax validation; the generated code should be validated by go/format or other tools.

Copyright (c) 2023-2026 thorsphere. All Rights Reserved. Use is governed with GNU Affero General Public License v3.0 that can be found in the LICENSE file.

Copyright (c) 2023-2026 thorsphere. All Rights Reserved. Use is governed with GNU Affero General Public License v3.0 that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssignmentArgs

type AssignmentArgs struct {
	ExprLeft, ExprRight string
}

AssignmentArgs contains the left and right hand side expressions to generate an assignment with Assignment

type Code

type Code struct {
	// contains filtered or unexported fields
}

Code contains the source code as string. The stored source code is amended by using its methods. The source code can be retrieved with String and formatted with Format.

func NewCode

func NewCode() *Code

NewCode returns a pointer to a new Code instance. The source code in the new Code instance is empty.

func (*Code) Addr

func (code *Code) Addr() *Code

Addr adds an address operator to code: &. It returns nil if code is nil.

func (*Code) Assignment

func (code *Code) Assignment(a *AssignmentArgs) *Code

Assignment adds an assignment to code: ExprLeft = ExprRight. The left and right hand side expressions are provided by a. It returns nil if code is nil.

func (*Code) BlockEnd

func (code *Code) BlockEnd() *Code

BlockEnd adds a block ending to code: }\n.

func (*Code) Call

func (code *Code) Call(n string) *Code

Call adds a function call to code: n(. The function name is provided by n.

func (*Code) CompositeLit

func (code *Code) CompositeLit(LiteralType string) *Code

CompositeLit adds the beginning of a composite literal to code: LiteralType{\n. The literal type is provided by argument LiteralType.

func (*Code) Format

func (code *Code) Format() error

Format formats the source code in code in canonical gfmt style. It uses Source from the go/format package. Format returns an error if code is nil or if go/format returns an error.

func (*Code) Func1

func (code *Code) Func1(a *Func1Args) *Code

Func1 adds a function declaration with one parameter to code: func Name(Var Type) Return {\n. The function name, parameter variable, parameter type and return type are provided by a.

func (*Code) FuncEnd

func (code *Code) FuncEnd() *Code

FuncEnd adds a block end and two new lines to code: }\n\n.

func (*Code) Ident

func (code *Code) Ident(n string) *Code

Ident adds an identifier to code: n. The identifier is provided by argument n.

func (*Code) If

func (code *Code) If(a *IfArgs) *Code

If adds an if statement to code: if ExprLeft Operator ExprRight {\n. The left and right hand side expressions and the operator are provided by a. It returns nil if a is nil.

func (*Code) IfErr

func (code *Code) IfErr(a *IfErrArgs) *Code

IfErr adds an if statement for error handling to code: if err := Method; err Operator nil {\n. The method and operator are provided by a. It returns nil if a is nil.

func (*Code) KeyedElement

func (code *Code) KeyedElement(a *KeyedElementArgs) *Code

KeyedElement generates a keyed element of a composite literal: Key: Element,\n. The key and element is provided by a. It returns nil if a is nil.

func (*Code) LineComment

func (code *Code) LineComment(c string) *Code

LineComment adds a line comment and a new line to code: // c\n. The comment is provided by argument c.

func (*Code) List

func (code *Code) List() *Code

List adds an identifier list to code: , .

func (*Code) Listln

func (code *Code) Listln() *Code

Listln adds an identifier list and a new line to code: ,\n.

func (*Code) ParamEnd

func (code *Code) ParamEnd() *Code

ParamEnd adds a parameters ending to code: ).

func (*Code) ParamEndln

func (code *Code) ParamEndln() *Code

ParamEndln adds a parameters ending and a new line to code: )\n.

func (*Code) Return

func (code *Code) Return() *Code

Return adds the return statement to code: return . It returns nil if code is nil.

func (*Code) SelField

func (code *Code) SelField(a *SelArgs) *Code

SelField adds a field selector to code: val.sel. The value val and selector sel are provided by a. It returns nil if a is nil.

func (*Code) SelMethod

func (code *Code) SelMethod(a *SelArgs) *Code

SelMethod adds a method selector to code: val.sel(. The value val and selector sel are provided by a. It returns nil if a is nil.

func (*Code) ShortVarDecl

func (code *Code) ShortVarDecl(a *ShortVarDeclArgs) *Code

ShortVarDecl generates a short variable declaration: Ident := Expr\n. The identifier and expression is provided by a. It returns nil if a is nil.

func (*Code) String

func (code *Code) String() string

String returns the source code in code as string. It returns an empty string if code is nil.

func (*Code) Testvariables

func (code *Code) Testvariables(t *Testvars) *Code

Testvariables generates test variables for unit tests. The test variables are generated based on t. A test variable is generated if the corresponding type in t is not equal to zero. It returns nil if t is nil.

func (*Code) TypeStruct

func (code *Code) TypeStruct(n string) *Code

TypeStruct adds a type declaration for a struct type to code: type n struct {\n. The name of the type is provided with n.

func (*Code) VarSpec

func (code *Code) VarSpec(a *VarSpecArgs) *Code

VarSpec adds a variable specification to code: Ident Type\n. The identifier and type is provided by a.

type Codefile

type Codefile struct {
	// contains filtered or unexported fields
}

Codefile represents a code file with a filename fn and filepath fp. It provides methods to start, write and finish the code file.

func NewCodefile

func NewCodefile(dn tsfio.Directory, fn tsfio.Filename) (*Codefile, error)

NewCodefile creates a new Codefile with the given directory name dn and filename fn. It returns a pointer to the created Codefile and an error if any occurs during the creation process otherwise nil.

func (*Codefile) Filepath

func (cf *Codefile) Filepath() tsfio.Filename

Filepath returns the file path of the Codefile. It returns an empty string if the Codefile is nil.

func (*Codefile) FinishFile

func (cf *Codefile) FinishFile() error

FinishFile finishes the code file by appending the footer file and formatting the code file. It returns an error if any occurs during the process otherwise nil.

func (*Codefile) Format

func (cf *Codefile) Format() error

Format formats the code file using the go/format package. It returns an error if any occurs during the formatting process otherwise nil.

func (*Codefile) StartFile

func (cf *Codefile) StartFile() error

StartFile starts the code file by resetting the file and appending the header file. It returns an error if any occurs during the process otherwise nil.

func (*Codefile) String

func (cf *Codefile) String() string

String returns the contents of the code file as a string. It returns an empty string if the Codefile is nil or if any error occurs during reading the file.

func (*Codefile) WriteCode

func (cf *Codefile) WriteCode(c *Code) error

WriteCode writes the source code of c to the code file. It returns an error if any occurs during the process otherwise nil.

type Func1Args

type Func1Args struct {
	Name, Var, Type, Return string
}

Func1Args contains the configuration for the generation of a function declaration with Func1.

type IfArgs

type IfArgs struct {
	ExprLeft, ExprRight, Operator string
}

IfArgs contains the left and right hand side expressions and the operator to generate an if statement with If.

type IfErrArgs

type IfErrArgs struct {
	Method, Operator string
}

type KeyedElementArgs

type KeyedElementArgs struct {
	Key, Elem string
}

KeyedElementArgs contains the key Key and element Elem to generate a keyed element of a composite literal with KeyedElement

type SelArgs

type SelArgs struct {
	Val, Sel string
}

type ShortVarDeclArgs

type ShortVarDeclArgs struct {
	Ident, Expr string
}

ShortVarDeclArgs contains the identifier Ident and expression Expr to generate a short variable declaration with ShortVarDecl

type Testvars

type Testvars struct {
	String, Error, Int, Float int
}

Testvars contains the configuration for the generation of test variables with Testvariables A test variable is generated if the corresponding type is not equal to zero.

type VarSpecArgs

type VarSpecArgs struct {
	Ident, Type string
}

VarSpecArgs contains the identifier Ident and type Type to generate a variable specification with VarSpec

Jump to

Keyboard shortcuts

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