generator

package
v0.0.0-...-519d24f Latest Latest
Warning

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

Go to latest
Published: May 5, 2020 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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

func ParseTag

func ParseTag(tag string) keyvalue.Getter

ParseTag returns a keyvalue.Getter for the defined tag like `json:"foo"`.

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

type FileInfo struct {
	Path       string
	Ast        *ast.File
	Source     []byte
	CommentMap ast.CommentMap
}

FileInfo is a file info that generator is analysing

func (*FileInfo) GetNodeInfo

func (f *FileInfo) GetNodeInfo(node ast.Node) *NodeInfo

GetNodeInfo returns *NodeInfo from ast.Node

func (*FileInfo) Inspect

func (f *FileInfo) Inspect(fun func(ast.Node) bool)

Inspect run ast.Inspect for the file

type Formatter

type Formatter interface {
	Format(string) (string, error)
}

type FormatterFunc

type FormatterFunc func(string) (string, error)

FormatterFunc is to define Formatter interface from a func

func (FormatterFunc) Format

func (f FormatterFunc) Format(s string) (string, error)

Format implements Formatter#format

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 NodeInfo

type NodeInfo struct {
	FilePath string
	LineNum  int
	LineText string
	Pos      int
	End      int
}

func (*NodeInfo) String

func (ni *NodeInfo) String() string

type PackageInfo

type PackageInfo struct {
	Name     string
	Package  *types.Package
	TypeInfo *types.Info
	Files    []*FileInfo
}

PackageInfo is a package infomaiton that generator is analysing

func (*PackageInfo) Inspect

func (p *PackageInfo) Inspect(fun func(ast.Node) bool)

Inspect runs a ast.Inspect for files in the package

type Result

type Result struct {
	Filename string
	Source   string
}

Result represents a result of Generator#Run

type Runner

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

Runner is a struct to run generators

func NewRunner

func NewRunner(generators ...Generator) *Runner

NewRunner returns a *Runner

func (*Runner) Run

func (r *Runner) Run(dir string) error

Run executes Generator#run for all generators.

Directories

Path Synopsis
example
Code generated by github.com/yssk22/go/generator DO NOT EDIT.
Code generated by github.com/yssk22/go/generator DO NOT EDIT.
example
Code generated by github.com/yssk22/go/generator DO NOT EDIT.
Code generated by github.com/yssk22/go/generator DO NOT EDIT.

Jump to

Keyboard shortcuts

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