pathtemplate

package
v1.141.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package pathtemplate matches a concrete request path against an OpenAPI path template: the one rule the api gateway's router, its WebDAV route matcher and its specificity ranking share, so a placeholder means the same thing to each (issues #876, #1297). It knows nothing about connections or specs; it compares strings.

Index

Constants

This section is empty.

Variables

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

Placeholder matches one OpenAPI path-template placeholder. The name class excludes braces so a segment carrying two placeholders ("{latitude},{longitude}") yields two matches rather than one spanning both, which is what made such a segment resolve to a parameter named "latitude},{longitude" that no caller could supply (issue #1297).

Functions

func CountPlaceholders

func CountPlaceholders(template string) int

CountPlaceholders returns the number of placeholders in an OpenAPI path template; used by findMostSpecificPathMatch to prefer literal paths over templated ones when both match the same concrete path. Occurrences are counted rather than segments, so a template with a two-placeholder segment ranks as less specific than one that spends a whole segment per placeholder (issue #1297).

func IsPlaceholderSegment

func IsPlaceholderSegment(seg string) bool

IsPlaceholderSegment reports whether a path-template segment is entirely one OpenAPI parameter placeholder (e.g. "{datasetId}"). A segment that merely contains a placeholder ("{name}.json") is not one: it has literal text that must still match, so it routes through templatedSegmentMatches instead. The interior brace check and the three-character minimum reject the degenerate "{a}{b}" and "{}" segments, which no spec generator emits but a hand-edited spec might.

func IsTemplated

func IsTemplated(seg string) bool

IsTemplated reports whether a path-template segment carries at least one placeholder, whether or not it also carries literal text. Distinct from IsPlaceholderSegment: this is the "not a fixed segment" test specificity ranking needs, where "{lat},{lon}" must count as templated even though it is not a whole-segment placeholder.

func Match

func Match(concrete, template string) bool

Match reports whether concrete (e.g. "/v1/users/42") matches an OpenAPI path template (e.g. "/v1/users/{id}"). Both strings are split on "/" and compared segment-by-segment; bracketed placeholder segments match any non-empty segment, literal segments must match exactly. Trailing slashes are normalized away so "/v1/users/" and "/v1/users" both match the same template.

func SegmentMatches

func SegmentMatches(concrete, template string) bool

SegmentMatches reports whether one concrete path segment satisfies one template segment. Three cases, cheapest first: a literal ("users") must match exactly; a whole-segment placeholder ("{id}") matches any non-empty segment; a partially-templated segment ("{lat},{lon}", "{name}.json") matches when the literal text around its placeholders lines up. The single per-segment rule shared by the exact-length matcher (Match) and the catch-all-tail matcher (webdavRoute.matches) so their placeholder/literal semantics cannot drift (issues #876, #1297).

func Split

func Split(p string) []string

Split splits a leading-slash path or template into segments after trimming a single trailing slash, so "/a/b/" and "/a/b" both yield ["", "a", "b"] and align with template segment counts. Shared by Match and the WebDAV route matcher so both derive segments identically (issue #876).

Types

This section is empty.

Jump to

Keyboard shortcuts

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