route

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package route is the leaf authority for craftgo's two path namespaces and the wall between them. The URL side: how a method's final route is assembled (base path + @prefix + method path), the string form of a DSL path, the shape key two colliding routes share, and net/http's pattern-overlap rule. The disk side: `@group`, which decides the output segment a block's generated files land under and never contributes to the URL. The analyzer, the routes/OpenAPI emitters, and the route-conflict detector all read these - one implementation, so the route and the directory the editor diagnoses are byte-for-byte the ones the generated server mounts and codegen writes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanGroupPath added in v1.5.3

func CleanGroupPath(raw string) string

CleanGroupPath normalises a @group value into a relative slash path: it trims surrounding slashes and drops empty segments so "/admin/" and "admin//ops" become "admin" and "admin/ops". Traversal (".", "..") segments are dropped as a defence-in-depth backstop - the semantic phase rejects them outright - so a malformed value reaching codegen can only ever nest deeper inside the output tree, never escape it.

func EffectiveGroup added in v1.5.3

func EffectiveGroup(block *ast.ServiceDecl, primaryGroup string) string

EffectiveGroup returns the @group that applies to one service block. The block's own @group wins; an extend block declaring none inherits the primary block's, so `@group("admin")` on the service covers its extend blocks unless an extend overrides it. Pass the primary block's own group as primaryGroup (it is its own effective group).

func OutputSegment added in v1.5.3

func OutputSegment(svcName, group, fileCase string) string

OutputSegment returns the path segment, under any output base, that holds a service block's generated files. A non-empty @group REPLACES the service-name segment entirely (so `@group("v2")` on any service emits to `<base>/v2/`), giving the author full control of the layout; the ungrouped case falls back to the service directory under the configured file case. The result is a forward-slash path - the group may itself be nested ("admin/ops").

Because the group replaces the service name it is effectively a GLOBAL namespace: two services picking the same group would land in one directory and overwrite each other's routes file. This is the segment the analyser's group-collision check compares, so the directory the editor diagnoses is the directory codegen writes.

func PathString

func PathString(p *ast.Path) string

func PatternsConflict

func PatternsConflict(a, b string) bool

patternsConflict reports whether two same-verb mux patterns overlap with neither strictly more specific - the exact condition net/http rejects. It models craftgo's single-segment wildcards (`{name}`): patterns of different segment counts can never overlap, and at each shared position a literal beats a wildcard. The pair conflicts when one is more specific at some segment AND the other is more specific at another (a cross-over), or when they are the same pattern (every segment ties) - i.e. neither side wins outright.

func Resolve

func Resolve(basePath string, svc *ast.ServiceDecl, m *ast.Method) string

ResolveRoute joins the OpenAPI base path, the service's @prefix, and the method's own path into the single absolute route the server registers and the OpenAPI document advertises. Empty segments are dropped, consecutive slashes collapse, the result always begins with '/', and a pathless method falls back to its kebab-cased name ("Ping" → "/ping"). @group is absent on purpose - it nests generated files on disk, never the URL.

This is THE route authority: the analyzer's path checks and every codegen emitter (routes, OpenAPI paths, route-conflict detection) call it, so the route the editor diagnoses is byte-for-byte the route the server mounts.

func ServiceGroup added in v1.5.3

func ServiceGroup(svc *ast.ServiceDecl) string

ServiceGroup returns the cleaned `@group("a/b")` path declared on a service or `extend service` block, or "" when the decorator is absent. @group is the URL's mirror image: it decides where a block's GENERATED FILES land on disk and never contributes a path segment to the route (see Resolve). Values are normalised through CleanGroupPath; the analyser rejects traversal and non-plain segments outright before codegen or the collision check read them.

func ServicePrefix

func ServicePrefix(svc *ast.ServiceDecl) string

decoratorString returns the first string-literal positional arg of `@name(...)` on the service decl, or "" when absent. Used to read `@prefix` and `@group` without depending on codegen helpers.

func Shape

func Shape(route string) string

Shape strips parameter names from a resolved route string, replacing every `{name}` segment with `{}`. Mirrors PathShape but operates on the already-joined route (post-prefix, post-basePath) that resolveMethodPath produces.

Types

This section is empty.

Jump to

Keyboard shortcuts

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