gotype

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2019 License: MIT Imports: 13 Imported by: 0

README

gotype

Golang source code parsing, usage like reflect package

Build Status Go Report Card GoDoc GitHub license cover.run

Usage

API Documentation

Examples

License

Pouch is licensed under the MIT License. See LICENSE for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal added in v0.3.0

func Equal(t0 Type, t1 Type) bool

Equal Reports whether the type type is equal

func Identical added in v0.3.0

func Identical(t0, t1 Type) bool

Identical reports whether t0 and t1 are identical types.

func IdenticalIgnoreTags added in v0.3.0

func IdenticalIgnoreTags(t0, t1 Type) bool

IdenticalIgnoreTags reports whether t0 and t1 are identical types if tags are ignored.

func Implements added in v0.3.0

func Implements(t Type, inter Type) bool

Implements reports whether type inter implements interface t.

Types

type ChanDir

type ChanDir int

ChanDir represents a channel type's direction.

const (
	RecvDir ChanDir = 1 << iota         // chan<-
	SendDir                             // <-chan
	BothDir ChanDir = RecvDir | SendDir // chan
)

Define channel direction

func (ChanDir) String

func (i ChanDir) String() string

type Importer

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

Importer Go source type analyzer

func NewImporter

func NewImporter(options ...Option) *Importer

NewImporter creates a new importer

func (*Importer) FileSet

func (i *Importer) FileSet() *token.FileSet

FileSet returns the FileSet

func (*Importer) Import

func (i *Importer) Import(path, src string) (Type, error)

Import returns go package scope

func (*Importer) ImportBuild

func (i *Importer) ImportBuild(path string, src string) (*build.Package, error)

ImportBuild returns details about the Go package named by the import path.

func (*Importer) ImportFile added in v0.0.3

func (i *Importer) ImportFile(path string, f *ast.File) (Type, error)

ImportFile returns go package scope

func (*Importer) ImportPackage added in v0.0.3

func (i *Importer) ImportPackage(path string, pkg *ast.Package) (Type, error)

ImportPackage returns go package scope

func (*Importer) ImportSource added in v0.0.3

func (i *Importer) ImportSource(path string, src []byte) (Type, error)

ImportSource returns go package scope

type Kind

type Kind uint8

Kind represents the specific kind of type that a Type represents. The zero Kind is not a valid kind.

const (
	Invalid Kind = iota

	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	String
	Byte
	Rune
	Error

	Array
	Chan
	Func
	Interface
	Map
	Ptr
	Slice
	Struct

	Field       // a Struct Field
	Scope       // package or func body
	Declaration // a top-level function, variable, or constant.
)

Define kind

func (Kind) String

func (i Kind) String() string

type Option added in v0.2.1

type Option func(i *Importer)

Option some basic options

func ErrorHandler

func ErrorHandler(f func(error)) Option

ErrorHandler returns the error handler option

func WithCommentLocator added in v0.0.3

func WithCommentLocator() Option

WithCommentLocator sets comment locator

type Type

type Type interface {

	// String returns a string representation of the type.
	// The string representation may use shortened package names
	// (e.g., base64 instead of "encoding/base64") and is not
	// guaranteed to be unique among types. To test for type identity,
	// compare the Types directly.
	String() string

	// PkgPath returns a named type's package path, that is, the import path
	// that uniquely identifies the package, such as "encoding/base64".
	// If the type was predeclared (string, error) or unnamed (*T, struct{}, []int),
	// the package path will be the empty string.
	PkgPath() string

	// IsGoroot returns package is found in Go root
	IsGoroot() bool

	// Name returns the type's name within its package.
	// It returns an empty string for unnamed types.
	Name() string

	// Kind returns the specific kind of this type.
	Kind() Kind

	// Key returns a map type's key type.
	// It panics if the type's Kind is not Map.
	Key() Type

	// Elem returns a type's element type.
	// It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
	Elem() Type

	// Declaration returns a type's declaration.
	// It panics if the type's Kind is not declaration.
	Declaration() Type

	// Tag returns a field type's tag.
	// It panics if the type's Kind is not Field.
	Tag() reflect.StructTag

	// Len returns an array type's length.
	// It panics if the type's Kind is not Array.
	Len() int

	// Value returns a type's value.
	// Only get constants
	Value() string

	// ChanDir returns a channel type's direction.
	// It panics if the type's Kind is not Chan.
	ChanDir() ChanDir

	// Out returns the type of a function type's i'th output parameter.
	// It panics if the type's Kind is not Func.
	// It panics if i is not in the range [0, NumOut()).
	Out(int) Type

	// NumOut returns a function type's output parameter count.
	// It panics if the type's Kind is not Func.
	NumOut() int

	// In returns the type of a function type's i'th input parameter.
	// It panics if the type's Kind is not Func.
	// It panics if i is not in the range [0, NumIn()).
	In(int) Type

	// NumIn returns a function type's input parameter count.
	// It panics if the type's Kind is not Func.
	NumIn() int

	// 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
	//
	//	t.NumIn() == 2
	//	t.In(0) is the reflect.Type for "int"
	//	t.In(1) is the reflect.Type for "[]float64"
	//	t.IsVariadic() == true
	//
	// IsVariadic panics if the type's Kind is not Func.
	IsVariadic() bool

	// 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) Type

	// FieldByName returns the struct field with the given name
	// and a boolean indicating if the field was found.
	FieldByName(string) (Type, bool)

	// NumField returns a struct type's field count.
	// It panics if the type's Kind is not Struct.
	NumField() int

	// IsAnonymous returns is an embedded field
	// It panics if the type's Kind is not Field.
	IsAnonymous() bool

	// Method returns the i'th method in the type's method set.
	// Not contain anonymo
	// It panics if i is not in the range [0, NumMethod()).
	//
	// 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) Type

	// 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) (Type, bool)

	// NumMethod returns the number of exported methods in the type's method set.
	// Not contain anonymo
	NumMethod() int

	// Child returns a scope type's i'th child.
	// It panics if i is not in the range [0, NumChild()).
	Child(int) Type

	// ChildByName returns the scope with the given name
	// and a boolean indicating if the child was found.
	ChildByName(string) (Type, bool)

	// NumChild returns a scope type's child count.
	NumChild() int

	// Origin returns the type's origin data within its package.
	Origin() ast.Node

	// Doc returns the type's doc within its package.
	Doc() *ast.CommentGroup

	// Comment returns the type's comment within its package.
	Comment() *ast.CommentGroup
}

Type is the representation of a Go type.

Not all methods apply to all kinds of types. Restrictions, if any, are noted in the documentation for each method. Use the Kind method to find out the kind of type before calling kind-specific methods. Calling a method inappropriate to the kind of type causes a run-time panic.

Type values are comparable, such as with the == operator, so they can be used as map keys. Two Type values are equal if they represent identical types.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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