generator

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const GoExt string = ".go"

GoExt is the extension for Go files.

Variables

This section is empty.

Functions

func GetPkgName added in v0.1.4

func GetPkgName(loc string) (string, error)

GetPkgName returns the package name of the given file.

Parameters:

  • loc: The location of the file. This must be a Go file.

Returns:

  • string: The package name of the file.
  • error: An error if getting the package name failed.

func NewLogger

func NewLogger(name string) *log.Logger

NewLogger creates a new logger with the given name.

If the name is empty, it defaults to "generator".

Parameters:

  • name: The name of the logger.

Returns:

  • *log.Logger: The logger. Never returns nil.

Types

type CodeGenerator added in v0.1.4

type CodeGenerator[T PkgNameSetter] struct {
	// contains filtered or unexported fields
}

CodeGenerator is a template-based code generator.

func Must added in v0.1.4

func Must[T PkgNameSetter](cg *CodeGenerator[T], err error) *CodeGenerator[T]

Must is a helper function that wraps a call to a function that returns (*CodeGenerator[T], error) and panics if the error is not nil or if the CodeGenerator is nil.

This function is intended to be used to handle errors in a way that is easy to read and write.

Example usage:

cg = Must(NewCodeGenerator("example", "package {{.PkgName}}\n\nfunc Example() { ... }"))

func New added in v0.1.4

func New[T PkgNameSetter](name, templ string) (*CodeGenerator[T], error)

New creates a new instance of CodeGenerator with a parsed template.

Parameters:

  • name: The name of the template.
  • templ: The template string to be parsed.

Returns:

  • *CodeGenerator[T]: A pointer to the newly created CodeGenerator.
  • error: An error if the template parsing fails.

func (CodeGenerator[T]) Generate added in v0.1.4

func (c CodeGenerator[T]) Generate(can_edit bool, sign, loc string, data T) error

Generate generates code into the given location with the given data.

Parameters:

  • can_edit: If true, the generated code will have a comment indicating that it can be edited.
  • sign: The signature of the code generator.
  • loc: The location to write the generated code to.
  • data: The data to pass to the template.

Returns:

  • error: An error if the template execution fails or if writing the file fails.

type PkgNameSetter added in v0.1.4

type PkgNameSetter interface {
	// SetPkgName sets the package name of the receiver to the given value.
	//
	// Parameters:
	//   - pkg_name: The package name to set.
	//
	// Returns:
	//   - error: Returns an error if the receiver is nil.
	SetPkgName(pkg_name string) error
}

PkgNameSetter is an interface that can set the package name.

Jump to

Keyboard shortcuts

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