Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var GoFormatter = FormatterFunc(func(src string) (string, error) { formatted, err := imports.Process("", []byte(src), &imports.Options{ Fragment: true, Comments: true, TabIndent: true, TabWidth: 8, }) if err != nil { return "", &InvalidSourceError{ Source: src, err: xerrors.Wrap(err, "failed to run goimports.Process"), } } formatted, err = returns.Process("", "", formatted, &returns.Options{ Fragment: true, }) if err != nil { return "", &InvalidSourceError{ Source: src, err: xerrors.Wrap(err, "failed to run goreturns.Process"), } } return strings.Join([]string{ "// Code generated by github.com/yssk22/go/generator DO NOT EDIT.", "//", string(formatted), }, "\n"), nil })
GoFormatter is a formatter for go.
var JavaScriptFormatter = FormatterFunc(func(src string) (string, error) { var out bytes.Buffer var errOut bytes.Buffer c := exec.Command("prettier", "--stdin", "--parser", "flow") r, w := io.Pipe() c.Stdin = r c.Stdout = &out c.Stderr = &errOut if err := c.Start(); err != nil { w.Close() return "", xerrors.Wrap(err, "cannot launch prettier") } if _, err := w.Write([]byte( []byte(strings.Join([]string{ "// Code generated by github.com/yssk22/go/generator DO NOT EDIT.", "// eslint-edisable", src, }, "\n")), )); err != nil { w.Close() return "", err } w.Close() c.Wait() if !c.ProcessState.Success() { return "", fmt.Errorf("failed to run prettier: %s", errOut.String()) } return out.String(), nil })
JavaScriptFormatter is a formatter for js
Functions ¶
Types ¶
type AnnotatedNode ¶
type AnnotatedNode struct { Node ast.Node Source *FileInfo // contains filtered or unexported fields }
AnnotatedNode represents a Node annotated by Annotation
func CollectAnnotatedNodes ¶
func CollectAnnotatedNodes(p *PackageInfo) []*AnnotatedNode
CollectAnnotatedNodes returns a list of node with annotations
func (*AnnotatedNode) GenError ¶
func (a *AnnotatedNode) GenError(e error, n ast.Node) error
GenError returns an wrapped error object with the node information n must be the ancestor of Signature node or nil
func (*AnnotatedNode) GetParamsBy ¶
func (a *AnnotatedNode) GetParamsBy(s AnnotationSymbol) keyvalue.Getter
GetParamsBy returns the key-vale pairs for annotation parameters.
func (*AnnotatedNode) IsAnnotated ¶
func (a *AnnotatedNode) IsAnnotated(s AnnotationSymbol) bool
IsAnnotated returns the node is annotated by the symbol `s`
type Annotation ¶
type Annotation struct { Symbol AnnotationSymbol Params map[string]interface{} }
Annotation is a pair of AnnotationSymbol and it's params.
type AnnotationSymbol ¶
type AnnotationSymbol string
AnnotationSymbol is a special string used for generator annotations and must comply with ^[a_zA_Z0-9][a_zA_Z0-9_]*$
func NewAnnotationSymbol ¶
func NewAnnotationSymbol(s string) AnnotationSymbol
NewAnnotationSymbol returns a Annotation instance from a string
func (AnnotationSymbol) Is ¶
func (a AnnotationSymbol) Is(s string) bool
Is returns if the `s` matches with the annotaiton string.
func (AnnotationSymbol) IsValid ¶
func (a AnnotationSymbol) IsValid() bool
IsValid returns if the annotaiton is valid syntax or not
func (AnnotationSymbol) MaybeMarkedIn ¶
func (a AnnotationSymbol) MaybeMarkedIn(contents []byte) bool
MaybeMarkedIn returns if @annotation appears in the `contents`
type Dependency ¶
type Dependency struct {
// contains filtered or unexported fields
}
Dependency object provides a utility to manage dependencies for the generated source file.
func NewDependency ¶
func NewDependency() *Dependency
NewDependency returns a new *Dependency object
func (*Dependency) Add ¶
func (d *Dependency) Add(pkg string) string
Add a `pkg` into the dependency
func (*Dependency) AddAs ¶
func (d *Dependency) AddAs(pkg string, alias string) string
AddAs is like Add, but use the specific alias name
func (*Dependency) GenImport ¶
func (d *Dependency) GenImport() string
GenImport generates import statements
func (*Dependency) GenImportForJavaScript ¶
func (d *Dependency) GenImportForJavaScript() string
GenImportForJavaScript generates import statements for JavaScript
type FileInfo ¶
FileInfo is a file info that generator is analysing
func (*FileInfo) GetNodeInfo ¶
GetNodeInfo returns *NodeInfo from ast.Node
type FormatterFunc ¶
FormatterFunc is to define Formatter interface from a func
type Generator ¶
type Generator interface { Run(*PackageInfo, []*AnnotatedNode) ([]*Result, error) GetAnnotationSymbol() AnnotationSymbol GetFormatter() Formatter }
Generator is an interface to implement generator command
type InvalidSourceError ¶
type InvalidSourceError struct { Source string // contains filtered or unexported fields }
InvalidSourceError is an error if generated source is unable to compile. Use SourceWithLine() to debug the generated code.
func (*InvalidSourceError) Error ¶
func (e *InvalidSourceError) Error() string
func (*InvalidSourceError) SourceWithLine ¶
func (e *InvalidSourceError) SourceWithLine(all bool) string
SourceWithLine returns the source code with line numbers
type PackageInfo ¶
type PackageInfo struct { Name string Package *types.Package TypeInfo *types.Info Files []*FileInfo }
PackageInfo is a package infomaiton that generator is analysing