Files
ersteller/doc/2025-08-22-extract-path-params.md
T
2025-08-22 18:33:19 +02:00

47 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ADR: Implement extractPathParams in CommonHtmxRoute
Date: 2025-08-22
## Context
We use a lightweight HTTP routing based on Go 1.22+ pattern-based ServeMux. Our route paths include placeholders in braces, such as `/en/events/{id}`. The HtmxContext provides a `GetPathParam(key string)` method that uses the requests `PathValue(key)` to retrieve values for path parameters.
The `CommonHtmxRoute` type already supports building URLs by replacing placeholders via `ToUrl`. However, the reverse operation — extracting path params from the template to build a `[]HtmxPathParam` based on the current request — had an unimplemented stub: `extractPathParams`.
## Decision
Implement `extractPathParams` to:
- Parse the language-specific path template for placeholders of the form `{name}`.
- Collect unique placeholder names in order of appearance.
- For each placeholder name, call `c.GetPathParam(name)` to obtain its value from the current request context.
- Return the slice of `HtmxPathParam{Key: name, Value: value}`.
This creates a consistent source of truth for path parameters both when generating URLs and when reading them from incoming requests.
## Implementation
- File: `ersteller-lib/htmx_route.go`
- Function: `CommonHtmxRoute.extractPathParams(c HtmxContext) []HtmxPathParam`
- Approach: String scanning to find `{` and `}` pairs, trimming and deduplicating keys; for each key, append `HtmxPathParam{Key: key, Value: c.GetPathParam(key)}` to the results.
- No external dependencies added.
## Alternatives Considered
- Using a regex like `\{([^}]+)\}`. Rejected to avoid introducing a regex dependency and to keep the implementation minimal and allocation-light.
- Parsing the actual request path rather than the template. Rejected because placeholder names must come from the template, and values come from `GetPathParam`.
## Trade-offs
- The simple scanner assumes non-nested, well-formed placeholders and does not validate balanced braces beyond the first missing `}`. This is acceptable since templates are authored by maintainers and validated elsewhere (routing registration).
## Consequences
- Callers that rely on `extractPathParams` now receive a correctly populated parameter list.
- No API changes; only internal behavior is completed.
## Notes
- No UI controls were added/changed, so no CSS changes were required.
- Localization is unaffected since this change does not introduce user-facing strings.