reflect

package
v0.0.0-...-5a1941a Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2016 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package reflect is a wrapper for go/ast, go/token, and go/parser packages. It is used to get information about functions, methods, structures, and imports of go files in a specific directory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEqualArg

func AssertEqualArg(a1, a2 *Arg) error

AssertEqualArg gets two *Arg parameters and returns nil in case they are equal and an error otherwise.

func AssertEqualArgs

func AssertEqualArgs(as1, as2 Args) error

AssertEqualArgs gets two lists of arguments and returns nil if they are equal to each other and an error otherwise.

func AssertEqualFunc

func AssertEqualFunc(f1, f2 *Func) error

AssertEqualFunc returns nil if received functions are equal to each other and an error otherwise.

func AssertEqualFuncs

func AssertEqualFuncs(fs1, fs2 Funcs) error

AssertEqualFuncs returns nil if received function slices are equal to each other and an error otherwise.

func AssertEqualMethods

func AssertEqualMethods(ms1, ms2 Methods) error

AssertEqualMethods returns nil if received maps of methods are equal to each other and an error otherwise.

func AssertEqualPkg

func AssertEqualPkg(p1, p2 *Package) error

AssertEqualPkg makes sure two packages are equal to each other. If they are nil is returned. Otherwise, it returns an error.

func AssertEqualStruct

func AssertEqualStruct(s1, s2 *Struct) error

AssertEqualStruct gets two *Struct arguments and makes sure they are equal. If they are a nil is returned. Otherwise, it will return an error.

func AssertEqualStructs

func AssertEqualStructs(ss1, ss2 Structs) error

AssertEqualStructs checks whether two slices of Structs are equal. If they are nil is returned. Otherwise, it will return an error.

func AssertEqualType

func AssertEqualType(t1, t2 *Type) error

AssertEqualType gets two *Type arguments and returns nil in case they are equal to each other and an error otherwise.

Types

type Arg

type Arg struct {
	Name string // Name of the argument, e.g. "name" or "age".
	Tag  string // Tag is a field tag that may be presented.
	Type *Type  // Type represents a type of argument.
}

Arg is used to describe arguments of functions and fields of structures.

type Args

type Args []Arg

Args is a type that is used for representation of an arguments list.

func (Args) Filter

func (as Args) Filter(fn func(f *Arg) bool) (res Args)

Filter returns a list of functions from members of a list fulfilling a condition given by the fn argument.

type Comments

type Comments []string

Comments is a type that is used for representation of a comments list.

func (Comments) Filter

func (cs Comments) Filter(fn func(s string) bool) (res Comments)

Filter returns a list of comments from members of a list fulfilling a condition given by the fn argument.

type Func

type Func struct {
	Comments Comments // Comments that are located right above the function declaration.
	File     string   // Name of the file where the function is located.
	Name     string   // Name of the function, e.g. "Index" or "About".
	Params   Args     // A list of arguments this function receives.
	Recv     *Arg     // Receiver if it is a method and nil otherwise.
	Results  Args     // A list of arguments the function returns.
}

Func is a type that represents information about a function or method.

type Funcs

type Funcs []Func

Funcs is a type that represents a list of functions.

func (Funcs) FilterGroups

func (fs Funcs) FilterGroups(cond func(f *Func) bool, groups ...func(f *Func) bool) ([]Funcs, int)

FilterGroups gets a condition function and a number of group functions. It cuts off those Funcs that do not satisfy condition. And then groups the rest of them. For illustration:

res, count := myFuncs.Filter(isExported, withArguments, withoutArguments)

The result will be:

// All this functions are satisfying isExported condition.
[]Funcs{
	Funcs{ these are functions withArguments },
	Funcs{ these are functions withoutArguments },
}

func (Funcs) Len

func (fs Funcs) Len() int

Len returns number of functions in the list.

func (Funcs) Less

func (fs Funcs) Less(i, j int) bool

Less compares file names + names of two functions.

func (Funcs) Swap

func (fs Funcs) Swap(i, j int)

Swap changes positions of functions with requested indexes.

type Imports

type Imports map[string]map[string]string

Imports is a map of import paths in the following format:

  • Filename:
  • Import name:
  • Import value

func (Imports) Name

func (i Imports) Name(file, value string) (string, bool)

Name checks whether an import that ends with a requested value exists in the requested file. If so, it's name and true are returned. Otherwise, empty string and false will be the results. Imports are ensured to end with the value rather than be equal to it for compatibility with "vendor" package manager.

func (Imports) Value

func (i Imports) Value(file, name string) (string, bool)

Value checks whether requested import name exists in requested file. If so, import value and true are returned. Otherwise, empty string and false will be the results.

type Methods

type Methods map[string]Funcs

Methods is a map of functions with receiver in the following format:

  • Name of a struct:
  • Methods

type Package

type Package struct {
	Funcs   Funcs   // A list of functions of the package.
	Imports Imports // Imports of this package grouped by files.
	Methods Methods // Struct names and their Methods (functions with receivers).
	Name    string  // Name of the package, e.g. "controllers".
	Structs Structs // A list of struct types of the package.
}

Package is a type that combines declarations of functions, types, and structs of a single go package.

func ParseDir

func ParseDir(path string, testPkg bool) *Package

ParseDir expects a path to directory with a go package that is parsed and returned in a form of *Package. There may be 2 packages inside a directory: somename and somename_test. If testPkg argument is false the first one will be returned. Otherwise, the latter is returned.

type Struct

type Struct struct {
	Comments Comments // Comments right above the struct declaration.
	Fields   Args     // A list of fields that belong to this struct.
	File     string   // Name of the file where the function is located.
	Name     string   // Name of the struct, e.g. "Application".
}

Struct is a type that represents information about a specific struct, its fields, and comments group.

type Structs

type Structs []Struct

Structs is a type that represents a list of structures.

func (Structs) Filter

func (ss Structs) Filter(fn func(f *Struct) bool) (res Structs)

Filter returns a list of structures from members of a list fulfilling a condition given by the fn argument.

type Type

type Type struct {
	Name    string // Name of the type, e.g. "URL". It is empty if Decl is not nil.
	Package string // Package name, e.g. "template" in case of "html/template".
	Star    bool   // Star indicates whether it is a pointer.
}

Type represents a type of argument.

func (*Type) String

func (t *Type) String() (name string)

String prints a type name, e.g. "*template.URL", "template.Template", "Controller", "int64", etc.

Jump to

Keyboard shortcuts

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