Documentation
¶
Overview ¶
Package velty implements subset of the JDK Velocity, using the same syntax as Velocity. Implemented subset:
variables - i.e. `${foo.Name} $Name` assignment - i.e. `#set($var1 = 10 + 20 * 10) #set($var2 = ${foo.Name})` if statements - i.e. `#if(1==1) abc #elsif(2==2) def #else ghi #end` foreach - i.e. `#foreach($name in ${foo.Names})` function calls - i.e. `${name.toUpper()}` template evaluation - i.e. `#evaluate($TEMPLATE)`
Index ¶
- Variables
- func ApplyPatches(src []byte, patches []Patch) []byte
- func TransformTemplate(src []byte, adjuster NodeAdjuster, evalCfg ...EvaluateConfig) ([]byte, error)
- type Action
- type ActionKind
- type AdjustFunc
- type Adjuster
- type AdjusterChain
- type BasicPolicy
- type BufferSize
- type CacheSize
- type CycleDetector
- type ErrorRecord
- type EscapeHTML
- type EvalCfg
- type EvaluateConfig
- type EvaluateMode
- type EvaluateSafety
- type Event
- type EventType
- type ExprContext
- type ExprContextKind
- type ForEachInfo
- type Listener
- type MultiCycleDetector
- type NodeAdjuster
- type Option
- type PanicOnError
- type ParserContext
- func (p *ParserContext) AddPatches(ps ...Patch)
- func (p *ParserContext) BumpOccurrence(name string) int
- func (p *ParserContext) CurrentExprContext() ExprContext
- func (p *ParserContext) CurrentNode() ast.Node
- func (p *ParserContext) EnterScope()
- func (p *ParserContext) ExitScope()
- func (p *ParserContext) GetSpan(n ast.Node) (Span, bool)
- func (p *ParserContext) HasUnexpandRaw(name string) bool
- func (p *ParserContext) InitSource(file string, src []byte)
- func (p *ParserContext) IsConst(name string) bool
- func (p *ParserContext) IsLocal(name string) bool
- func (p *ParserContext) IsNamespace(name string) bool
- func (p *ParserContext) IsParam(name string) bool
- func (p *ParserContext) IsStandaloneFunc(name string) bool
- func (p *ParserContext) MarkLocal(name string)
- func (p *ParserContext) MarkUnexpandRaw(name string)
- func (p *ParserContext) Patches() []Patch
- func (p *ParserContext) PopExprContext()
- func (p *ParserContext) PopNode()
- func (p *ParserContext) PushExprContext(ctx ExprContext)
- func (p *ParserContext) PushNode(node ast.Node)
- func (p *ParserContext) ResolvePosition(s Span) Position
- func (p *ParserContext) SeedSymbols(params, namespaces, funcs, consts []string)
- func (p *ParserContext) SetSpan(n ast.Node, s Span)
- func (p *ParserContext) VarKind(name string) VarKind
- type ParserListener
- type Patch
- type PlanHooks
- type Planner
- func (p *Planner) Compile(template []byte) (*est.Execution, func() *est.State, error)
- func (p *Planner) DefineVariable(name string, v interface{}, names ...string) error
- func (p *Planner) EmbedVariable(val interface{}) error
- func (p *Planner) Func(prev *op.Selector, methodName string, call *expr.Call) (*op.Func, error)
- func (p *Planner) New() *Planner
- type PlannerListener
- type Policies
- type Policy
- type PolicyKind
- type PolicyRegistry
- type Pool
- type Position
- type Reporter
- type Scope
- type Severity
- type Span
- type SymbolSeeds
- type Tag
- type TypeParser
- type VarKind
Constants ¶
This section is empty.
Variables ¶
var TimeType = reflect.TypeOf(time.Time{})
Functions ¶
func ApplyPatches ¶ added in v0.3.0
ApplyPatches applies a set of textual replacements to src according to Patch spans and returns a new byte slice. Spans are interpreted using the same convention as parser spans: [Start, End] (inclusive end offset).
Callers are expected to provide non-overlapping patches relative to the original src. Overlapping or out-of-range patches are ignored.
func TransformTemplate ¶ added in v0.3.0
func TransformTemplate(src []byte, adjuster NodeAdjuster, evalCfg ...EvaluateConfig) ([]byte, error)
TransformTemplate parses a template with spans, runs the supplied adjuster, and applies accumulated text patches to return transformed source.
Types ¶
type Action ¶ added in v0.3.0
type Action struct {
Kind ActionKind
Node ast.Node // for replace
Patches []Patch // optional patches
SkipChildren bool // skip walking children of this node
}
Action is the result returned by an adjuster.
func (Action) WithSkipChildren ¶ added in v0.3.0
type ActionKind ¶ added in v0.3.0
type ActionKind int
ActionKind describes the result of an adjuster.
const ( ActionKeep ActionKind = iota ActionReplace ActionRemove ActionPatchOnly )
type AdjustFunc ¶ added in v0.3.0
type AdjustFunc func(node ast.Node, ctx *ParserContext) (Action, error)
AdjustFunc is a helper to define NodeAdjuster via a function.
func (AdjustFunc) Adjust ¶ added in v0.3.0
func (f AdjustFunc) Adjust(node ast.Node, ctx *ParserContext) (Action, error)
Adjust implements NodeAdjuster for AdjustFunc.
type Adjuster ¶ added in v0.3.0
type Adjuster NodeAdjuster
Adjuster registers a NodeAdjuster to transform AST nodes. Usage: New(Adjuster(yourAdjuster))
type AdjusterChain ¶ added in v0.3.0
type AdjusterChain struct {
Adjusters []NodeAdjuster
}
AdjusterChain applies multiple NodeAdjusters in sequence.
func NewAdjusterChain ¶ added in v0.3.0
func NewAdjusterChain(adjusters ...NodeAdjuster) *AdjusterChain
NewAdjusterChain constructs a chain of NodeAdjusters.
func (*AdjusterChain) Adjust ¶ added in v0.3.0
func (c *AdjusterChain) Adjust(node ast.Node, ctx *ParserContext) (Action, error)
Adjust runs each adjuster on the node in order, passing the parser context. It merges patches and respects removal and replace.
type BasicPolicy ¶ added in v0.3.0
type BasicPolicy struct {
ID string
Order int
Active bool
Kinds []PolicyKind // optional node kinds filter
Contexts []string // optional context kinds (reserved)
Fn func(node ast.Node, ctx *ParserContext) (Action, error)
}
BasicPolicy is a helper policy base with simple filters and a function logic.
func (*BasicPolicy) Apply ¶ added in v0.3.0
func (p *BasicPolicy) Apply(node ast.Node, ctx *ParserContext) (Action, error)
func (*BasicPolicy) Enabled ¶ added in v0.3.0
func (p *BasicPolicy) Enabled() bool
func (*BasicPolicy) Match ¶ added in v0.3.0
func (p *BasicPolicy) Match(node ast.Node, ctx *ParserContext) bool
func (*BasicPolicy) Name ¶ added in v0.3.0
func (p *BasicPolicy) Name() string
func (*BasicPolicy) Priority ¶ added in v0.3.0
func (p *BasicPolicy) Priority() int
type CacheSize ¶
type CacheSize int
CacheSize represents cache size in case of the dynamic template evaluation
type CycleDetector ¶
type CycleDetector struct {
// contains filtered or unexported fields
}
func NewCycleDetector ¶
func NewCycleDetector(rType reflect.Type) *CycleDetector
func (*CycleDetector) Child ¶
func (c *CycleDetector) Child(rType reflect.Type, parentSelector *op.Selector) (next *CycleDetector, hasCycle bool)
func (*CycleDetector) Has ¶
func (c *CycleDetector) Has(parent *CycleDetector) bool
type ErrorRecord ¶ added in v0.3.0
type ErrorRecord struct {
Severity Severity
Code string
Message string
File string
Span Span
Position Position
Policy string
// Optional node/context info
NodeKind string
NodeID int
// SelectorChain may carry $foo.bar.baz chain segments if available
SelectorChain []string
}
ErrorRecord captures a diagnostic with optional span/position.
type EvalCfg ¶ added in v0.3.0
type EvalCfg EvaluateConfig
Evaluate controls evaluate traversal. EvalCfg registers evaluate traversal configuration.
type EvaluateConfig ¶ added in v0.3.0
type EvaluateConfig struct {
Mode EvaluateMode
DepthLimit int // 0 = unlimited
Safety EvaluateSafety
Whitelist []string // allowed selector IDs for evaluated content
Blacklist []string // disallowed selector IDs
}
EvaluateConfig defines behavior for #evaluate traversal.
type EvaluateMode ¶ added in v0.3.0
type EvaluateMode int
EvaluateMode controls how #evaluate is handled during traversal.
const ( EvalOpaque EvaluateMode = iota EvalInspect EvalRewrite )
type EvaluateSafety ¶ added in v0.3.0
EvaluateSafety defines safety constraints for handling #evaluate content.
type Event ¶ added in v0.3.0
type Event struct {
Type EventType
Node ast.Node
Context *ParserContext
// Optional span/position if available
Span Span
Position Position
// ExprContext describes where this node appears (control/text/etc.).
ExprContext ExprContext
// Occurrence is a 1-based occurrence index for variable-like symbols
// such as selectors ($X) when applicable; otherwise 0.
Occurrence int
}
Event carries parser event data to listeners.
type ExprContext ¶ added in v0.3.0
type ExprContext struct {
Kind ExprContextKind
ArgIdx int16 // for CtxFuncArg, -1 otherwise
}
ExprContext describes the current expression context for a node.
type ExprContextKind ¶ added in v0.3.0
type ExprContextKind uint8
ExprContextKind classifies where an expression/node appears in the template.
const ( CtxUnknown ExprContextKind = iota // Control flow CtxIfCond CtxIfBody CtxElseIfCond CtxElseBody CtxForEachCond CtxForEachBody CtxForLoopInit CtxForLoopCond CtxForLoopPost // Assignment CtxSetLHS CtxSetRHS // Expression vs text CtxAppendExpr CtxPlainText // Function arguments CtxFuncArg // Evaluate expression CtxEvaluate )
type ForEachInfo ¶ added in v0.3.0
type ForEachInfo struct {
Index int // zero-based index
Count int // one-based counter
HasNext bool // true if there is a next element
First bool // true if this is the first element
Last bool // true if this is the last element
}
ForEachInfo holds loop context similar to Apache Velocity $foreach. Fields are accessible as: $foreach.Index, $foreach.Count, $foreach.HasNext, $foreach.First, $foreach.Last
type Listener ¶ added in v0.3.0
type Listener ParserListener
Listener registers a ParserListener to receive parse events. Usage: New(Listener(yourListener))
type MultiCycleDetector ¶
type MultiCycleDetector struct {
// contains filtered or unexported fields
}
func (*MultiCycleDetector) Get ¶
func (d *MultiCycleDetector) Get(c *CycleDetector, rType reflect.Type, parentSelector *op.Selector) (*CycleDetector, bool)
type NodeAdjuster ¶ added in v0.3.0
type NodeAdjuster interface {
// Adjust applies transformations to the given node within the parser context.
// It returns an action or error.
Adjust(node ast.Node, ctx *ParserContext) (Action, error)
}
NodeAdjuster defines a transformation applied to AST nodes.
type PanicOnError ¶
type PanicOnError bool
PanicOnError panics and recover when first error returned.
type ParserContext ¶ added in v0.3.0
type ParserContext struct {
// Current variable scope
Scope *Scope
// NodeStack tracks ancestor nodes
NodeStack []ast.Node
// Source and position mapping (optional)
File string
Source []byte
Reporter *Reporter
// Evaluate handling configuration
EvalConfig EvaluateConfig
EvalDepth int
// contains filtered or unexported fields
}
ParserContext holds parser state, including scope stack and node ancestry.
func (*ParserContext) AddPatches ¶ added in v0.3.0
func (p *ParserContext) AddPatches(ps ...Patch)
AddPatches appends patches to the context accumulator.
func (*ParserContext) BumpOccurrence ¶ added in v0.3.0
func (p *ParserContext) BumpOccurrence(name string) int
BumpOccurrence increments and returns the 1-based occurrence index for name.
func (*ParserContext) CurrentExprContext ¶ added in v0.3.0
func (p *ParserContext) CurrentExprContext() ExprContext
CurrentExprContext returns the innermost expression context, if any.
func (*ParserContext) CurrentNode ¶ added in v0.3.0
func (p *ParserContext) CurrentNode() ast.Node
CurrentNode returns the most recently pushed node or nil if empty.
func (*ParserContext) EnterScope ¶ added in v0.3.0
func (p *ParserContext) EnterScope()
EnterScope pushes a new nested scope on the context.
func (*ParserContext) ExitScope ¶ added in v0.3.0
func (p *ParserContext) ExitScope()
ExitScope pops the current scope, reverting to the parent.
func (*ParserContext) GetSpan ¶ added in v0.3.0
func (p *ParserContext) GetSpan(n ast.Node) (Span, bool)
GetSpan retrieves a span for a node, if any.
func (*ParserContext) HasUnexpandRaw ¶ added in v0.3.0
func (p *ParserContext) HasUnexpandRaw(name string) bool
HasUnexpandRaw reports if a name is marked with UnexpandRaw decorator.
func (*ParserContext) InitSource ¶ added in v0.3.0
func (p *ParserContext) InitSource(file string, src []byte)
InitSource initializes source bytes and precomputes line starts.
func (*ParserContext) IsConst ¶ added in v0.3.0
func (p *ParserContext) IsConst(name string) bool
IsConst reports if a symbol is a known constant.
func (*ParserContext) IsLocal ¶ added in v0.3.0
func (p *ParserContext) IsLocal(name string) bool
IsLocal reports if a symbol is a local variable.
func (*ParserContext) IsNamespace ¶ added in v0.3.0
func (p *ParserContext) IsNamespace(name string) bool
IsNamespace reports if a name is a function namespace.
func (*ParserContext) IsParam ¶ added in v0.3.0
func (p *ParserContext) IsParam(name string) bool
IsParam reports if a symbol is a planner-defined param (root variable).
func (*ParserContext) IsStandaloneFunc ¶ added in v0.3.0
func (p *ParserContext) IsStandaloneFunc(name string) bool
IsStandaloneFunc reports if a name is a registered standalone function.
func (*ParserContext) MarkLocal ¶ added in v0.3.0
func (p *ParserContext) MarkLocal(name string)
MarkLocal records a local variable in the current scope and set.
func (*ParserContext) MarkUnexpandRaw ¶ added in v0.3.0
func (p *ParserContext) MarkUnexpandRaw(name string)
MarkUnexpandRaw marks a name as having UnexpandRaw decorator.
func (*ParserContext) Patches ¶ added in v0.3.0
func (p *ParserContext) Patches() []Patch
Patches returns accumulated patches.
func (*ParserContext) PopExprContext ¶ added in v0.3.0
func (p *ParserContext) PopExprContext()
PopExprContext removes the most recent expression context.
func (*ParserContext) PopNode ¶ added in v0.3.0
func (p *ParserContext) PopNode()
PopNode removes the most recent node from the context stack.
func (*ParserContext) PushExprContext ¶ added in v0.3.0
func (p *ParserContext) PushExprContext(ctx ExprContext)
PushExprContext adds a new expression context on the stack.
func (*ParserContext) PushNode ¶ added in v0.3.0
func (p *ParserContext) PushNode(node ast.Node)
PushNode records a node entry into the context stack.
func (*ParserContext) ResolvePosition ¶ added in v0.3.0
func (p *ParserContext) ResolvePosition(s Span) Position
ResolvePosition converts a span to line/column positions.
func (*ParserContext) SeedSymbols ¶ added in v0.3.0
func (p *ParserContext) SeedSymbols(params, namespaces, funcs, consts []string)
SeedSymbols initializes known params, namespaces, functions, and consts.
func (*ParserContext) SetSpan ¶ added in v0.3.0
func (p *ParserContext) SetSpan(n ast.Node, s Span)
SetSpan registers a span for a node.
func (*ParserContext) VarKind ¶ added in v0.3.0
func (p *ParserContext) VarKind(name string) VarKind
VarKind reports the classification of a symbol within this context.
type ParserListener ¶ added in v0.3.0
type ParserListener interface {
// OnEvent is invoked for each parser event with full event data.
OnEvent(e Event)
}
ParserListener defines hooks for parser events during AST construction. Implementations can perform actions when nodes are entered or exited.
type PlanHooks ¶ added in v0.3.0
type PlanHooks PlannerListener
PlanHooks registers a planning listener to receive binding hooks. Usage: New(PlanHooks(yourPlannerListener))
type Planner ¶
type Planner struct {
*est.Control
Type *est.Type
*op.Functions
// contains filtered or unexported fields
}
func (*Planner) DefineVariable ¶
DefineVariable enrich the Type by adding field with given name. val can be either of the reflect.Type or regular type (i.e. Foo)
func (*Planner) EmbedVariable ¶
EmbedVariable enrich the Type by adding Anonymous field with given name. val can be either of the reflect.Type or regular type (i.e. Foo)
type PlannerListener ¶ added in v0.3.0
type PlannerListener interface {
// OnDefineVariable is called when a planner defines a top-level variable.
OnDefineVariable(name string, rtype reflect.Type)
// OnSelectorResolved is called when an expression resolves to a selector.
OnSelectorResolved(sel *op.Selector)
// OnFunctionBind is called when a function/method is bound.
OnFunctionBind(name string, f *op.Func, receiverType reflect.Type)
// OnForEachResolved is called when a foreach item is resolved with types.
OnForEachResolved(itemName string, itemType, setType reflect.Type)
}
PlannerListener receives hooks during planning/binding (post-parse).
type Policies ¶ added in v0.3.0
type Policies *PolicyRegistry
Policies registers a PolicyRegistry to drive adjustments. Usage: New(Policies(reg)) and then use reg.AsAdjuster() as Adjuster.
type Policy ¶ added in v0.3.0
type Policy interface {
// Name is the policy identifier.
Name() string
// Priority defines policy ordering (lower first).
Priority() int
// Enabled reports whether policy is active.
Enabled() bool
// Match selects nodes/contexts this policy applies to.
Match(node ast.Node, ctx *ParserContext) bool
// Apply executes policy logic and returns an action.
Apply(node ast.Node, ctx *ParserContext) (Action, error)
}
Policy represents a composable rule applied over nodes with metadata.
type PolicyKind ¶ added in v0.3.0
type PolicyKind string
PolicyKind is a string label for node kinds (e.g., "Selector", "Call", "If", "ForEach", ...)
func NodeKindOf ¶ added in v0.3.0
func NodeKindOf(n ast.Node) PolicyKind
NodeKindOf returns a coarse node kind name for policies.
type PolicyRegistry ¶ added in v0.3.0
type PolicyRegistry struct {
// contains filtered or unexported fields
}
PolicyRegistry stores and applies policies in priority order.
func NewPolicyRegistry ¶ added in v0.3.0
func NewPolicyRegistry() *PolicyRegistry
func (*PolicyRegistry) AsAdjuster ¶ added in v0.3.0
func (r *PolicyRegistry) AsAdjuster() NodeAdjuster
AsAdjuster builds a NodeAdjuster that evaluates registered policies.
func (*PolicyRegistry) Enable ¶ added in v0.3.0
func (r *PolicyRegistry) Enable(name string, on bool)
Enable toggles a policy by name when it implements BasicPolicy convention.
func (*PolicyRegistry) Policies ¶ added in v0.3.0
func (r *PolicyRegistry) Policies() []Policy
func (*PolicyRegistry) Register ¶ added in v0.3.0
func (r *PolicyRegistry) Register(p Policy)
Register adds or replaces a policy by name.
type Reporter ¶ added in v0.3.0
type Reporter struct {
// contains filtered or unexported fields
}
Reporter accumulates diagnostics and can flush them to a sink.
func NewReporter ¶ added in v0.3.0
func NewReporter() *Reporter
func (*Reporter) Records ¶ added in v0.3.0
func (r *Reporter) Records() []ErrorRecord
Records returns current diagnostics.
func (*Reporter) Report ¶ added in v0.3.0
func (r *Reporter) Report(rec ErrorRecord)
Report appends a diagnostic record.
type Scope ¶ added in v0.3.0
Scope represents a lexical scope with variables and an optional parent scope.
type SymbolSeeds ¶ added in v0.3.0
applyParserHooksWithConfig initializes context with source and evaluate config, then traverses.
type TypeParser ¶
type TypeParser = functions.TypeParser
TypeParser parses type string representation into reflect.Type
Source Files
¶
- adjuster.go
- binary.go
- block.go
- cache.go
- compile.go
- const.go
- cyclic.go
- doc.go
- evaluate.go
- expr.go
- foreach_info.go
- init.go
- listener.go
- literal.go
- options.go
- parser_context.go
- parser_hooks.go
- patches.go
- planner.go
- planner_listener.go
- policy.go
- pool.go
- range.go
- registry.go
- reporter.go
- selector.go
- stmt.go
- tag.go
- transform.go
- unary.go