loader

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2022 License: MIT Imports: 30 Imported by: 1

Documentation

Index

Constants

View Source
const (
	UnknownError = packages.UnknownError
	ListError    = packages.ListError
	ParseError   = packages.ParseError
	TypeError    = packages.TypeError
	// Our kinds of errors. Add a gap of 10 to be sure we won't conflict
	// with previous enum values.
	ValidateError = packages.TypeError + 10 + iota
)

Variables

This section is empty.

Functions

func CommentFromText

func CommentFromText(orig ast.Node, text string) *ast.CommentGroup

TODO(mvdan): both loader and format use CommentFromText, but it feels awkward to have it here. CommentFromText creates a multi-line comment from the given text, with its start and end positions matching the given node's.

func ConvertFromProto

func ConvertFromProto(w io.Writer, r io.Reader, filename string, importPath string, protocPath string) error

ConvertFromProto converts a single proto file read from r, writing the generated Gunk file to w. The output isn't canonically formatted, so it's up to the caller to use gunk/format.Source on the result if needed.

func ErrorAbsolutePos

func ErrorAbsolutePos(err error, pos token.Position) error

ErrorAbsolutePos modifies all positions in err, considered to be relative to pos. This is useful so that the position information of syntax tree nodes parsed from a comment are relative to the entire file, and not only relative to the comment containing the source.

func PrintErrors

func PrintErrors(pkgs []*GunkPackage) int

PrintErrors prints to os.Stderr the accumulated errors of all packages in the import graph rooted at pkgs, dependencies first. PrintErrors returns the number of errors present, including import errors which aren't printed.

func Visit

func Visit(pkgs []*GunkPackage, pre func(*GunkPackage) bool, post func(*GunkPackage))

This file is an almost exact copy of go/packages/visit.go, but changed to work on Gunk packages. Visit visits all the packages in the import graph whose roots are pkgs, calling the optional pre function the first time each package is encountered (preorder), and the optional post function after a package's dependencies have been visited (postorder). The boolean result of pre(pkg) determines whether the imports of package pkg are visited.

Types

type GunkPackage

type GunkPackage struct {
	packages.Package
	Dir        string      // for now, we require all files to be in the same dir
	GunkFiles  []string    // absolute paths of the Gunk files
	GunkSyntax []*ast.File // syntax trees for the files in GunkFiles
	// GunkNames are unique arbitrary names for each gunk file in GunkFiles.
	// We don't want to use absolute paths when referring to files in the
	// CodeGeneratorRequest, because that will trigger many generators to
	// write to disk.
	GunkNames []string
	// GunkTags stores the "+gunk" tags associated with each syntax tree
	// node in GunkSyntax.
	GunkTags  map[ast.Node][]GunkTag
	Imports   map[string]*GunkPackage
	ProtoName string // protobuf package name
	// contains filtered or unexported fields
}

type GunkTag

type GunkTag struct {
	ast.Expr                // original expression
	Type     types.Type     // type of the expression
	Value    constant.Value // constant value of the expression, if any
}

func SplitGunkTag

func SplitGunkTag(pkg *GunkPackage, fset *token.FileSet, comment *ast.CommentGroup) (string, []GunkTag, error)

SplitGunkTag splits '+gunk' tags from a comment group, returning the leading documentation and the tags Go expressions.

If pkg is not nil, the tag is also type-checked using the package's type information.

type Loader

type Loader struct {
	Dir  string
	Fset *token.FileSet
	// If Types is true, we parse and type-check the given packages and all
	// transitive dependencies, including gunk tags. Otherwise, we only
	// parse the given packages.
	Types bool
	// contains filtered or unexported fields
}

func (*Loader) Import

func (l *Loader) Import(path string) (*types.Package, error)

Import satisfies the go/types.Importer interface.

Unlike standard Go ones like go/importer and x/tools/go/packages, this one is adapted to load Gunk packages.

Aside from that, it is very similar to standard Go importers that load from source.

func (*Loader) Load

func (l *Loader) Load(patterns ...string) ([]*GunkPackage, error)

Load loads the Gunk packages on the provided patterns from the given dir and using the given fileset.

Similar to Go, if a path begins with ".", it is interpreted as a file system path where a package is located, and "..." patterns are supported.

type ProtoLoader

type ProtoLoader struct {
	// Dir is the absolute path from where the LoadProto method
	// will load proto files.
	// If empty, it will load from executing directory
	Dir        string
	ProtocPath string
}

func (*ProtoLoader) LoadProto

func (l *ProtoLoader) LoadProto(names ...string) ([]*descriptorpb.FileDescriptorProto, error)

LoadProto loads the specified protobuf packages as if they were dependencies.

It does so with protoc, to leverage protoc's features such as locating the files, and the protoc parser to get a FileDescriptorProto out of the proto file content.

Jump to

Keyboard shortcuts

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