Skip to content

v0.2.0 — strict URLFor + chain form + Ref qualified path

Choose a tag to compare

@jackielii jackielii released this 18 May 19:49
· 119 commits to main since this release

Closes #8.

URLFor(ctx, SomeType{}) previously returned the first match silently when the same page type was mounted under multiple parents — production traffic going to the wrong page, no error, no warning. This release fixes that and adds the type-safe disambiguation primitive.

Highlights

Strict URLFor — no opt-out. Bare type lookups that match multiple nodes now error with every match listed. There is no `WithLenientURLFor` escape hatch; silent first-match is always wrong, so disambiguating at the call site is mandatory.

`[]any{...}` chain form. Inside the composition slice, leading typed values now form a chain through the page tree (each typed value descends into a child of that type). Strings still concat literally as URL fragments. Typed values after a string fragment are rejected.

```go
// Before: ambiguous, silently returned first match
url, _ := structpages.URLFor(ctx, entryPage{}, map[string]any{"slug": "button"})

// After: chain disambiguates by anchoring on the unique parent
url, _ := structpages.URLFor(ctx,
[]any{componentsRoot{}, entryPage{}},
map[string]any{"slug": "button"})
// → "/components/button"
```

`Ref` qualified path. `Ref("Parent.Field")` and `Ref("Grand.Parent.Field")` walk down by `PageNode.Name`. Existing `Ref("Name")` and `Ref("/route")` forms unchanged. This is the cross-package fallback for callers that can't import the typed page.

Recommended call shape: `URLFor(ctx, page, params)` with `map[string]any` for params.

Breaking changes

  • Bare `URLFor(ctx, T{})` where `T` is mounted under multiple parents now errors. Migrate to `[]any{parent, T{}}` chain form or `Ref("Parent.Field")`.
  • `[]any{TypedA, TypedB}` previously concatenated each one's FullRoute independently. It now forms a chain through the tree. No known callers in this repo or examples relied on the old semantics; if you did, switch to `[]any{TypedA, "/literal-path"}` or compose the routes manually.

Validation patterns

The new `examples/url-validation/` directory ships the full pattern for keeping URLs from drifting silently in production:

  • `urls.go` — typed helper per URL family
  • `validate.go` — `validateURLs(sp)` exercising every helper at boot; `main.go` calls it after `Mount` so a broken deploy refuses to start serving
  • `integration_test.go` — renders every page and asserts URLs in the body; runs in CI

`skills/structpages/SKILL.md` §3 rewritten with the form table, chain semantics, and the validation pattern.

Migration cookbook

If you hit the strict-mode error in your app:

  1. Bare lookup of a uniquely-mounted type still works — most call sites need no change.
  2. Same type under multiple parents — switch to `[]any{parentType{}, leafType{}}`.
  3. Can't import the target page (cross-package) — switch to `structpages.Ref("Parent.Field")`.
  4. Validate every URL at boot — copy the `validateURLs` pattern from `examples/url-validation/validate.go`.

Full PR discussion: #9