go.temporal.io/server/common/routing/route.go
149 LOC · 63 covered · 86 uncovered · 19 ranges · 17487 concepts · 9 introducers · 8723 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
// Package routing provides utilities to define a number of [Route] instances, which can be...
//
// 1. Used by servers to...
//
// 1.a. Register with [github.com/gorilla/mux.Router] instances via the [Route.Representation] method.
//
// 1.b. Deserialize HTTP path variables into a struct with the [Route.Deserialize] method.
//
// 2. Used by clients to construct HTTP paths for requests by calling the [Route.Path] method.
package routing
import (
"net/url"
"strings"
)
// Route represents a series of HTTP path components.
type Route[T any] struct {
components []Component[T]
}
// Component represents a single HTTP path component, either a constant slug or a variable parameter.
type Component[T any] interface {
// Representation is the string representation of the component for usage in a path definition, e.g. "v1" for a
// constant slug or "{namespace}" for a variable. This should be compatible with the format specified in
// the [github.com/gorilla/mux] package.
Representation() string
// Serialize returns the actual value of the slug when used in an HTTP path, e.g. "v1" for a constant slug or
// "test-namespace" for a variable.
Serialize(params T) string
// Deserialize mutates the given params object with the value of the component when parsing an HTTP path, e.g.
// setting the value of a variable to "test-namespace". If the component is a constant slug, this method should
// be a no-op.
Deserialize(vars map[string]string, t *T)
}
// NewRoute returns a new [Route] instance with the given components.
return Route[T]{components: components}
}
// RouteBuilder is a builder for the [Route] interface.
type RouteBuilder[T any] struct {
components []Component[T]
}
// NewBuilder creates a new [RouteBuilder] instance, which can be used to define a new [Route] via a fluent API.
return &RouteBuilder[T]{}
}
// With adds a series of [Component] instances to the [Route].
r.components = append(r.components, c...)
return r
}
// Constant adds a [Constant] component to the [Route].
return r.With(Constant[T](values...))
}
// StringVariable adds a [StringVariable] component to the [Route].
func (r *RouteBuilder[T]) StringVariable(name string, getter func(*T) *string) *RouteBuilder[T] {
route.go ×6
return r.With(StringVariable[T](name, getter))
}
// Build returns a read-only [Route].
return NewRoute[T](r.components...)
}
// Representation returns the [github.com/gorilla/mux] compatible string representation of the route for usage in a
// path definition. It does not add a leading or trailing slash to the representation, but it won't remove them if
// they're present in the components. We do this because it's easier to add a slash depending on the context than to
// remove it.
return r.serialize(func(c Component[T]) string {
return c.Representation()
})
}
// Path returns the serialized path of the route with the given params. There will be no leading or trailing slashes,
// similar to the behavior of the [Route.Representation] method.
return r.serialize(func(c Component[T]) string {
return c.Serialize(t)
})
}
var sb strings.Builder
for i, c := range r.components {
if i > 0 {
sb.WriteString("/")
}
sb.WriteString(f(c))
}
}
// Deserialize the given vars into a new instance of the params type, T.
var t T
for _, c := range r.components {
c.Deserialize(vars, &t)
}
return t
}
// Constant returns a [Component] that represents a series of constant HTTP path components in a Route.
// They will be joined via strings when used to construct a path or path representation.
return values
}
type constant[T any] []string
return strings.Join(s, "/")
}
return strings.Join(s, "/")
}
// StringVariable returns a [Component] that represents a string variable in a Route.
func StringVariable[T any](name string, getter func(*T) *string) stringVariable[T] {
route.go ×1
return stringVariable[T]{name, getter}
}
type stringVariable[T any] struct {
name string
getter func(*T) *string
}
return "{" + s.name + "}"
}
return url.PathEscape(*s.getter(&t))
}
*s.getter(t) = vars[s.name]
}