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
- Variables
- func AddressOf(x string) string
- func And(conds ...string) string
- func Call(fn string, args ...string) string
- func CanQualify(expr string) bool
- func CheckDecls(name string, src []byte) error
- func Comment(text string) string
- func Composite(typ string, elems []KeyValue) string
- func Define(names []string, values ...string) string
- func Deref(x string) string
- func Duration(d time.Duration, timePkg string) string
- func Equal(x, y string) string
- func Format(src []byte) ([]byte, error)
- func Get(x, value, ok string) string
- func ImportName(path, alias string) string
- func Index(x, key string) string
- func IsNil(x string) string
- func Leaf(t gomodel.Type) gomodel.Type
- func Literal(v spec.Value) string
- func NotEmpty(x string) string
- func NotNil(x string) string
- func NotZero(x, zero, isZero string) string
- func Or(conds ...string) string
- func Param(name, typ string) string
- func Qualify(expr, pkg string) string
- func Quote(s string) string
- func RawString(s string) string
- func Return(values ...string) string
- func Selector(x, name string) string
- func Signature(params []string, result string) string
- func Tag(tags []gomodel.Tag) string
- func Text(t gomodel.Type) string
- func Zero(t gomodel.Type) string
- type ImportSet
- func (s *ImportSet) Add(path, want string) string
- func (s *ImportSet) Decl() string
- func (s *ImportSet) DropUnused(src []byte, paths []string)
- func (s *ImportSet) Has(path string) bool
- func (s *ImportSet) Idle() []string
- func (s *ImportSet) Name(path string) (string, bool)
- func (s *ImportSet) Offer(path, want string)
- func (s *ImportSet) Paths() []string
- func (s *ImportSet) Take(src []byte)
- type KeyValue
- type Scope
- func (s *Scope) Expr(t gomodel.Type) string
- func (s *Scope) Import(imp gomodel.Import) string
- func (s *Scope) Qualified(expr string, imp gomodel.Import) string
- func (s *Scope) RuntimeGuard() string
- func (s *Scope) Symbol(part layout.PartID, name string) string
- func (s *Scope) Value(t gomodel.Type, v spec.Value) string
Constants ¶
const CommentWidth = 100
CommentWidth is the column Comment wraps lines at.
Variables ¶
var ( ErrFormat = errors.New("format generated code") ErrParse = errors.New("parse generated code") )
Functions ¶
func CanQualify ¶
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 ¶
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 ¶
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 Duration ¶
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 Format ¶
Format runs go/format on src. On failure it returns src unchanged and an error that shows the lines around the first problem.
func ImportName ¶
ImportName is the name path is imported under: alias when given, else the package name guessed from the path.
func Leaf ¶
Leaf is the type inside the pointers, slices and maps of t: t itself when it holds none.
func NotZero ¶
NotZero writes x != zero, x when zero is false, or !isZero(x) when zero, a literal, is empty.
func Qualify ¶
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 RawString ¶
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 Signature ¶
Signature writes the parameters of a func and its result, when it has one: (params) result.
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 ¶
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 ¶
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 ¶
DropUnused removes each of paths whose name src, declarations of the file, does not refer to.
func (*ImportSet) Idle ¶
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 ¶
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 ¶
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.
type KeyValue ¶
KeyValue is one element of a keyed composite literal, its key and value written as Go.
type Scope ¶
Scope is the file code is written into. It qualifies types declared in other packages and records the imports they need.
func (*Scope) Qualified ¶
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 ¶
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.