internal

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package internal provides shared helpers used by api/rest and api/events. It is not part of the public API and must not be imported by any non-api package.

Template variable parsing

ParseTemplateVars parses `{varName}` placeholders from a path or topic template and returns the set of variable names. Used by builders to validate [PathParam] and [TopicParam] names against the template at [Register] time.

StripTemplateVars replaces `{varName}` placeholders with `x` before builder-level codec validation — ensuring constraints work correctly on parameterised templates.

BuildFromTemplate substitutes concrete variable values into a template, validating each value against its registered codec. Used by [BuildPath] and [BuildTopic].

Index

Constants

This section is empty.

Variables

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

TemplateVarRe matches {varName} placeholders in path or topic templates.

Functions

func BuildFromTemplate

func BuildFromTemplate(
	template string,
	vars map[string]string,
	paramCodecs map[string]*codex.Codec[string],
	wrapMissing func(name string) error,
	wrapErr func(name, value string, err error) error,
) (string, error)

from vars, validating each against the corresponding codec in paramCodecs. wrapMissing is called to produce a typed error when a template variable has no entry in vars. wrapErr is called to produce a typed error when codec validation fails.

func MatchTemplate added in v0.12.0

func MatchTemplate(template, concrete string, wrapMismatch func(template, concrete string) error) (map[string]string, error)

MatchTemplate matches a concrete path/topic string against a template containing {varName} placeholders and literal text, returning the extracted variable values. Each {varName} placeholder captures everything up to the next "/" (it never crosses a path/topic segment boundary), but — unlike a naive segment-by-segment split — a placeholder MAY share a segment with literal text, e.g. "{date}.json" in "readings/{sensorID}/{date}.json" correctly captures "2024-01-15" from ".../2024-01-15.json". No wildcard support ({+}/{#}-style MQTT semantics) — this is the shared, protocol-agnostic core; adapters/mqtt's matchTopicTemplate keeps its own wildcard handling separately since file paths and REST-shaped templates have no wildcard concept.

Delegates to templatematch.MatchNonWildcard — the shared, module-internal core also used by adapters/zeromq and ports/file.go, which cannot import this api/internal package (Go's internal/ visibility rule restricts it to code rooted at api/'s own subtree). See

Returns wrapMismatch(template, concrete) when the concrete string's structure does not match the template (wrong segment count, or literal text does not match).

func ParseTemplateVars

func ParseTemplateVars(template string) map[string]bool

ParseTemplateVars returns the set of variable names (without braces) found in a path or topic template such as "/users/{id}/posts/{postId}".

func StripTemplateVars added in v0.5.0

func StripTemplateVars(template string) string

StripTemplateVars replaces each {varName} placeholder in template with the literal segment placeholder (the single character "x"). The result has the same structural shape as the template but contains no brace syntax.

This is used internally to make path/topic codec validation template-transparent: constraints run on the shape of the path/topic, not on the literal {varName} tokens.

Types

This section is empty.

Jump to

Keyboard shortcuts

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