annotation

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2023 License: MIT Imports: 15 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToAST

func BytesToAST(data []byte) (ast.Node, *token.FileSet, error)

BytesToAST conversion some bytes to ast.Node

func CastNode

func CastNode[T ast.Node](n Node) (T, bool)

CastNode quick cast annotation.Node to ast.Node with type check

func FindAnnotations

func FindAnnotations[T any](a []Annotation) []T

FindAnnotations returns all annotation with particular type

func Process

func Process()

Process is entry point for parsing project annotations

func Register

func Register[T Annotation](processor AnnotationProcessor)

func RegisterNoop

func RegisterNoop[T Annotation]()

Types

type Annotation

type Annotation any

type AnnotationProcessor

type AnnotationProcessor interface {
	Process(node Node) error
	Output() map[string][]byte
	Version() string
	Name() string
}

type Lookup added in v0.1.0

type Lookup interface {
	// FindImportByAlias returns related import for alias in current ast.File.
	// For example:
	// import "github.com/YReshetko/go-annotation/internal/tag"
	// ...
	// tag.Parse(...)
	// FindImportByAlias("tag") returns "github.com/YReshetko/go-annotation/internal/tag", true
	FindImportByAlias(alias string) (string, bool)

	// FindNodeByAlias returns related Node by alias, related import if any and a type/function name from related module
	// if alias is empty, then the search will go in current directory of ast.File
	FindNodeByAlias(alias, nodeName string) (Node, string, error)
}

Lookup provides API to retrieve related AST entities by dependency for a node at processing time

type Meta added in v0.1.0

type Meta interface {
	// Root returns related module root (absolut path)
	Root() string
	// Dir returns absolut path to the file directory
	Dir() string
	// FileName returns file name with extension
	FileName() string
	// PackageName returns current package name
	PackageName() string
}

Meta contains node metadata. At any point of processing the framework works with an AST node that is located in some file/package/module The interface provides meta information about current node

type Node

type Node interface {
	// Annotations returns all annotations declared for ast.Node
	Annotations() []Annotation
	// ASTNode returns ast.Node that currently is in processing
	ASTNode() ast.Node
	// AnnotatedNode returns annotation.Node by ast.Node that declared as a sub ast.Node for ASTNode()
	AnnotatedNode(ast.Node) Node
	// ParentNode returns parent annotation.Node by current. false is returned if there is no parents for ast.Node
	ParentNode() (Node, bool)
	// Imports returns all file imports ([]*ast.ImportSpec)
	Imports() []*ast.ImportSpec
	// IsSamePackage compares nodes by module root, file location and package name
	IsSamePackage(v Node) bool
	// Lookup returns a lookup module
	Lookup() Lookup
	// Meta returns node metadata
	Meta() Meta
}

func ParentType

func ParentType[T ast.Node](n Node) (Node, bool)

ParentType quick retrieve parent ast.Node by type

type Rerunable

type Rerunable interface {
	ToRerun() []string
	Clear()
}

Jump to

Keyboard shortcuts

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