aster

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2018 License: Apache-2.0 Imports: 16 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsExported

func IsExported(name string) bool

IsExported reports whether name is an exported Go symbol (that is, whether it begins with an upper-case letter).

func IsMethod added in v0.2.3

func IsMethod(obj Object) bool

IsMethod returns true if b is implementd method FuncObject.

func IsPureFunc added in v0.2.3

func IsPureFunc(obj Object) bool

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

func ParseFile(filename string, src interface{}, mode ...parser.Mode) (f *File, err error)

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) Fetch

func (f *File) Fetch(fn func(Object) bool) (objects []Object)

Fetch traversing through the current file, fetches node if fn returns true.

func (*File) Format

func (f *File) Format() (string, error)

Format formats the file and returns the string.

func (*File) FormatNode

func (f *File) FormatNode(node ast.Node) (string, error)

FormatNode formats the node and returns the string.

func (*File) Inspect

func (f *File) Inspect(fn func(Object) bool)

Inspect traverses objects in the file.

func (*File) LookupImports

func (f *File) LookupImports(currPkgName string) (imports []*Import, found bool)

LookupImports lookups the import info by package name.

func (*File) LookupPackages

func (f *File) LookupPackages(currPkgName string) (pkgs []*Package, found bool)

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.

func (*File) Package

func (f *File) Package() (*Package, bool)

Package returns package object if exist.

func (*File) Reparse

func (f *File) Reparse() (err error)

Reparse reparses AST.

func (*File) Store

func (f *File) Store() (err error)

Store formats the file codes and writes to the local file.

func (*File) String

func (f *File) String() string

String returns the formated file text.

func (*File) TryFormatNode

func (f *File) TryFormatNode(node ast.Node, defaultValue ...string) string

TryFormatNode formats the node and returns the string, returns the default string if fail.

type FuncField

type FuncField struct {
	Name     string
	TypeName string // not contain `*`
}

FuncField function params or results.

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.

const (
	Invalid Kind = iota
	Suspense
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	String
	Interface
	Chan
	Array
	Slice
	Map
	Func
	Struct
	Ptr
)

Kind enumerate

func (Kind) String

func (i Kind) String() string

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

func (m *Module) Fetch(fn func(Object) bool) (objects []Object)

Fetch traversing through the current module, fetches node if fn returns true.

func (*Module) Format

func (m *Module) Format() (codes map[string]map[string]string, first error)

Format format the package and returns the string. @codes <packageName,<fileName,code>>

func (*Module) FormatNode

func (m *Module) FormatNode(node ast.Node) (string, error)

FormatNode formats the node and returns the string.

func (*Module) Inspect

func (m *Module) Inspect(fn func(Object) bool)

Inspect traverses objects in the module.

func (*Module) Reparse

func (m *Module) Reparse() (first error)

Reparse reparses AST.

func (*Module) Store

func (m *Module) Store() (first error)

Store formats the module codes and writes to the local files.

type NilNode

type NilNode struct{}

NilNode nil Node

func (NilNode) End

func (NilNode) End() token.Pos

End .

func (NilNode) Pos

func (NilNode) Pos() token.Pos

Pos .

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

func (p *Package) Fetch(fn func(Object) bool) (objects []Object)

Fetch traversing through the current package, fetches node if fn returns true.

func (*Package) Format

func (p *Package) Format() (codes map[string]string, first error)

Format format the package and returns the string. @codes <fileName,code>

func (*Package) FormatNode

func (p *Package) FormatNode(node ast.Node) (string, error)

FormatNode formats the node and returns the string.

func (*Package) Inspect

func (p *Package) Inspect(fn func(Object) bool)

Inspect traverses objects in the package.

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.

func (*Package) Module

func (p *Package) Module() (*Module, bool)

Module returns module object if exist.

func (*Package) Store

func (p *Package) Store() (first error)

Store formats the package codes and writes to the local files.

type StructField

type StructField struct {
	*ast.Field
	Tags *StructTag // field tags handler
}

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.

func (*StructField) Comment

func (s *StructField) Comment() string

Comment returns line comment.

func (*StructField) Doc

func (s *StructField) Doc() string

Doc returns lead comment.

func (*StructField) Name

func (s *StructField) Name() string

Name returns field name

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

func (s *StructTag) AddOptions(key string, options ...string)

AddOptions adds the given option for the given key. If the option already exists it doesn't add it again.

func (*StructTag) Delete

func (s *StructTag) Delete(keys ...string)

Delete deletes the tag for the given keys

func (*StructTag) DeleteOptions

func (s *StructTag) DeleteOptions(key string, options ...string)

DeleteOptions deletes the given options for the given key

func (*StructTag) Get

func (s *StructTag) Get(key string) (*Tag, error)

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

func (s *StructTag) Keys() []string

Keys returns a slice of tag keys. The order is the original tag order unless it was changed.

func (*StructTag) Set

func (s *StructTag) Set(tag *Tag) error

Set sets the given tag. If the tag key already exists it'll override it

func (*StructTag) String

func (s *StructTag) String() string

String reassembles the tags into a valid literal tag field representation

func (*StructTag) Tags

func (s *StructTag) Tags() []*Tag

Tags returns a slice of tags. The order is the original tag order unless it was changed.

type Tag

type Tag = structtag.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

Jump to

Keyboard shortcuts

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