pathtemplate

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package pathtemplate reads the paths of an OpenAPI document, whose segments may hold parameters, such as {id}. The mock generator and the openapi matcher read them alike: the matcher routes a request to the operation whose mocks the generator orders first.

Index

Constants

This section is empty.

Variables

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

Param matches a parameter of a path, such as {id}.

Functions

func Decode

func Decode(s string) string

Decode returns s, text of a path of the spec, decoded, as the server compares a path: a % that starts no escape, such as that of 50%, is a percent sign.

func Fill

func Fill(path string, literal, param func(s string) string) string

Fill returns path, a path of the spec, with literal applied to each text between its parameters and each parameter, such as {id}, replaced by param of it.

func Join

func Join(base, path string) string

Join joins base, a base path, and path, a path under it.

func Templated

func Templated(path string) int

Templated returns the number of segments of path that hold a parameter. Of two paths that match a request, OpenAPI takes the one with fewer, as /pets/mine before /pets/{id}.

Types

type Kind

type Kind int

Kind is how the mocks of a path compare a request's path.

const (
	// Literal compares the request's path, decoded, with [Route.Path], for a path without parameters.
	Literal Kind = iota

	// Segments compares the request's path with [Route.Path], a path template whose parameters are whole segments:
	// as a mock's path_template does it, both split at each / before they are decoded.
	Segments

	// Pattern matches the request's path, decoded, against [Route.Path], a regular expression, for a parameter inside
	// a segment, such as {name}.txt.
	Pattern
)

type Route

type Route struct {
	// Kind is how the mocks compare a request's path.
	Kind Kind

	// Path is what they compare it with: the path decoded, a path template, or a regular expression.
	Path string
	// contains filtered or unexported fields
}

Route is where the mocks of a path answer: how they compare a request's path, and with what.

func NewExample

func NewExample(base, path string, values map[string]string) (*Route, bool)

NewExample returns the route of an example of path, a path of the spec under base, whose parameters, such as {id}, take the values of values, by parameter, decoded: a parameter values lacks stays one. Its mocks match only requests that those of NewRoute match: the ones with those values, or with others for a parameter inside a segment, whose example matches the same path decoded. It returns false when they would match none: a value is empty, or holds a / inside a segment.

func NewRoute

func NewRoute(base, path string) *Route

NewRoute returns the route of path, a path of the spec under base, a base path decoded. An escape of the spec, such as %20, matches the request that sends it.

func (*Route) Match

func (rt *Route) Match(u *url.URL) (map[string]string, bool)

Match returns the values of the parameters of rt in the path of u, a request's URL, decoded, and whether the mocks of rt match it. A parameter takes any text but an empty one, and a slash only sent as %2F in a whole segment.

Jump to

Keyboard shortcuts

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