Documentation ¶
Overview ¶
Package aster is golang coding efficiency engine.
Copyright 2018 henrylee2cn. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- func IsExported(name string) bool
- func IsMethod(obj Object) bool
- func IsPureFunc(obj Object) bool
- type CommObjectMethods
- type ConstObject
- type File
- func (f *File) Fetch(fn func(Object) bool) (objects []Object)
- func (f *File) Format() (string, error)
- func (f *File) FormatNode(node ast.Node) (string, error)
- func (f *File) Inspect(fn func(Object) bool)
- func (f *File) LookupImports(currPkgName string) (imports []*Import, found bool)
- func (f *File) LookupPackages(currPkgName string) (pkgs []*Package, found bool)
- func (f *File) LookupPureFunc(name string) (pf FuncObject, found bool)
- func (f *File) LookupPureFuncInMod(name string) (pf FuncObject, found bool)
- func (f *File) LookupPureFuncInPkg(name string) (pf FuncObject, found bool)
- func (f *File) LookupType(name string) (t TypeObject, found bool)
- func (f *File) LookupTypeInMod(name string) (t TypeObject, found bool)
- func (f *File) LookupTypeInPkg(name string) (t TypeObject, found bool)
- func (f *File) Package() (*Package, bool)
- func (f *File) Reparse() (err error)
- func (f *File) Store() (err error)
- func (f *File) String() string
- func (f *File) TryFormatNode(node ast.Node, defaultValue ...string) string
- type FuncField
- type FuncObject
- type FuncObjectMethods
- type Import
- type Kind
- type Module
- func (m *Module) Fetch(fn func(Object) bool) (objects []Object)
- func (m *Module) Format() (codes map[string]map[string]string, first error)
- func (m *Module) FormatNode(node ast.Node) (string, error)
- func (m *Module) Inspect(fn func(Object) bool)
- func (m *Module) Reparse() (first error)
- func (m *Module) Store() (first error)
- type NilNode
- type Object
- type Package
- func (p *Package) Fetch(fn func(Object) bool) (objects []Object)
- func (p *Package) Format() (codes map[string]string, first error)
- func (p *Package) FormatNode(node ast.Node) (string, error)
- func (p *Package) Inspect(fn func(Object) bool)
- func (p *Package) LookupPureFunc(name string) (f FuncObject, found bool)
- func (p *Package) LookupType(name string) (t TypeObject, found bool)
- func (p *Package) Module() (*Module, bool)
- func (p *Package) Store() (first error)
- type StructField
- type StructTag
- func (s *StructTag) AddOptions(key string, options ...string)
- func (s *StructTag) Delete(keys ...string)
- func (s *StructTag) DeleteOptions(key string, options ...string)
- func (s *StructTag) Get(key string) (*Tag, error)
- func (s *StructTag) Keys() []string
- func (s *StructTag) Set(tag *Tag) error
- func (s *StructTag) String() string
- func (s *StructTag) Tags() []*Tag
- type Tag
- type TypeObject
- type TypeObjectMethods
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsExported ¶
IsExported reports whether name is an exported Go symbol (that is, whether it begins with an upper-case letter).
func IsPureFunc ¶ added in v0.2.3
IsPureFunc returns true if b is implementd FuncObject, but not method function.
Types ¶
type CommObjectMethods ¶ added in v0.2.3
type CommObjectMethods interface { // Name returns the type's name within its package for a defined type. // For other (non-defined) types it returns the empty string. Name() string // Filename returns package name to which the node belongs PkgName() string // Filename returns filename to which the node belongs Filename() string // ObjKind returns what the object represents. ObjKind() ast.ObjKind // Kind returns the data kind. Kind() Kind // IsGlobal returns whether the declaration is global. IsGlobal() bool // Doc returns lead comment. Doc() string // String returns the formated code block. String() string // contains filtered or unexported methods }
CommObjectMethods is the common methods of Object interface.
type ConstObject ¶ added in v0.2.3
type ConstObject interface { CommObjectMethods TypeDecl() (*FuncField, bool) // contains filtered or unexported methods }
ConstObject is the representation of a Go const value node.
type File ¶
type File struct { *ast.File PkgName string FileSet *token.FileSet Filename string Src []byte Imports []*Import Objects map[token.Pos]Object // <type node pos, Node> // contains filtered or unexported fields }
A File node represents a Go source file.
The Comments list contains all comments in the source file in order of appearance, including the comments that are pointed to from other objects via Doc and Comment fields.
For correct printing of source code containing comments (using packages go/format and go/printer), special care must be taken to update comments when a File's syntax tree is modified: For printing, comments are interspersed between tokens based on their position. If syntax tree objects are removed or moved, relevant comments in their vicinity must also be removed (from the File.Comments list) or moved accordingly (by updating their positions). A CommentMap may be used to facilitate some of these operations.
Whether and how a comment is associated with a node depends on the interpretation of the syntax tree by the manipulating program: Except for Doc and Comment comments directly associated with objects, the remaining comments are "free-floating" (see also issues #18593, #20744).
func ParseFile ¶
ParseFile parses the source code of a single Go source file and returns the corresponding ast.File node. The source code may be provided via the filename of the source file, or via the src parameter.
If src != nil, ParseFile parses the source from src and the filename is only used when recording position information. The type of the argument for the src parameter must be string, []byte, or io.Reader. If src == nil, ParseFile parses the file specified by filename.
The mode parameter controls the amount of source text parsed and other optional parser functionality. Position information is recorded in the file set fset, which must not be nil.
If the source couldn't be read, the returned AST is nil and the error indicates the specific failure. If the source was read but syntax errors were found, the result is a partial AST (with ast.Bad* objects representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by file position.
func (*File) FormatNode ¶
FormatNode formats the node and returns the string.
func (*File) LookupImports ¶
LookupImports lookups the import info by package name.
func (*File) LookupPackages ¶
LookupPackages lookups the package object by package name. NOTE: Only lookup the parsed module.
func (*File) LookupPureFunc ¶ added in v0.2.2
func (f *File) LookupPureFunc(name string) (pf FuncObject, found bool)
LookupPureFunc lookups FuncObject by name in the current file. NOTE: lookup FuncObject, but not method function.
func (*File) LookupPureFuncInMod ¶ added in v0.2.2
func (f *File) LookupPureFuncInMod(name string) (pf FuncObject, found bool)
LookupPureFuncInMod lookup FuncObject by name in the current module. NOTE: lookup FuncObject, but not method function.
func (*File) LookupPureFuncInPkg ¶ added in v0.2.2
func (f *File) LookupPureFuncInPkg(name string) (pf FuncObject, found bool)
LookupPureFuncInPkg lookups FuncObject by name in the current package. NOTE: lookup FuncObject, but not method function.
func (*File) LookupType ¶
func (f *File) LookupType(name string) (t TypeObject, found bool)
LookupType lookups TypeObject by type name in the current file.
func (*File) LookupTypeInMod ¶
func (f *File) LookupTypeInMod(name string) (t TypeObject, found bool)
LookupTypeInMod lookup Type by type name in the current module.
func (*File) LookupTypeInPkg ¶
func (f *File) LookupTypeInPkg(name string) (t TypeObject, found bool)
LookupTypeInPkg lookups TypeObject by type name in the current package.
type FuncObject ¶ added in v0.2.3
type FuncObject interface { CommObjectMethods FuncObjectMethods // contains filtered or unexported methods }
FuncObject is the representation of a Go function or method. NOTE: Equivalent to n.Kind()==Func
type FuncObjectMethods ¶ added in v0.2.3
type FuncObjectMethods interface { // NumParam returns a function type's input parameter count. NumParam() int // NumResult returns a function type's output parameter count. NumResult() int // Param returns the type of a function type's i'th input parameter. Param(int) (*FuncField, bool) // Result returns the type of a function type's i'th output parameter. Result(int) (*FuncField, bool) // IsVariadic reports whether a function type's final input parameter // is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's // implicit actual type []T. // // For concreteness, if t represents func(x int, y ... float64), then // // f.NumParam() == 2 // f.Param(0) is the Type for "int" // f.Param(1) is the Type for "[]float64" // f.IsVariadic() == true // IsVariadic() bool // Recv returns receiver (methods); or returns false (functions) Recv() (*FuncField, bool) }
FuncObjectMethods is the representation of a Go function or method. NOTE: Kind = Func
type Import ¶
type Import struct { *ast.ImportSpec Name string Path string Doc *ast.CommentGroup }
Import import info
type Kind ¶
type Kind uint
A Kind represents the specific kind of an Object data. The zero Kind is not a valid kind.
type Module ¶
type Module struct { FileSet *token.FileSet Dir string Packages map[string]*Package // <package name, *Package> // contains filtered or unexported fields }
Module packages AST
func ParseDir ¶
func ParseDir(dir string, filter func(os.FileInfo) bool, mode ...parser.Mode) (module *Module, first error)
ParseDir calls ParseFile for all files with names ending in ".go" in the directory specified by path and returns a map of package name -> package AST with all the packages found.
If filter != nil, only the files with os.FileInfo entries passing through the filter (and ending in ".go") are considered. The mode bits are passed to ParseFile unchanged. Position information is recorded in fset, which must not be nil.
If the directory couldn't be read, a nil map and the respective error are returned. If a parse error occurred, a non-nil but incomplete map and the first error encountered are returned.
func (*Module) Fetch ¶
Fetch traversing through the current module, fetches node if fn returns true.
func (*Module) Format ¶
Format format the package and returns the string. @codes <packageName,<fileName,code>>
func (*Module) FormatNode ¶
FormatNode formats the node and returns the string.
type Object ¶ added in v0.2.3
type Object interface { CommObjectMethods FuncObjectMethods TypeObjectMethods // contains filtered or unexported methods }
Object the basic sub-interface based on ast.Node extension, is the supertype of other extended interfaces.
type Package ¶
type Package struct { FileSet *token.FileSet Dir string Name string // package name Scope *ast.Scope // package scope across all files Imports map[string]*ast.Object // map of package id -> package object Files map[string]*File // Go source files by filename // contains filtered or unexported fields }
A Package node represents a set of source files collectively building a Go package.
func (*Package) Fetch ¶
Fetch traversing through the current package, fetches node if fn returns true.
func (*Package) FormatNode ¶
FormatNode formats the node and returns the string.
func (*Package) LookupPureFunc ¶ added in v0.2.2
func (p *Package) LookupPureFunc(name string) (f FuncObject, found bool)
LookupPureFunc lookups FuncObject by name in the current package. NOTE: lookup FuncObject, but not method function.
func (*Package) LookupType ¶
func (p *Package) LookupType(name string) (t TypeObject, found bool)
LookupType lookups TypeObject by type name in the current package.
type StructField ¶
A StructField describes a single field in a struct.
func (*StructField) Anonymous ¶
func (s *StructField) Anonymous() bool
Anonymous returns whether the field is an anonymous field.
type StructTag ¶
type StructTag struct {
// contains filtered or unexported fields
}
A StructTag is the tag string in a struct field.
By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.
func (*StructTag) AddOptions ¶
AddOptions adds the given option for the given key. If the option already exists it doesn't add it again.
func (*StructTag) DeleteOptions ¶
DeleteOptions deletes the given options for the given key
func (*StructTag) Get ¶
Get returns the tag associated with the given key. If the key is present in the tag the value (which may be empty) is returned. Otherwise the returned value will be the empty string. The ok return value reports whether the tag exists or not (which the return value is nil).
func (*StructTag) Keys ¶
Keys returns a slice of tag keys. The order is the original tag order unless it was changed.
type Tag ¶
Tag defines a single struct's string literal tag
type Tag struct { Key is the tag key, such as json, xml, etc.. i.e: `json:"foo,omitempty". Here key is: "json" Key string
Name is a part of the value i.e: `json:"foo,omitempty". Here name is: "foo" Name string
Options is a part of the value. It contains a slice of tag options i.e: `json:"foo,omitempty". Here options is: ["omitempty"] Options []string }
type TypeObject ¶ added in v0.2.3
type TypeObject interface { CommObjectMethods TypeObjectMethods // contains filtered or unexported methods }
TypeObject is the representation of a Go type node.
type TypeObjectMethods ¶ added in v0.2.3
type TypeObjectMethods interface { // IsAssign is there `=` for declared type? IsAssign() bool // NumMethod returns the number of exported methods in the type's method set. NumMethod() int // Method returns the i'th method in the type's method set. // For a non-interface type T or *T, the returned Method's Type and Func // fields describe a function whose first argument is the receiver. // // For an interface type, the returned Method's Type field gives the // method signature, without a receiver, and the Func field is nil. Method(int) (FuncObject, bool) // MethodByName returns the method with that name in the type's // method set and a boolean indicating if the method was found. // // For a non-interface type T or *T, the returned Method's Type and Func // fields describe a function whose first argument is the receiver. // // For an interface type, the returned Method's Type field gives the // method signature, without a receiver, and the Func field is nil. MethodByName(string) (FuncObject, bool) // Implements reports whether the type implements the interface type u. Implements(u TypeObject) bool // NumField returns a struct type's field count. // It panics if the type's Kind is not Struct. NumField() int // Field returns a struct type's i'th field. // It panics if the type's Kind is not Struct. // It panics if i is not in the range [0, NumField()). Field(int) *StructField // FieldByName returns the struct field with the given name // and a boolean indicating if the field was found. // It panics if the type's Kind is not Struct. FieldByName(name string) (field *StructField, found bool) // contains filtered or unexported methods }
TypeObjectMethods is the representation of a Go type node. NOTE: Kind != Func