Documentation ¶
Index ¶
- Constants
- Variables
- func AddFormerMixins(cps *Composer, filename, nameSpace, alias string) []string
- func FormatGolangCode(src []byte) ([]byte, error)
- func GetLineFeature(code string) string
- func GetNameList(ids []*ast.Ident) (names []string)
- func PrepareMixins(mixinDir, mixinNS string) (mixinNames []string)
- func ReplaceModelFields(cp *CodeParser, node *DeclNode, summary *ModelSummary)
- func RewriteGolangFile(filename string, cleanImports bool) (changed bool, err error)
- func RewritePackage(pkgpath, pkgname string) error
- func SaveCodeToFile(filename string, codeText []byte) ([]byte, error)
- func TrimComment(c string) string
- func WriteGolangFileCleanImports(filename string, codeText []byte) ([]byte, error)
- func WriteGolangFilePrettify(filename string, codeText []byte) ([]byte, error)
- type CodeParser
- type CodeSource
- func (cs *CodeSource) AddCode(code []byte) error
- func (cs *CodeSource) AddImport(path, alias string) bool
- func (cs *CodeSource) AddReplace(first, last ast.Node, code string)
- func (cs *CodeSource) AddStringCode(code string) error
- func (cs *CodeSource) AltSource() ([]byte, bool)
- func (cs *CodeSource) CleanImports() (removes int)
- func (cs *CodeSource) DelImport(path, alias string) bool
- func (cs *CodeSource) GetComment(c *ast.CommentGroup, trim bool) string
- func (cs *CodeSource) GetContent() ([]byte, error)
- func (cs *CodeSource) GetFieldCode(node *DeclNode, i int) string
- func (cs *CodeSource) GetFirstFileName() string
- func (cs *CodeSource) GetNodeCode(node ast.Node) string
- func (cs *CodeSource) GetPackage() string
- func (cs *CodeSource) GetPackageOffset() int
- func (cs *CodeSource) ResetImports(filename string, imports map[string]string) error
- func (cs *CodeSource) SetPackage(name string) (err error)
- func (cs *CodeSource) SetSource(source []byte) (err error)
- func (cs *CodeSource) WriteTo(filename string) error
- type Composer
- func (c *Composer) ParseAndMixinFile(filename string, verbose bool) error
- func (c *Composer) RegisterGlobalSubstitute(sub *ModelSummary)
- func (c *Composer) RegisterSubstitute(sub *ModelSummary)
- func (c *Composer) RemoveSubstitute(name string)
- func (c *Composer) SubstituteSummary(summary *ModelSummary, verbose bool) []*ModelSummary
- type DeclNode
- type FieldNode
- type ModelSummary
- func (s *ModelSummary) GetInnerCode() string
- func (s *ModelSummary) GetSortedFeatures() []string
- func (s *ModelSummary) GetSubstitute(prefix string) string
- func (s *ModelSummary) Isomorphic() bool
- func (s *ModelSummary) ParseFields(cp *CodeParser, node *DeclNode) int
- func (s *ModelSummary) ReplaceSummary(sub *ModelSummary) bool
- func (s *ModelSummary) ScanAndUseMixins(sub *ModelSummary, deep, verbose bool) (matched, needImport bool)
- type PosAlt
Constants ¶
const ( AdaptivePkgName = "#" ModelExtends = "`json:\",inline\" xorm:\"extends\"`" )
Variables ¶
var (
MixinWildcards = []string{"*Core", "*Mixin"} // 可注册的Mixin类名形式
)
Functions ¶
func AddFormerMixins ¶
AddFormerMixins 将文件中符合的Mixin类先行注册
func FormatGolangCode ¶
FormatGolangCode 格式化代码,如果出错返回原内容
func GetLineFeature ¶
GetLineFeature 提取 struct field 的名称与类型作为特征
func PrepareMixins ¶ added in v1.5.6
PrepareMixins 扫描目录中的可注册Mixin,并声明为某个NameSpace下
func ReplaceModelFields ¶
func ReplaceModelFields(cp *CodeParser, node *DeclNode, summary *ModelSummary)
ReplaceModelFields 将Mixin写入到Model内,替代它的部分字段
func RewriteGolangFile ¶ added in v1.6.0
RewriteGolangFile 读出来go代码,重新写入文件
func RewritePackage ¶
RewritePackage 将包中的Go文件格式化,如果提供了pkgname则用作新包名
func SaveCodeToFile ¶ added in v1.6.0
SaveCodeToFile 将go代码保存到文件
func WriteGolangFileCleanImports ¶
WriteGolangFileCleanImports 美化和整理导入,并输出go代码到文件
Types ¶
type CodeParser ¶
type CodeParser struct { DeclNodes []*DeclNode DeclIndexes map[string][]int *CodeSource }
CodeParser 代码节点解析器
func NewFileParser ¶
func NewFileParser(filename string) (cp *CodeParser, err error)
NewFileParser 从文件创建解析器
func NewSourceParser ¶
func NewSourceParser(source []byte) (cp *CodeParser, err error)
NewFileParser 从代码创建解析器
func (*CodeParser) AllDeclNode ¶
func (cp *CodeParser) AllDeclNode(kind string) []*DeclNode
AllDeclNode 获取指定种类的所有节点
func (*CodeParser) FindDeclNode ¶ added in v1.5.6
func (cp *CodeParser) FindDeclNode(kind string, wildcards ...string) []*DeclNode
FindDeclNode 根据名称规则查找
func (*CodeParser) GetDeclNode ¶
func (cp *CodeParser) GetDeclNode(kind string, offset int) *DeclNode
GetDeclNode 获取指定种类的一个节点
func (*CodeParser) ParseDecls ¶
func (cp *CodeParser) ParseDecls(kind string, limit int) bool
ParseDecls 解析指定种类的声明
type CodeSource ¶
type CodeSource struct { Fileast *ast.File Fileset *token.FileSet Source []byte Alternates []PosAlt // Source 只能替换一次,然后必须重新解析 Fileast *printer.Config }
CodeSource 源码解析器
func RewriteWithImports ¶ added in v1.6.0
func RewriteWithImports(pkg string, source []byte, imports map[string]string, ) (*CodeSource, error)
RewriteWithImports 注入导入声明
func (*CodeSource) AddImport ¶
func (cs *CodeSource) AddImport(path, alias string) bool
AddImport 增加一个import
func (*CodeSource) AddReplace ¶
func (cs *CodeSource) AddReplace(first, last ast.Node, code string)
AddReplace 将两个节点以及中间的部分,使用新内容代替
func (*CodeSource) AddStringCode ¶
func (cs *CodeSource) AddStringCode(code string) error
AddStringCode 增加新代码在原有之后
func (*CodeSource) AltSource ¶
func (cs *CodeSource) AltSource() ([]byte, bool)
AltSource 改写源码,应用事先准备的可代替代码Alternates
func (*CodeSource) CleanImports ¶
func (cs *CodeSource) CleanImports() (removes int)
CleanImports 整理全部import代码
func (*CodeSource) DelImport ¶
func (cs *CodeSource) DelImport(path, alias string) bool
DelImport 删除一个import
func (*CodeSource) GetComment ¶
func (cs *CodeSource) GetComment(c *ast.CommentGroup, trim bool) string
GetComment 获取注释
func (*CodeSource) GetContent ¶
func (cs *CodeSource) GetContent() ([]byte, error)
GetContent 获取代码内容
func (*CodeSource) GetFieldCode ¶
func (cs *CodeSource) GetFieldCode(node *DeclNode, i int) string
GetFieldCode 获得类成员代码内容
func (*CodeSource) GetFirstFileName ¶ added in v1.5.6
func (cs *CodeSource) GetFirstFileName() string
GetFirstFileName 如果代码由多个文件组成,返回第一个文件路径
func (*CodeSource) GetNodeCode ¶
func (cs *CodeSource) GetNodeCode(node ast.Node) string
GetNodeCode 获得节点代码内容
func (*CodeSource) GetPackageOffset ¶
func (cs *CodeSource) GetPackageOffset() int
GetPackageOffset 获取包名结束位置
func (*CodeSource) ResetImports ¶ added in v1.5.6
func (cs *CodeSource) ResetImports(filename string, imports map[string]string) error
ResetImports 重新注入声明,并美化代码
func (*CodeSource) SetPackage ¶
func (cs *CodeSource) SetPackage(name string) (err error)
SetPackage 设置新的包名
func (*CodeSource) SetSource ¶
func (cs *CodeSource) SetSource(source []byte) (err error)
SetSource 替换全部代码,并重新解析
func (*CodeSource) WriteTo ¶
func (cs *CodeSource) WriteTo(filename string) error
WriteTo 美化代码并保存到文件
type Composer ¶ added in v1.5.3
type Composer struct { Global *Composer // contains filtered or unexported fields }
Composer 是一个Model分析和嵌入工具
func GlobalComposer ¶ added in v1.7.0
func GlobalComposer() *Composer
GlobalComposer 公共嵌入器,带有xquery的两个mixins
func (*Composer) ParseAndMixinFile ¶ added in v1.5.6
ParseAndMixinFile 使用Mixin改写文件
func (*Composer) RegisterGlobalSubstitute ¶ added in v1.5.3
func (c *Composer) RegisterGlobalSubstitute(sub *ModelSummary)
RegisterGlobalSubstitute 注册可替换Model
func (*Composer) RegisterSubstitute ¶ added in v1.5.3
func (c *Composer) RegisterSubstitute(sub *ModelSummary)
RegisterSubstitute 注册可替换Model
func (*Composer) RemoveSubstitute ¶ added in v1.5.3
RemoveSubstitute 删除可替换Model
func (*Composer) SubstituteSummary ¶ added in v1.5.3
func (c *Composer) SubstituteSummary(summary *ModelSummary, verbose bool) []*ModelSummary
SubstituteSummary 替换和改写Model
type DeclNode ¶
type DeclNode struct { Token token.Token Kinds []string Names []string Fields []*FieldNode Comment *ast.CommentGroup Offset int ast.Decl }
DeclNode 声明节点
func NewDeclNode ¶
NewDeclNode 创建声明节点
func (*DeclNode) ParseFunDecl ¶
ParseFunDecl //解析函数节点
func (*DeclNode) ParseGenDecl ¶
ParseGenDecl 解析类声明节点
type FieldNode ¶
type FieldNode struct { Names []string Comment *ast.CommentGroup *ast.Field }
FieldNode 类成员节点
type ModelSummary ¶
type ModelSummary struct { Name string Substitute string Import, Alias string Features []string FieldLines []string Children []string IsChanged bool IsExists bool // 同构Model已存在 // contains filtered or unexported fields }
ModelSummary Model摘要
func (*ModelSummary) GetInnerCode ¶
func (s *ModelSummary) GetInnerCode() string
GetInnerCode 找出 model 内部代码,即在 {} 里面的内容
func (*ModelSummary) GetSortedFeatures ¶
func (s *ModelSummary) GetSortedFeatures() []string
GetSortedFeatures 找出 model 的所有特征并排序
func (*ModelSummary) GetSubstitute ¶
func (s *ModelSummary) GetSubstitute(prefix string) string
GetSubstitute 使用inline tag代替原来的那些字段,prefix可以是*星号
func (*ModelSummary) Isomorphic ¶ added in v1.7.0
func (s *ModelSummary) Isomorphic() bool
Isomorphic 已经是其他Model的同构体,没有嵌入的空间
func (*ModelSummary) ParseFields ¶
func (s *ModelSummary) ParseFields(cp *CodeParser, node *DeclNode) int
ParseFields 解析 struct 代码,提取特征并补充注释到代码
func (*ModelSummary) ReplaceSummary ¶ added in v1.5.6
func (s *ModelSummary) ReplaceSummary(sub *ModelSummary) bool
ReplaceSummary 使用subModel重写当前Model
func (*ModelSummary) ScanAndUseMixins ¶ added in v1.5.6
func (s *ModelSummary) ScanAndUseMixins(sub *ModelSummary, deep, verbose bool) (matched, needImport bool)
ScanAndUseMixins 扫描和使用Mixin