gocode

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package gocode writes Go source text: type expressions as a given file spells them, imports, comments, tags and literals, and formats the result. No other package builds Go code as text.

Index

Constants

View Source
const CommentWidth = 100

CommentWidth is the column Comment wraps lines at.

Variables

View Source
var (
	ErrFormat = errors.New("format generated code")
	ErrParse  = errors.New("parse generated code")
)

Functions

func AddressOf

func AddressOf(x string) string

AddressOf writes &x.

func And

func And(conds ...string) string

And writes the conditions joined by &&, or true when there are none.

func Call

func Call(fn string, args ...string) string

Call writes fn(args...).

func CanQualify

func CanQualify(expr string) bool

CanQualify reports whether Qualify writes expr right: expr is an identifier, or pointers, slices, arrays, maps and channels around one, and has nothing before the type or after the identifier.

func CheckDecls

func CheckDecls(name string, src []byte) error

CheckDecls returns an error when src is no list of Go declarations, as a file holds them below its package clause. name stands for src in the error, which shows the lines around the first problem.

func Comment

func Comment(text string) string

Comment turns text into // lines at most 100 columns wide. Long lines wrap at spaces; the line breaks, indentation and spacing of text stay, runs of blank lines become one. Characters Go source cannot hold are dropped. Blank text gives "".

func Composite

func Composite(typ string, elems []KeyValue) string

Composite writes typ{key: value}, one element per line when it has several.

func Define

func Define(names []string, values ...string) string

Define writes names := values, one value per name.

func Deref

func Deref(x string) string

Deref writes *x.

func Duration

func Duration(d time.Duration, timePkg string) string

Duration writes d in the largest unit that holds it whole, such as 30 * time.Second, where timePkg is the name the time package is imported under.

func Equal

func Equal(x, y string) string

Equal writes x == y.

func Format

func Format(src []byte) ([]byte, error)

Format runs go/format on src. On failure it returns src unchanged and an error that shows the lines around the first problem.

func Get

func Get(x, value, ok string) string

Get writes value, ok := x.Get(); ok, which holds when the Nullable x holds a value.

func ImportName

func ImportName(path, alias string) string

ImportName is the name path is imported under: alias when given, else the package name guessed from the path.

func Index

func Index(x, key string) string

Index writes x[key].

func IsNil

func IsNil(x string) string

IsNil writes x == nil.

func Leaf

func Leaf(t gomodel.Type) gomodel.Type

Leaf is the type inside the pointers, slices and maps of t: t itself when it holds none.

func Literal

func Literal(v spec.Value) string

Literal returns v as a Go constant. Numbers keep the form the spec wrote them in.

func NotEmpty

func NotEmpty(x string) string

NotEmpty writes x != "".

func NotNil

func NotNil(x string) string

NotNil writes x != nil.

func NotZero

func NotZero(x, zero, isZero string) string

NotZero writes x != zero, x when zero is false, or !isZero(x) when zero, a literal, is empty.

func Or

func Or(conds ...string) string

Or writes the conditions joined by ||.

func Param

func Param(name, typ string) string

Param writes one parameter of a func: name typ.

func Qualify

func Qualify(expr, pkg string) string

Qualify writes expr, a type expression whose trailing identifier is declared in package pkg, as another package writes it: []Pet with models gives []models.Pet. An empty pkg leaves expr as it is.

func Quote

func Quote(s string) string

Quote returns s as a Go string literal.

func RawString

func RawString(s string) string

RawString returns s as a raw string literal, or a quoted one when s holds a backquote or a character a raw literal cannot show.

func Return

func Return(values ...string) string

Return writes return, with the values when there are any.

func Selector

func Selector(x, name string) string

Selector writes x.name.

func Signature

func Signature(params []string, result string) string

Signature writes the parameters of a func and its result, when it has one: (params) result.

func Tag

func Tag(tags []gomodel.Tag) string

Tag returns a struct tag literal, raw unless a value holds a backquote. No tags give "".

