visitor

package
v0.0.0-...-f2c7550 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func AppendIndex(ctx context.Context, index int) context.Context

AppendIndex appends an array index to the current path.

func AppendPath

func AppendPath(ctx context.Context, segment string) context.Context

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 Depth

func Depth(ctx context.Context) int

Depth returns the current traversal depth from context. Returns 0 if not set.

func Parent

func Parent(ctx context.Context) any

Parent returns the parent node from context. Returns nil if not set or at root.

func Path

func Path(ctx context.Context) string

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

func Stack(ctx context.Context) map[schemaKey]bool

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 WithDepth

func WithDepth(ctx context.Context, depth int) context.Context

WithDepth returns a context with the given depth.

func WithParent

func WithParent(ctx context.Context, parent any) context.Context

WithParent returns a context with the given parent node.

func WithPath

func WithPath(ctx context.Context, path string) context.Context

WithPath returns a context with the given path.

func WithStack

func WithStack(ctx context.Context, stack map[schemaKey]bool) context.Context

WithStack returns a context with the given schema stack.

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).

type Walker

type Walker struct {
	// contains filtered or unexported fields
}

Walker traverses AsyncAPI documents in pre-order (visit node before children).

func NewWalker

func NewWalker(v Visitor) *Walker

NewWalker creates a new Walker with the given visitor.

func (*Walker) Walk

func (w *Walker) Walk(ctx context.Context, doc *asyncapi.AsyncAPI) error

Walk traverses the document starting from the root. Returns an error if the visitor returns an error. ErrStopTraversal is converted to nil, allowing clean early termination.

Jump to

Keyboard shortcuts

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