Documentation
¶
Overview ¶
Package parser provides tools for parsing Go source files to extract adapter configurations.
Index ¶
- Constants
- func NewParserError(format string, args ...any) error
- func NewParserErrorWithCause(cause error, format string, args ...any) error
- func NewParserErrorWithCauseAndContext(cause error, context any, format string, args ...any) error
- func NewParserErrorWithContext(context any, format string, args ...any) error
- func ParseDirective(parentCtx *Context, ruleType interfaces.RuleType, directive *Directive) error
- func ParseFileDirectives(cfg *config.Config, file *goast.File, fset *gotoken.FileSet) (*config.Config, error)
- func RegisterContainer(rt interfaces.RuleType, factoryFunc ContainerFactory)
- type ConstRule
- func (r *ConstRule) AddConstRule(rule *ConstRule) error
- func (r *ConstRule) AddFieldRule(rule *FieldRule) error
- func (r *ConstRule) AddFuncRule(rule *FuncRule) error
- func (r *ConstRule) AddMethodRule(rule *MethodRule) error
- func (r *ConstRule) AddPackage(pkg *PackageRule) error
- func (r *ConstRule) AddRule(rule any) error
- func (r *ConstRule) AddTypeRule(rule *TypeRule) error
- func (r *ConstRule) AddVarRule(rule *VarRule) error
- func (r *ConstRule) Finalize(parent Container) error
- func (r *ConstRule) ParseDirective(directive *Directive) error
- func (r *ConstRule) Type() interfaces.RuleType
- type Container
- type ContainerFactory
- type Context
- func (c *Context) ActiveContext() *Context
- func (c *Context) Container() Container
- func (c *Context) EndContext() error
- func (c *Context) IsActive() bool
- func (c *Context) IsExplicit() bool
- func (c *Context) Parent() *Context
- func (c *Context) SetActivate(active bool)
- func (c *Context) SetExplicit(explicit bool) *Context
- func (c *Context) StartContext(container Container) (*Context, error)
- func (c *Context) StartOrActiveContext(ruleType interfaces.RuleType) (*Context, error)
- type Directive
- type DirectiveIterator
- type FieldRule
- func (f *FieldRule) AddConstRule(rule *ConstRule) error
- func (f *FieldRule) AddFieldRule(rule *FieldRule) error
- func (f *FieldRule) AddFuncRule(rule *FuncRule) error
- func (f *FieldRule) AddMethodRule(rule *MethodRule) error
- func (f *FieldRule) AddPackage(pkg *PackageRule) error
- func (f *FieldRule) AddRule(rule any) error
- func (f *FieldRule) AddTypeRule(rule *TypeRule) error
- func (f *FieldRule) AddVarRule(rule *VarRule) error
- func (f *FieldRule) Finalize(parent Container) error
- func (f *FieldRule) ParseDirective(directive *Directive) error
- func (f *FieldRule) Type() interfaces.RuleType
- type FuncRule
- func (r *FuncRule) AddConstRule(rule *ConstRule) error
- func (r *FuncRule) AddFieldRule(rule *FieldRule) error
- func (r *FuncRule) AddFuncRule(rule *FuncRule) error
- func (r *FuncRule) AddMethodRule(rule *MethodRule) error
- func (r *FuncRule) AddPackage(pkg *PackageRule) error
- func (r *FuncRule) AddRule(rule any) error
- func (r *FuncRule) AddTypeRule(rule *TypeRule) error
- func (r *FuncRule) AddVarRule(rule *VarRule) error
- func (r *FuncRule) Finalize(parent Container) error
- func (r *FuncRule) ParseDirective(directive *Directive) error
- func (r *FuncRule) Type() interfaces.RuleType
- type InvalidRule
- func (i *InvalidRule) AddConstRule(rule *ConstRule) error
- func (i *InvalidRule) AddFieldRule(rule *FieldRule) error
- func (i *InvalidRule) AddFuncRule(rule *FuncRule) error
- func (i *InvalidRule) AddMethodRule(rule *MethodRule) error
- func (i *InvalidRule) AddPackage(pkg *PackageRule) error
- func (i *InvalidRule) AddRule(rule any) error
- func (i *InvalidRule) AddTypeRule(rule *TypeRule) error
- func (i *InvalidRule) AddVarRule(rule *VarRule) error
- func (i *InvalidRule) Finalize(parent Container) error
- func (i *InvalidRule) ParseDirective(directive *Directive) error
- func (i *InvalidRule) Type() interfaces.RuleType
- type MethodRule
- func (m *MethodRule) AddConstRule(rule *ConstRule) error
- func (m *MethodRule) AddFieldRule(rule *FieldRule) error
- func (m *MethodRule) AddFuncRule(rule *FuncRule) error
- func (m *MethodRule) AddMethodRule(rule *MethodRule) error
- func (m *MethodRule) AddPackage(pkg *PackageRule) error
- func (m *MethodRule) AddRule(rule any) error
- func (m *MethodRule) AddTypeRule(rule *TypeRule) error
- func (m *MethodRule) AddVarRule(rule *VarRule) error
- func (m *MethodRule) Finalize(parent Container) error
- func (m *MethodRule) ParseDirective(directive *Directive) error
- func (m *MethodRule) Type() interfaces.RuleType
- type PackageRule
- func (p *PackageRule) AddConstRule(rule *ConstRule) error
- func (p *PackageRule) AddFieldRule(rule *FieldRule) error
- func (p *PackageRule) AddFuncRule(rule *FuncRule) error
- func (p *PackageRule) AddMethodRule(rule *MethodRule) error
- func (p *PackageRule) AddPackage(pkg *PackageRule) error
- func (p *PackageRule) AddRule(rule any) error
- func (p *PackageRule) AddTypeRule(rule *TypeRule) error
- func (p *PackageRule) AddVarRule(rule *VarRule) error
- func (p *PackageRule) Finalize(parent Container) error
- func (p *PackageRule) ParseDirective(directive *Directive) error
- func (p *PackageRule) Type() interfaces.RuleType
- type RootConfig
- func (r *RootConfig) AddConstRule(rule *ConstRule) error
- func (r *RootConfig) AddFieldRule(rule *FieldRule) error
- func (r *RootConfig) AddFuncRule(rule *FuncRule) error
- func (r *RootConfig) AddMethodRule(rule *MethodRule) error
- func (r *RootConfig) AddPackage(pkg *PackageRule) error
- func (r *RootConfig) AddRule(rule any) error
- func (r *RootConfig) AddTypeRule(rule *TypeRule) error
- func (r *RootConfig) AddVarRule(rule *VarRule) error
- func (r *RootConfig) Finalize(parent Container) error
- func (r *RootConfig) ParseDirective(directive *Directive) error
- func (r *RootConfig) Type() interfaces.RuleType
- type TypeRule
- func (r *TypeRule) AddConstRule(rule *ConstRule) error
- func (r *TypeRule) AddFieldRule(rule *FieldRule) error
- func (r *TypeRule) AddFuncRule(rule *FuncRule) error
- func (r *TypeRule) AddMethodRule(rule *MethodRule) error
- func (r *TypeRule) AddPackage(pkg *PackageRule) error
- func (r *TypeRule) AddRule(rule any) error
- func (r *TypeRule) AddTypeRule(rule *TypeRule) error
- func (r *TypeRule) AddVarRule(rule *VarRule) error
- func (r *TypeRule) Finalize(parent Container) error
- func (r *TypeRule) ParseDirective(directive *Directive) error
- func (r *TypeRule) Type() interfaces.RuleType
- type VarRule
- func (r *VarRule) AddConstRule(rule *ConstRule) error
- func (r *VarRule) AddFieldRule(rule *FieldRule) error
- func (r *VarRule) AddFuncRule(rule *FuncRule) error
- func (r *VarRule) AddMethodRule(rule *MethodRule) error
- func (r *VarRule) AddPackage(pkg *PackageRule) error
- func (r *VarRule) AddRule(rule any) error
- func (r *VarRule) AddTypeRule(rule *TypeRule) error
- func (r *VarRule) AddVarRule(rule *VarRule) error
- func (r *VarRule) Finalize(parent Container) error
- func (r *VarRule) ParseDirective(directive *Directive) error
- func (r *VarRule) Type() interfaces.RuleType
Constants ¶
const DirectivePrefix = "//go:adapter:"
DirectivePrefix is the prefix used to identify adapter directives in Go source code comments
Variables ¶
This section is empty.
Functions ¶
func NewParserError ¶
NewParserError creates a new parser error instance with a formatted message. It captures the current stack trace. This is for general parser errors not directly tied to a specific directive.
func NewParserErrorWithCause ¶
func NewParserErrorWithContext ¶
NewParserErrorWithContext creates a new parser error instance with a formatted message and an arbitrary context object. It captures the current stack trace.
func ParseDirective ¶
func ParseDirective(parentCtx *Context, ruleType interfaces.RuleType, directive *Directive) error
func ParseFileDirectives ¶
func ParseFileDirectives(cfg *config.Config, file *goast.File, fset *gotoken.FileSet) (*config.Config, error)
ParseFileDirectives parses a Go source file and returns the built configuration. This is the exported entry point.
func RegisterContainer ¶
func RegisterContainer(rt interfaces.RuleType, factoryFunc ContainerFactory)
RegisterContainer registers a factory function for a given interfaces.RuleType. It will resize the registry slice if necessary.
Types ¶
type ConstRule ¶
ConstRule is a wrapper around config.ConstRule to implement the Container interface.
func (*ConstRule) AddConstRule ¶
func (*ConstRule) AddFieldRule ¶
func (*ConstRule) AddFuncRule ¶
func (*ConstRule) AddMethodRule ¶
func (r *ConstRule) AddMethodRule(rule *MethodRule) error
func (*ConstRule) AddPackage ¶
func (r *ConstRule) AddPackage(pkg *PackageRule) error
func (*ConstRule) AddTypeRule ¶
func (*ConstRule) AddVarRule ¶
func (*ConstRule) ParseDirective ¶
func (*ConstRule) Type ¶
func (r *ConstRule) Type() interfaces.RuleType
type Container ¶
type Container interface { // Type returns the type of this container. Type() interfaces.RuleType // ParseDirective applies a sub-command (e.g., ":rename", ":disabled") to the rule. // It takes the builder to interact with the broader parsing state if necessary (e.g., to set an active member). ParseDirective(directive *Directive) error // AddRule adds a child rule to this container. This is the generic method. AddRule(rule any) error // AddPackage adds a PackageConfig package configuration to this container. AddPackage(pkg *PackageRule) error // AddTypeRule adds a TypeRule to this container. AddTypeRule(rule *TypeRule) error // AddFuncRule adds a FuncRule to this container. AddFuncRule(rule *FuncRule) error // AddVarRule adds a VarRule to this container. AddVarRule(rule *VarRule) error // AddConstRule adds a ConstRule to this container. AddConstRule(rule *ConstRule) error // AddMethodRule adds a MethodRule to this container. AddMethodRule(rule *MethodRule) error // AddFieldRule adds a FieldRule to this container. AddFieldRule(rule *FieldRule) error // Finalize performs any post-processing or validation for this container // after all its direct rules have been added. Finalize(parent Container) error }
Container defines the interface for any object that can hold parsed rules and participate in the hierarchical configuration structure.
func NewContainer ¶
func NewContainer(ruleType interfaces.RuleType) Container
NewContainer creates a new Container instance for a given interfaces.RuleType. It returns nil if the type is not registered or invalid.
type ContainerFactory ¶
type ContainerFactory func() Container
ContainerFactory defines a function that creates a new instance of a Container.
func NewContainerFactory ¶
func NewContainerFactory(ruleType interfaces.RuleType) ContainerFactory
NewContainerFactory resolves a command string (including abbreviations) and returns the corresponding interfaces.RuleType constant.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context represents a node in the parsing state hierarchy. It holds the configuration for a specific scope (e.g., a file or a specific struct) and maintains links to its parent and children contexts, forming a tree structure.
func NewContext ¶
NewContext creates a new root Context node. It takes the data container (Container) for this scope and whether it was explicitly created.
func (*Context) ActiveContext ¶
ActiveContext finds and returns the currently active child context from the activeContexts. It returns nil if no child context is active.
func (*Context) EndContext ¶
EndContext deactivates the current context, finalizes its container, and returns its parent context. This process is recursive, ensuring that all active child contexts are also ended and finalized from the bottom up.
func (*Context) IsExplicit ¶
IsExplicit returns true if this context was created by an explicit directive.
func (*Context) SetActivate ¶
SetActivate sets the active status of the context.
func (*Context) SetExplicit ¶
SetExplicit sets the explicit flag for the context and returns the context.
func (*Context) StartContext ¶
StartContext creates a new child context, makes it the sole active context among its siblings, and returns it. Before starting the new context, it ensures any previously active sibling context is properly ended by calling EndContext.
func (*Context) StartOrActiveContext ¶
func (c *Context) StartOrActiveContext(ruleType interfaces.RuleType) (*Context, error)
StartOrActiveContext gets an active child context or creates a new one. It first checks if an active child context already exists and returns it. If not, it creates a new one by calling the provided factory function.
type Directive ¶
type Directive struct { Line int // Line number in the source file. Command string // The full command string (e.g., "type:struct"). Note: :json suffix is removed here. Argument string // The raw argument string. // Parsed components of the command. BaseCmd string // The base command (e.g., "type"). SubCmds []string // Sub-commands (e.g., ["struct"]). IsJSON bool // True if the original command had a ":json" suffix. }
Directive represents a parsed adptool directive from a Go comment. It is immutable after creation.
func (Directive) ShouldUnmarshal ¶
type DirectiveIterator ¶
func NewDirectiveIterator ¶
func NewDirectiveIterator(file *goast.File, fset *gotoken.FileSet) DirectiveIterator
NewDirectiveIterator creates a new directiveIterator.
type FieldRule ¶
type FieldRule struct {
*config.MemberRule
}
FieldRule is a wrapper around config.MemberRule to implement the Container interface.
func (*FieldRule) AddConstRule ¶
func (*FieldRule) AddFieldRule ¶
func (*FieldRule) AddFuncRule ¶
func (*FieldRule) AddMethodRule ¶
func (f *FieldRule) AddMethodRule(rule *MethodRule) error
func (*FieldRule) AddPackage ¶
func (f *FieldRule) AddPackage(pkg *PackageRule) error
func (*FieldRule) AddTypeRule ¶
func (*FieldRule) AddVarRule ¶
func (*FieldRule) ParseDirective ¶
func (*FieldRule) Type ¶
func (f *FieldRule) Type() interfaces.RuleType
type FuncRule ¶
FuncRule is a wrapper around config.FuncRule to implement the Container interface.
func (*FuncRule) AddConstRule ¶
func (*FuncRule) AddFieldRule ¶
func (*FuncRule) AddFuncRule ¶
func (*FuncRule) AddMethodRule ¶
func (r *FuncRule) AddMethodRule(rule *MethodRule) error
func (*FuncRule) AddPackage ¶
func (r *FuncRule) AddPackage(pkg *PackageRule) error
func (*FuncRule) AddTypeRule ¶
func (*FuncRule) AddVarRule ¶
func (*FuncRule) ParseDirective ¶
func (*FuncRule) Type ¶
func (r *FuncRule) Type() interfaces.RuleType
type InvalidRule ¶
type InvalidRule struct{}
InvalidRule is a singleton container returned by the factory when a type is not found. Its methods always return an error, allowing for deferred error handling at the call site.
func (*InvalidRule) AddConstRule ¶
func (i *InvalidRule) AddConstRule(rule *ConstRule) error
func (*InvalidRule) AddFieldRule ¶
func (i *InvalidRule) AddFieldRule(rule *FieldRule) error
func (*InvalidRule) AddFuncRule ¶
func (i *InvalidRule) AddFuncRule(rule *FuncRule) error
func (*InvalidRule) AddMethodRule ¶
func (i *InvalidRule) AddMethodRule(rule *MethodRule) error
func (*InvalidRule) AddPackage ¶
func (i *InvalidRule) AddPackage(pkg *PackageRule) error
func (*InvalidRule) AddRule ¶
func (i *InvalidRule) AddRule(rule any) error
func (*InvalidRule) AddTypeRule ¶
func (i *InvalidRule) AddTypeRule(rule *TypeRule) error
func (*InvalidRule) AddVarRule ¶
func (i *InvalidRule) AddVarRule(rule *VarRule) error
func (*InvalidRule) Finalize ¶
func (i *InvalidRule) Finalize(parent Container) error
Finalize for an invalid rule is a no-op.
func (*InvalidRule) ParseDirective ¶
func (i *InvalidRule) ParseDirective(directive *Directive) error
ParseDirective for an invalid rule always returns an error.
func (*InvalidRule) Type ¶
func (i *InvalidRule) Type() interfaces.RuleType
type MethodRule ¶
type MethodRule struct {
*config.MemberRule
}
MethodRule is a wrapper around config.MemberRule to implement the Container interface.
func (*MethodRule) AddConstRule ¶
func (m *MethodRule) AddConstRule(rule *ConstRule) error
func (*MethodRule) AddFieldRule ¶
func (m *MethodRule) AddFieldRule(rule *FieldRule) error
func (*MethodRule) AddFuncRule ¶
func (m *MethodRule) AddFuncRule(rule *FuncRule) error
func (*MethodRule) AddMethodRule ¶
func (m *MethodRule) AddMethodRule(rule *MethodRule) error
func (*MethodRule) AddPackage ¶
func (m *MethodRule) AddPackage(pkg *PackageRule) error
func (*MethodRule) AddRule ¶
func (m *MethodRule) AddRule(rule any) error
func (*MethodRule) AddTypeRule ¶
func (m *MethodRule) AddTypeRule(rule *TypeRule) error
func (*MethodRule) AddVarRule ¶
func (m *MethodRule) AddVarRule(rule *VarRule) error
func (*MethodRule) Finalize ¶
func (m *MethodRule) Finalize(parent Container) error
func (*MethodRule) ParseDirective ¶
func (m *MethodRule) ParseDirective(directive *Directive) error
func (*MethodRule) Type ¶
func (m *MethodRule) Type() interfaces.RuleType
type PackageRule ¶
PackageRule is a wrapper around config.Package to implement the Container interface. (Previously PackageConfig)
func (*PackageRule) AddConstRule ¶
func (p *PackageRule) AddConstRule(rule *ConstRule) error
func (*PackageRule) AddFieldRule ¶
func (p *PackageRule) AddFieldRule(rule *FieldRule) error
func (*PackageRule) AddFuncRule ¶
func (p *PackageRule) AddFuncRule(rule *FuncRule) error
func (*PackageRule) AddMethodRule ¶
func (p *PackageRule) AddMethodRule(rule *MethodRule) error
func (*PackageRule) AddPackage ¶
func (p *PackageRule) AddPackage(pkg *PackageRule) error
func (*PackageRule) AddRule ¶
func (p *PackageRule) AddRule(rule any) error
func (*PackageRule) AddTypeRule ¶
func (p *PackageRule) AddTypeRule(rule *TypeRule) error
func (*PackageRule) AddVarRule ¶
func (p *PackageRule) AddVarRule(rule *VarRule) error
func (*PackageRule) Finalize ¶
func (p *PackageRule) Finalize(parent Container) error
func (*PackageRule) ParseDirective ¶
func (p *PackageRule) ParseDirective(directive *Directive) error
func (*PackageRule) Type ¶
func (p *PackageRule) Type() interfaces.RuleType
type RootConfig ¶
RootConfig is a wrapper around config.Config to implement the Container interface.
func (*RootConfig) AddConstRule ¶
func (r *RootConfig) AddConstRule(rule *ConstRule) error
func (*RootConfig) AddFieldRule ¶
func (r *RootConfig) AddFieldRule(rule *FieldRule) error
func (*RootConfig) AddFuncRule ¶
func (r *RootConfig) AddFuncRule(rule *FuncRule) error
func (*RootConfig) AddMethodRule ¶
func (r *RootConfig) AddMethodRule(rule *MethodRule) error
func (*RootConfig) AddPackage ¶
func (r *RootConfig) AddPackage(pkg *PackageRule) error
func (*RootConfig) AddRule ¶
func (r *RootConfig) AddRule(rule any) error
func (*RootConfig) AddTypeRule ¶
func (r *RootConfig) AddTypeRule(rule *TypeRule) error
func (*RootConfig) AddVarRule ¶
func (r *RootConfig) AddVarRule(rule *VarRule) error
func (*RootConfig) Finalize ¶
func (r *RootConfig) Finalize(parent Container) error
func (*RootConfig) ParseDirective ¶
func (r *RootConfig) ParseDirective(directive *Directive) error
func (*RootConfig) Type ¶
func (r *RootConfig) Type() interfaces.RuleType
type TypeRule ¶
TypeRule is a wrapper around config.TypeRule to implement the Container interface.
func (*TypeRule) AddConstRule ¶
func (*TypeRule) AddFieldRule ¶
func (*TypeRule) AddFuncRule ¶
func (*TypeRule) AddMethodRule ¶
func (r *TypeRule) AddMethodRule(rule *MethodRule) error
func (*TypeRule) AddPackage ¶
func (r *TypeRule) AddPackage(pkg *PackageRule) error
func (*TypeRule) AddTypeRule ¶
func (*TypeRule) AddVarRule ¶
func (*TypeRule) ParseDirective ¶
func (*TypeRule) Type ¶
func (r *TypeRule) Type() interfaces.RuleType
type VarRule ¶
VarRule is a wrapper around config.VarRule to implement the Container interface.
func (*VarRule) AddConstRule ¶
func (*VarRule) AddFieldRule ¶
func (*VarRule) AddFuncRule ¶
func (*VarRule) AddMethodRule ¶
func (r *VarRule) AddMethodRule(rule *MethodRule) error
func (*VarRule) AddPackage ¶
func (r *VarRule) AddPackage(pkg *PackageRule) error
func (*VarRule) AddTypeRule ¶
func (*VarRule) AddVarRule ¶
func (*VarRule) ParseDirective ¶
func (*VarRule) Type ¶
func (r *VarRule) Type() interfaces.RuleType