Documentation
¶
Index ¶
- func DisambiguateName(name string) iter.Seq[string]
- func Errorf(pkger Pkger, poser Poser, format string, args ...any) error
- func FormatExpr(pkger Pkger, expr ast.Expr) string
- func FormatObj(pkger Pkger, obj types.Object) string
- func FormatPos(pkger Pkger, pos token.Pos) string
- func FormatPosition(pos token.Position) string
- func FormatSig(pkger Pkger, sig *types.Signature) string
- func FormatType(pkger Pkger, typ types.Type) string
- func FormatTypeParen(pkger Pkger, typ types.Type) string
- func Fprintf(pkger Pkger, w io.Writer, format string, args ...any) (int, error)
- func NormalizeName(name string) string
- func RewriteImports[T ast.Node](w *Writer, node T) T
- func Sprintf(pkger Pkger, format string, args ...any) string
- type CodeError
- type Ender
- type Exprer
- type Formatter
- func (f Formatter) Errorf(poser Poser, format string, args ...any) error
- func (f Formatter) Expr(expr ast.Expr) string
- func (f Formatter) Fprintf(w io.Writer, format string, args ...any) (int, error)
- func (f Formatter) Obj(obj types.Object) string
- func (f Formatter) Pos(pos token.Pos) string
- func (f Formatter) Sig(sig *types.Signature) string
- func (f Formatter) Sprintf(format string, args ...any) string
- func (f Formatter) Type(typ types.Type) string
- func (f Formatter) TypeParen(typ types.Type) string
- type Import
- type NS
- type Objecter
- type Pkger
- type Poser
- type TypeInfoTyper
- type TypeInfoer
- type Typer
- type Writer
- func (w *Writer) Import(path, name string) string
- func (w *Writer) Imports() map[string]Import
- func (w *Writer) Name(name string) string
- func (w *Writer) Printf(format string, args ...any) (int, error)
- func (w *Writer) Reserve(name string) bool
- func (w *Writer) Sprintf(format string, args ...any) string
- func (w *Writer) WithBuf(buf io.Writer) *Writer
- func (w *Writer) WithNS(ns NS) *Writer
- func (w *Writer) Write(p []byte) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisambiguateName ¶
DisambiguateName offers an alternative unique names.
func FormatExpr ¶
FormatExpr is a shorthand for Formatter.Expr.
func FormatObj ¶
FormatObj is a shorthand for Formatter.Obj.
func FormatPos ¶
FormatPos is a shorthand for Formatter.Pos.
func FormatPosition ¶
func FormatSig ¶
FormatSig is a shorthand for Formatter.Sig.
func FormatType ¶
FormatType is a shorthand for Formatter.Type.
func FormatTypeParen ¶
FormatTypeParen is a shorthand for Formatter.TypeParen.
func NormalizeName ¶
func RewriteImports ¶
RewriteImports modifies the given AST node to rewrite imported package names to ensure there is no name conflict.
Types ¶
type CodeError ¶
type CodeError struct {
// contains filtered or unexported fields
}
CodeError indicates where the error occurred in user's source code.
func (CodeError) Error ¶
Error implements the error interface. If pos is valid, the position is prepended to the error message.
type Formatter ¶
Formatter formats types, objects, expressions, and positions.
func (Formatter) Errorf ¶
Errorf formats an error message. The error will indicate the position in the source code if the position is valid.
func (Formatter) Obj ¶
Obj returns a code string to refer the given object.
e.g., f.Obj([types.Object for strconv.Atoi]) => "strconv.Atoi"
func (Formatter) Sig ¶
Sig returns a compact string representation of the given function signature without "func" keyword, receiver, and parameter names.
e.g., f.Sig([*types.Signature of strconv.Atoi) => "(string) (int, error)"
type NS ¶
type NS map[string]struct{}
NS manages unique names in a namespace.
type TypeInfoTyper ¶
type TypeInfoer ¶
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is a writer for generated code.
func NewWriter ¶
NewWriter creates a new Writer. It does not initialize the namespace. To specify a namespace, use [SetNamespace].
func (*Writer) Import ¶
Import adds an import for the package with the given path and alias. It returns the name of the imported package. The name might be different if it has tried to resolve name conflicts.
// fmtName can be used to refer to the "fmt" package without any name conflict.
fmtName := w.Import("fmt", "fmt")
w.Printf("%s.Println(\"Hello, World!\")", fmtName)
When calling it, the package to import is recorded. Call [Imports] to retrieve them.
func (*Writer) Imports ¶
Imports returns the collected imports. Imports are collected by [Ref] and Type.
func (*Writer) Printf ¶
Printf writes a formatted string to the underlying writer using [CodeFormatter.Fprintf].