pathutil

package
v1.51.5 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package pathutil provides efficient path building utilities for OpenAPI document traversal.

The primary type is PathBuilder, which uses push/pop semantics to build paths incrementally without allocating intermediate strings. This is particularly useful in recursive traversal where paths are built on each recursive call but only used when reporting errors or differences.

PathBuilder Usage

Use Get to obtain a pooled PathBuilder, and Put to return it:

path := pathutil.Get()
defer pathutil.Put(path)

path.Push("properties")
path.Push(propName)
// ... recurse ...
path.Pop()
path.Pop()

// Only call String() when needed (e.g., reporting an error)
if hasError {
    return fmt.Errorf("error at %s", path.String())
}

Array indices are supported via PathBuilder.PushIndex:

path.Push("items")
path.PushIndex(0)  // produces "items[0]"

Reference Builders

The package also provides functions for building JSON Pointer references to OpenAPI components:

ref := pathutil.SchemaRef("Pet")      // "#/components/schemas/Pet"
ref := pathutil.DefinitionRef("Pet")  // "#/definitions/Pet"

These use simple string concatenation which Go optimizes well for two operands, avoiding the overhead of fmt.Sprintf.

Version-aware helpers handle OAS 2.0 vs 3.x differences:

ref := pathutil.ParameterRef("limit", true)   // "#/parameters/limit" (OAS 2.0)
ref := pathutil.ParameterRef("limit", false)  // "#/components/parameters/limit" (OAS 3.x)

Index

Constants

View Source
const (
	RefPrefixDefinitions         = "#/definitions/"
	RefPrefixParameters          = "#/parameters/"
	RefPrefixResponses           = "#/responses/"
	RefPrefixSecurityDefinitions = "#/securityDefinitions/"
)

OAS 2.0 reference prefixes

View Source
const (
	RefPrefixSchemas         = "#/components/schemas/"
	RefPrefixParameters3     = "#/components/parameters/"
	RefPrefixResponses3      = "#/components/responses/"
	RefPrefixExamples        = "#/components/examples/"
	RefPrefixRequestBodies   = "#/components/requestBodies/"
	RefPrefixHeaders         = "#/components/headers/"
	RefPrefixSecuritySchemes = "#/components/securitySchemes/"
	RefPrefixLinks           = "#/components/links/"
	RefPrefixCallbacks       = "#/components/callbacks/"
	RefPrefixPathItems       = "#/components/pathItems/"
)

OAS 3.x reference prefixes

Variables

View Source
var PathParamRegex = regexp.MustCompile(`\{([^}]+)\}`)

PathParamRegex matches path template parameters like {paramName}. It captures the parameter name inside the braces.

Functions

func CallbackRef

func CallbackRef(name string) string

CallbackRef builds "#/components/callbacks/{name}" (OAS 3.x only).

func DefinitionRef

func DefinitionRef(name string) string

DefinitionRef builds "#/definitions/{name}" (OAS 2.0).

func ExampleRef

func ExampleRef(name string) string

ExampleRef builds "#/components/examples/{name}" (OAS 3.x only).

func HeaderRef

func HeaderRef(name string) string

HeaderRef builds "#/components/headers/{name}" (OAS 3.x only).

func LinkRef

func LinkRef(name string) string

LinkRef builds "#/components/links/{name}" (OAS 3.x only).

func ParameterRef

func ParameterRef(name string, oas2 bool) string

ParameterRef builds the appropriate parameter ref. If oas2 is true, returns "#/parameters/{name}", otherwise "#/components/parameters/{name}".

func PathItemRef

func PathItemRef(name string) string

PathItemRef builds "#/components/pathItems/{name}" (OAS 3.1+ only).

func Put

func Put(p *PathBuilder)

Put returns a PathBuilder to the pool if not oversized.

func RequestBodyRef

func RequestBodyRef(name string) string

RequestBodyRef builds "#/components/requestBodies/{name}" (OAS 3.x only).

func ResponseRef

func ResponseRef(name string, oas2 bool) string

ResponseRef builds the appropriate response ref. If oas2 is true, returns "#/responses/{name}", otherwise "#/components/responses/{name}".

func SchemaRef

func SchemaRef(name string) string

SchemaRef builds "#/components/schemas/{name}" (OAS 3.x).

func SecuritySchemeRef

func SecuritySchemeRef(name string, oas2 bool) string

SecuritySchemeRef builds the appropriate security scheme ref. If oas2 is true, returns "#/securityDefinitions/{name}", otherwise "#/components/securitySchemes/{name}".

Types

type PathBuilder

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

PathBuilder provides efficient incremental path construction. Uses push/pop semantics to avoid allocations during traversal. The full string is only materialized when String() is called.

func Get

func Get() *PathBuilder

Get retrieves a PathBuilder from the pool, reset and ready to use.

func (*PathBuilder) Pop

func (p *PathBuilder) Pop()

Pop removes the last segment.

func (*PathBuilder) Push

func (p *PathBuilder) Push(segment string)

Push adds a segment to the path.

func (*PathBuilder) PushIndex

func (p *PathBuilder) PushIndex(i int)

PushIndex adds an array index segment: "[0]", "[1]", etc.

func (*PathBuilder) Reset

func (p *PathBuilder) Reset()

Reset clears the builder for reuse.

func (*PathBuilder) String

func (p *PathBuilder) String() string

String materializes the full path. Only call when the path is needed.

Jump to

Keyboard shortcuts

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