rewrite

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const MODEL_EXTENDS = "`json:\",inline\" xorm:\"extends\"`"

Variables

This section is empty.

Functions

func AddFormerMixins

func AddFormerMixins(fileName, nameSpace, alias string) []string

func CleanImportsWriteGolangFile

func CleanImportsWriteGolangFile(fileName string, sourceCode []byte) ([]byte, error)

func FormatGolangCode

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

格式化代码,如果出错返回原内容

func GetLineFeature

func GetLineFeature(code string) (feature string)

提取 struct field 的名称与类型作为特征

func GetNameList

func GetNameList(ids []*ast.Ident) (names []string)

func ParseAndMixinFile

func ParseAndMixinFile(fileName string, verbose bool) error

func RegisterSubstitute

func RegisterSubstitute(sub *ModelSummary)

func ReplaceModelFields

func ReplaceModelFields(cp *CodeParser, node *DeclNode, summary *ModelSummary)

func RewritePackage

func RewritePackage(pkgpath, pkgname string) error

将包中的Go文件格式化,如果提供了pkgname则用作新包名

func TrimComment

func TrimComment(c string) string

func WriteCodeFile

func WriteCodeFile(fileName string, sourceCode []byte) ([]byte, error)

func WriteGolangFile

func WriteGolangFile(fileName string, sourceCode []byte) ([]byte, error)

Types

type CodeParser

type CodeParser struct {
	DeclNodes   []*DeclNode
	DeclIndexes map[string][]int
	*CodeSource
}

func NewCodeParser

func NewCodeParser() *CodeParser

func NewFileParser

func NewFileParser(filename string) (cp *CodeParser, err error)

func NewSourceParser

func NewSourceParser(source []byte) (cp *CodeParser, err error)

func (*CodeParser) AllDeclNode

func (cp *CodeParser) AllDeclNode(kind string) []*DeclNode

func (*CodeParser) GetDeclNode

func (cp *CodeParser) GetDeclNode(kind string, offset int) *DeclNode

func (*CodeParser) ParseDecls

func (cp *CodeParser) ParseDecls(kind string, limit int) bool

type CodeSource

type CodeSource struct {
	Fileast    *ast.File
	Fileset    *token.FileSet
	Source     []byte
	Alternates []PosAlt // Source 只能替换一次,然后必须重新解析 Fileast
	*printer.Config
}

func NewCodeSource

func NewCodeSource() *CodeSource

func ResetImports

func ResetImports(cs *CodeSource, imports map[string]string) (*CodeSource, error)

func WithImports

func WithImports(pkg string, source []byte, imports map[string]string) (*CodeSource, error)

func (*CodeSource) AddCode

func (cs *CodeSource) AddCode(code []byte) error

func (*CodeSource) AddImport

func (cs *CodeSource) AddImport(path, alias string) bool

func (*CodeSource) AddReplace

func (cs *CodeSource) AddReplace(first, last ast.Node, code string)

func (*CodeSource) AddStringCode

func (cs *CodeSource) AddStringCode(code string) error

func (*CodeSource) AltSource

func (cs *CodeSource) AltSource() ([]byte, bool)

func (*CodeSource) CleanImports

func (cs *CodeSource) CleanImports() (removes int)

func (*CodeSource) DelImport

func (cs *CodeSource) DelImport(path, alias string) bool

func (*CodeSource) GetComment

func (cs *CodeSource) GetComment(c *ast.CommentGroup, trim bool) string

func (*CodeSource) GetContent

func (cs *CodeSource) GetContent() ([]byte, error)

func (*CodeSource) GetFieldCode

func (cs *CodeSource) GetFieldCode(node *DeclNode, i int) string

func (*CodeSource) GetNodeCode

func (cs *CodeSource) GetNodeCode(node ast.Node) string

func (*CodeSource) GetPackage

func (cs *CodeSource) GetPackage() string

func (*CodeSource) GetPackageOffset

func (cs *CodeSource) GetPackageOffset() int

func (*CodeSource) SetPackage

func (cs *CodeSource) SetPackage(name string) (err error)

func (*CodeSource) SetSource

func (cs *CodeSource) SetSource(source []byte) (err error)

func (*CodeSource) WriteTo

func (cs *CodeSource) WriteTo(filename string) error

type DeclNode

type DeclNode struct {
	Token   token.Token
	Kinds   []string
	Names   []string
	Fields  []*FieldNode
	Comment *ast.CommentGroup
	Offset  int
	ast.Decl
}

func NewDeclNode

func NewDeclNode(decl ast.Decl, offset int, position token.Pos) (n *DeclNode, err error)

func (DeclNode) GetKind

func (n DeclNode) GetKind() string

func (DeclNode) GetName

func (n DeclNode) GetName() string

func (*DeclNode) ParseFunDecl

func (n *DeclNode) ParseFunDecl(fun *ast.FuncDecl) (err error)

func (*DeclNode) ParseGenDecl

func (n *DeclNode) ParseGenDecl(gen *ast.GenDecl) (err error)

func (*DeclNode) ParseStruct

func (n *DeclNode) ParseStruct(fields []*ast.Field) (err error)

type FieldNode

type FieldNode struct {
	Names   []string
	Comment *ast.CommentGroup
	*ast.Field
}

func (*FieldNode) GetTag

func (n *FieldNode) GetTag() reflect.StructTag

type ModelSummary

type ModelSummary struct {
	Name          string
	Substitute    string
	Import, Alias string
	Features      []string

	FieldLines []string
	IsChanged  bool
	// contains filtered or unexported fields
}

func ReplaceSummary

func ReplaceSummary(summary, sub *ModelSummary) *ModelSummary

func (ModelSummary) GetInnerCode

func (s ModelSummary) GetInnerCode() string

找出 model 内部代码,即在 {} 里面的内容

func (ModelSummary) GetSortedFeatures

func (s ModelSummary) GetSortedFeatures() []string

找出 model 的所有特征并排序

func (*ModelSummary) GetSubstitute

func (s *ModelSummary) GetSubstitute() string

func (*ModelSummary) ParseFields

func (s *ModelSummary) ParseFields(cp *CodeParser, node *DeclNode) int

解析 struct 代码,提取特征并补充注释到代码

type PosAlt

type PosAlt struct {
	Pos, End  token.Position
	Alternate []byte
}

替换位置

Jump to

Keyboard shortcuts

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