gen

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package gen provides functions for generating go source code

The gen package provides wrapper functions around the go/ast and go/token packages to reduce boilerplate.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommentGroup

func CommentGroup(comments ...string) *ast.CommentGroup

CommentGroup creates a comment group from strings.

func ConstChar

func ConstChar(args ...string) *ast.GenDecl

ConstChar creates a series of character const declarations from the name/value pairs in args.

func ConstFloat

func ConstFloat(args ...string) *ast.GenDecl

ConstFloat creates a series of floating-point const declarations from the name/value pairs in args.

func ConstImaginary

func ConstImaginary(args ...string) *ast.GenDecl

ConstImaginary creates a series of imaginary const declarations from the name/value pairs in args.

func ConstInt

func ConstInt(args ...string) *ast.GenDecl

ConstInt creates a series of numeric const declarations from the name/value pairs in args.

func ConstString

func ConstString(args ...string) *ast.GenDecl

ConstString creates a series of string const declarations from the name/value pairs in args.

func Declarations

func Declarations(blocks ...string) ([]ast.Decl, error)

Declarations parses a list of Go source code blocks and converts them into *ast.Decl values. If a parsing error occurs, it is returned immediately and no further parsing takes place.

func ExprString

func ExprString(expr ast.Expr) string

ExprString converts an ast.Expr to the Go source it represents.

func FieldList

func FieldList(fields ...string) (*ast.FieldList, error)

FieldList generates a field list from strings in the form "[name] expr".

func FormattedSource

func FormattedSource(file *ast.File, output string) ([]byte, error)

FormattedSource converts an abstract syntax tree to formatted Go source code.

func PackageDoc

func PackageDoc(file *ast.File, comments ...string) *ast.File

PackageDoc inserts package-level comments into a file, preceding the "package" statement.

func Public

func Public(name string) *ast.Ident

Public turns a string into a public (uppercase) identifier.

func Sanitize

func Sanitize(name string) string

Sanitize modifies any names that are reserved in Go, so that they may be used as identifiers without causing a syntax error.

func SimpleType

func SimpleType(name string) ast.Expr

SimpleType creates an identifier suitable for use as a type expression.

func Snippets

func Snippets(val interface{}, snippets ...string) ([]ast.Decl, error)

Snippets evaluates zero or more input templates with dot set to val and parses them as Go source code.

func String

func String(s string) *ast.BasicLit

String generates a literal string. If the string contains a double quote, backticks are used for quoting instead.

func Struct

func Struct(args ...ast.Expr) *ast.StructType

Struct creates a struct{} expression. The arguments are a series of name/type/tag tuples. Name must be of type *ast.Ident, type must be of type ast.Expr, and tag must be of type *ast.BasicLit, The number of arguments must be a multiple of 3, or a run-time panic will occur.

func TagKey

func TagKey(field *ast.Field, key string) string

TagKey gets the struct tag item with the given key.

func ToString

func ToString(expr ast.Expr) (string, error)

ToString converts the expression to a Go source string.

func TypeDecl

func TypeDecl(name *ast.Ident, typ ast.Expr) *ast.GenDecl

TypeDecl generates a type declaration with the given name.

Types

type Function

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

func Func

func Func(name string) *Function

func (*Function) Args

func (fn *Function) Args(args ...string) *Function

Args sets the arguments that a function takes.

func (*Function) Body

func (fn *Function) Body(format string, v ...interface{}) *Function

Body sets the body of a function. The body should not include enclosing braces.

func (*Function) BodyTmpl

func (fn *Function) BodyTmpl(tmpl string, dot interface{}) *Function

BodyTmpl allows use of the text/template package to construct the body of a function.

func (*Function) Comment

func (fn *Function) Comment(s string) *Function

Comment sets the Godoc comments for the function.

func (*Function) Decl

func (fn *Function) Decl() (*ast.FuncDecl, error)

Decl generates Go source for a Func. an error is returned if the body, or parameters cannot be parsed.

func (*Function) MustDecl

func (fn *Function) MustDecl() *ast.FuncDecl

MustDecl is like Decl, but panics if an error is returned.

func (*Function) Name

func (fn *Function) Name() string

Name returns the name of the function.

func (*Function) Receiver

func (fn *Function) Receiver(receiver string) *Function

Receiver turns the function into a method operating on the specified type.

func (*Function) Returns

func (fn *Function) Returns(values ...string) *Function

Returns sets the return values of a function. Each return value should be a string matching the Go syntax for a single return value.

Jump to

Keyboard shortcuts

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