search

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

package search is used to find targets for the generator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindConstantsByType

func FindConstantsByType(pkgpath, name string, a *AST) (consts []*types.Const)

FindConstantsByType scans the given AST looking for all declared constants of the type identified by pkgpath and name. On success the result will be a slice of go/types.Const instances that represent those constants.

FindConstantsByType is exepcted to be invoked *after* Search and the AST argument is expected to be the same as the one given to Search for caching the packages it loads.

func FindFunc

func FindFunc(pkgpath, name string, a *AST) (fn *types.Func, rawCfg []byte, err error)

FindFunc scans the package identified by pkgpath looking for a function with the given name and, if successful, returns the go/types.Func representation of that function.

FindFunc is exepcted to be invoked *after* Search and the AST argument is expected to be the same as the one given to Search for caching the packages it loads.

The pkgpath parameter should be the import path of a single package, if it's a pattern or something else then the result is undefined.

func FindIncludedFuncs added in v1.2.1

func FindIncludedFuncs(a *AST, callback func(fn *types.Func, rawCfg []byte) error) error

FindIncludedFuncs

TODO(mkopriva): make this not blow up if package can't be found

                on the system, because of the following:

	It is possible that the user of the cmd/validgen tool does not
	have github.com/frk/valid source on the user's machine, which
	is ok because the source would be downloaded automatically as
	soon as the user attempts to run the generated code, or maybe
	the user does not intend to use the included rules, or perhaps
	the user has supplied a set of custom rules that override
	the included ones anyway.

	In case the error is genuine the code should keep working without
	issues, it's just that the reporting of user errors will be poorer.

func FindObject

func FindObject(pkgpath, name string, a *AST) (obj types.Object, err error)

FindObject returns a top-level declared object that matches the given pkgpath and name. The returned object will either be a top-level declared type or a top-level declared function.

Types

type AST

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

AST is used to hold the packages that were loaded during a call to Search.

func (*AST) FileAndLine

func (a *AST) FileAndLine(obj interface{ Pos() token.Pos }) string

type Error

type Error struct {
	C ErrorCode
	// contains filtered or unexported fields
}

func (*Error) ErrType

func (e *Error) ErrType() string

func (*Error) Error

func (e *Error) Error() string

func (*Error) ObjName

func (e *Error) ObjName() string

func (*Error) OriginalError

func (e *Error) OriginalError() string

func (*Error) PkgPath

func (e *Error) PkgPath() string

type ErrorCode

type ErrorCode uint
const (
	ERR_OBJECT_NOTFOUND ErrorCode // object (func or type) not found
	ERR_FUNC_NOTFOUND             // func not found
	ERR_PKG_NOTFOUND              // package not found
	ERR_PKG_LOADFAIL              // failed loading package
	ERR_PKG_ERROR                 // package contains errors
)

type File

type File struct {
	Path    string
	Package *Package `cmp:"+"`
	Matches []*Match
}

File represents a Go file that contains one or more matching validator struct types.

type Match

type Match struct {
	// The go/types.Named representation of the matched type.
	Named *types.Named `cmp:"+"`
	// The file set with which the matched type is associated.
	Fset *token.FileSet `cmp:"+"`
	// The source position of the matched type.
	Pos token.Pos `cmp:"+"`
}

Match holds information on a matched validator struct type.

type Package

type Package struct {
	Name  string
	Path  string
	Fset  *token.FileSet `cmp:"+"`
	Type  *types.Package `cmp:"+"`
	Info  *types.Info    `cmp:"+"`
	Files []*File
}

Package represents a Go package that contains one or more matching validator struct types.

func Search(dir string, recursive bool, rxValidator *regexp.Regexp, filter func(filePath string) bool, a *AST) (out []*Package, err error)

Search scans one or more Go packages looking for validator struct types whose names match the rxValidator regexp. The result will be a list of Packages, where each Package will contain a list of Files that belong to that Package, and each of these Files will contain a list of Matches each representing a validator struct type declared in that File.

Scanned files and packages that do not contain any matching validator struct type declarations will be omitted from the result.

Search will scan the Go package that is located in the specified directory and, optionally, if recursive is true, it will also scan the packages located in the hierarchy of the specified directory.

If the *AST argument is not nil it will be populated with the list of packages that were loaded from the specified directory.

func (Package) Pkg added in v1.1.0

func (p Package) Pkg() Pkg

Pkg returns the Pkg description of p.

type Pkg added in v1.1.0

type Pkg struct {
	Path string
	Name string
}

Pkg describes a Go package. This is used by generator.Generate to identify the package for which the code should be generated.

Jump to

Keyboard shortcuts

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