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 ¶
- Variables
- func BaseCarriesPrefix(baseURL, prefix string) bool
- func CheckConfigPath(p string) error
- func CountPlaceholders(template string) int
- func IsPlaceholderSegment(seg string) bool
- func IsTemplated(seg string) bool
- func Match(concrete, template string) bool
- func SegmentMatches(concrete, template string) bool
- func Split(p string) []string
- func StripQuery(p string) string
- func ValidateCallerPath(p string) error
- func WithinPrefix(path, prefix string) bool
Constants ¶
This section is empty.
Variables ¶
var ErrNotAPath = errors.New("must be a path")
ErrNotAPath is a configured path that does not start with "/" or carries a fragment.
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 BaseCarriesPrefix ¶ added in v1.153.0
BaseCarriesPrefix reports whether a base URL's path ends with prefix, on a segment boundary since the prefix starts with "/": "/xapi/v1" does not end with "/api/v1". A base URL that carries a connection's required prefix supplies it to every raw path.
func CheckConfigPath ¶ added in v1.153.0
CheckConfigPath holds a path a connection's configuration names (a required prefix, a probe path) to the shape a caller's path has: it starts with "/", carries no fragment, and has no segment ValidateCallerPath refuses; a query is allowed. The error says what is wrong, for the caller to put beside the key.
func CountPlaceholders ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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).
func StripQuery ¶ added in v1.153.0
StripQuery is a path without its "?query" or "#fragment": the component a router matches on. A collection endpoint invoked with query parameters (/v1/orders?limit=100) must still resolve to its operation (#519).
func ValidateCallerPath ¶ added in v1.142.0
ValidateCallerPath refuses a path a caller supplies for the api gateway to append to a connection's base URL, when its shape would let it reach another host or match a route rule the upstream would not see it match. The messages carry the gateway's prefix: they are the gateway's refusals.
func WithinPrefix ¶ added in v1.153.0
WithinPrefix reports whether path is prefix itself or lies beneath it on a segment boundary, so "/api/v10" is not inside "/api/v1".
Types ¶
This section is empty.