Documentation
¶
Overview ¶
Package visitor provides a visitor pattern for traversing AsyncAPI 3.0 documents. It operates on high-level models from doc.Model(), using pre-order traversal (visit node before children) with stack-based cycle detection for schemas.
Node types passed to Visit include high-level asyncapi types, high-level base types from libopenapi (SchemaProxy, Schema, Contact, License), and *low.Reference for unresolved references (channel servers, operation channels/messages, etc.). Use a type switch in Visit to handle specific node types.
To stop traversal early without error, return ErrStopTraversal from Visit.
Index ¶
- Variables
- func AppendIndex(ctx context.Context, index int) context.Context
- func AppendPath(ctx context.Context, segment string) context.Context
- func Depth(ctx context.Context) int
- func Parent(ctx context.Context) any
- func Path(ctx context.Context) string
- func Stack(ctx context.Context) map[schemaKey]bool
- func WithDepth(ctx context.Context, depth int) context.Context
- func WithParent(ctx context.Context, parent any) context.Context
- func WithPath(ctx context.Context, path string) context.Context
- func WithStack(ctx context.Context, stack map[schemaKey]bool) context.Context
- type PolymorphicVisitor
- type SchemaVisitor
- type Visitor
- type Walker
Constants ¶
This section is empty.
Variables ¶
var ErrStopTraversal = errors.New("stop traversal")
ErrStopTraversal can be returned from Visit to stop traversal cleanly. This allows visitors to find a specific node and stop without returning an error.
Functions ¶
func AppendIndex ¶
AppendIndex appends an array index to the current path.
func AppendPath ¶
AppendPath appends a segment to the current path and returns a new context. Handles empty root correctly: "" + "info" = "/info" Escapes ~ as ~0 and / as ~1 per RFC 6901. Guards against empty segments to avoid trailing slashes.
func Path ¶
Path returns the current JSON Pointer path (RFC 6901) from context. Root is empty string "". First segment creates "/segment". Example: "/channels/userSignup/messages/signupMessage"
func Stack ¶
Stack returns the current schema recursion stack from context. This is a stack, not a global visited set - entries are removed after LeaveSchema. Returns nil if not set.
func WithParent ¶
WithParent returns a context with the given parent node.
Types ¶
type PolymorphicVisitor ¶
type PolymorphicVisitor interface {
SchemaVisitor
// EnterAllOf is called before walking allOf schemas.
// count is the number of schemas in the allOf array.
EnterAllOf(ctx context.Context, schema *highbase.Schema, count int) error
// LeaveAllOf is called after walking allOf schemas (guaranteed via defer).
LeaveAllOf(ctx context.Context, schema *highbase.Schema, err error)
// EnterOneOf is called before walking oneOf schemas.
EnterOneOf(ctx context.Context, schema *highbase.Schema, count int) error
// LeaveOneOf is called after walking oneOf schemas (guaranteed via defer).
LeaveOneOf(ctx context.Context, schema *highbase.Schema, err error)
// EnterAnyOf is called before walking anyOf schemas.
EnterAnyOf(ctx context.Context, schema *highbase.Schema, count int) error
// LeaveAnyOf is called after walking anyOf schemas (guaranteed via defer).
LeaveAnyOf(ctx context.Context, schema *highbase.Schema, err error)
}
PolymorphicVisitor extends SchemaVisitor with allOf/oneOf/anyOf events. Leave callbacks are guaranteed to fire via defer, even on errors.
type SchemaVisitor ¶
type SchemaVisitor interface {
Visitor
// EnterSchema is called before resolving and walking a SchemaProxy.
// Return an error to skip this schema and its children.
EnterSchema(ctx context.Context, proxy *highbase.SchemaProxy) error
// LeaveSchema is called after walking a schema (guaranteed via defer).
// The err parameter contains any error from walking children.
// The schema parameter may be nil if resolution failed.
LeaveSchema(ctx context.Context, proxy *highbase.SchemaProxy, schema *highbase.Schema, err error)
// SkipCircularRef is called when a circular reference is detected.
// The ref is the reference string that would cause infinite recursion.
SkipCircularRef(ctx context.Context, proxy *highbase.SchemaProxy, ref string) error
}
SchemaVisitor extends Visitor with schema lifecycle events. EnterSchema fires before resolution, LeaveSchema fires after (guaranteed via defer).
type Visitor ¶
type Visitor interface {
// Visit is called for each node in pre-order traversal.
// Return an error to stop traversal.
Visit(ctx context.Context, node any) error
}
Visitor visits AsyncAPI document nodes during traversal. Visit is called in pre-order (before descending into children).