Documentation ¶
Overview ¶
tagproc provides utilities to quickly process declarations in a Go codebase. The idea is as follows: Both struct and interface declarations may have embedded tag member that are processed and removed by the TagProcessor. As an example, consider
type X struct { tags.ImportantStruct }
The TagProcessor allows you to register handlers for the tags.ImportantStruct type that are called for all declarations that have an embedded (i.e. anonymous) member of this kind. A type may be used as a tag whenever it is defined in a package named tags.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HandlerMap ¶
type HandlerMap = map[string][]TagHandler
HandlerMap maps path types to lists of tag handlers.
type TagContext ¶
type TagContext struct { File *ast.File FileSet *token.FileSet // An import map that maps package scopes to the full package paths. This work under // the assumption that a path github.com/foo/bar actually points to a package called // bar. Imports map[string]string }
TagContext contains all the data available to a tag processor.
type TagHandler ¶
type TagHandler interface { // BeginFile is called whenever a new file is processed. BeginFile(context TagContext) error // HandleTag is called once for each struct with a tag in each file. // The object parameter then points to the definition of the struct in the source file, // the literalTag parameter contains the struct tag associated to the field in the struct. // It is the empty string if there is no tag attached to the struct field. HandleTag(context TagContext, object *ast.Object, literalTag string) error // FinishFile is called whenever the processing of a file finishes. FinishFile(context TagContext) error // Finalize is called when all files have been processed. Finalize() error }
TagHandler is implemented by all types that want to handle tags embedded in structs. A TagHandler gets a chance to react to each file and to each tag embedded in that file.
type TagProcessor ¶
type TagProcessor struct {
HandlerMap HandlerMap
}
TagProcessor is the FileTransformation that handles all tags.
func New ¶
func New() *TagProcessor
func (*TagProcessor) AddHandler ¶
func (tp *TagProcessor) AddHandler(tag string, handler TagHandler)
AddHandler adds a new handler for the given tag. For example:
tp.AddHandler("github.com/foo/tags/bartag", handler)
registers a handler to be called for each tags.bartag typed tag in a struct.
func (*TagProcessor) Apply ¶
func (tp *TagProcessor) Apply(fileContext gotransform.FileContext) error
Apply goes through the given file, looks for structs with tags, and calls all the respective tag processors on the structs.
func (*TagProcessor) Finalize ¶
func (tp *TagProcessor) Finalize() error
Finalize finalizes all tag handlers.
func (*TagProcessor) Prepare ¶
func (tp *TagProcessor) Prepare() error