func Text

func Text(t gomodel.Type) string

Text returns t as the package that declares its named type writes it: never qualified. A nil type is any.

func Zero

func Zero(t gomodel.Type) string

Zero writes the zero value of t, a type whose underlying type is a string or one that can be nil: "" or nil.

Types

type ImportSet

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

ImportSet holds the imports of one file: the name each is used under, and the ones under _ or . that have none. A path on offer holds its name, and the file does not import it yet; it is denied when another path asked for that name and got a number.

func NewImportSet

func NewImportSet() *ImportSet

func (*ImportSet) Add

func (s *ImportSet) Add(path, want string) string

Add imports path and returns the name code uses for it. want is the preferred name; empty means the package name guessed from the path. A name another path holds gets the first free number. With _ or . as want the import takes no name and want comes back.

func (*ImportSet) Decl

func (s *ImportSet) Decl() string

Decl returns the import declaration: the standard library first, then the rest, each group sorted by path. It is empty when nothing is imported.

func (*ImportSet) DropUnused

func (s *ImportSet) DropUnused(src []byte, paths []string)

DropUnused removes each of paths whose name src, declarations of the file, does not refer to.

func (*ImportSet) Has

func (s *ImportSet) Has(path string) bool

Has reports whether path is imported, under a name or without one.

func (*ImportSet) Idle

func (s *ImportSet) Idle() []string

Idle lists, sorted, the paths that are still on offer and denied their name to another path: without them the file would name its imports otherwise.

func (*ImportSet) Name

func (s *ImportSet) Name(path string) (string, bool)

Name is the name path is imported under. It has none when the file imports it under _ or . alone, has it on offer, or does not import it.

func (*ImportSet) Offer

func (s *ImportSet) Offer(path, want string)

Offer holds for path the name Add would give it, for code the file may get from elsewhere. The file imports path once Add asks for it or Take finds the name in that code. With _ or . as want the path is imported right away.

func (*ImportSet) Paths

func (s *ImportSet) Paths() []string

Paths lists what is imported, sorted.

func (*ImportSet) Take

func (s *ImportSet) Take(src []byte)

Take imports every path on offer whose name src, declarations of the file, refers to.

type KeyValue

type KeyValue struct {
	Key   string
	Value string
}

KeyValue is one element of a keyed composite literal, its key and value written as Go.

type Scope

type Scope struct {
	File    *layout.File
	Layout  *layout.Layout
	Imports *ImportSet
}

Scope is the file code is written into. It qualifies types declared in other packages and records the imports they need.

func NewScope

func NewScope(f *layout.File, l *layout.Layout) *Scope

func (*Scope) Expr

func (s *Scope) Expr(t gomodel.Type) string

Expr returns t as code in the scope's file writes it. A nil type is any.

func (*Scope) Import

func (s *Scope) Import(imp gomodel.Import) string

Import adds imp to the file and returns the name to qualify with.

func (*Scope) Qualified

func (s *Scope) Qualified(expr string, imp gomodel.Import) string

Qualified returns expr, a type expression whose trailing identifier is declared in the package imp, as the scope's file writes it: plain in that package or without one, else qualified with the name the file imports imp under.

func (*Scope) RuntimeGuard

func (s *Scope) RuntimeGuard() string

RuntimeGuard returns the constant a file that imports the runtime or one of its packages refers to, so a runtime too old or too new for the file fails to compile. A file that imports only one of its packages imports the runtime for it. It is empty when the file imports none of them under a name: its code then uses nothing of the runtime.

func (*Scope) Symbol

func (s *Scope) Symbol(part layout.PartID, name string) string

Symbol returns name, declared in the file that holds part, as the scope's file writes it.

func (*Scope) Value

func (s *Scope) Value(t gomodel.Type, v spec.Value) string

Value returns v as a value of type t: an enum constant, a list, or a constant.

Jump to

Keyboard shortcuts

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