framework

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package framework is what the router of one HTTP framework needs from the generator, and what every framework shares.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPattern = errors.New("the router rejects the path")
	ErrMethod  = errors.New("the router does not take the method")
)

Functions

func Brace

func Brace(path string, name, wildcard func(string) string) (string, error)

Brace writes path for a router that takes {name} too, with names and a trailing /* as given.

func Check

func Check(path string) error

Check fails on what no router takes: a path without a leading slash, a { without its } and a * that is not last.

func CheckClean

func CheckClean(p string) error

CheckClean fails on a path that is not clean, such as /a//b; a trailing slash is clean.

func CheckMethod

func CheckMethod(method string) error

CheckMethod fails on a method a router with one function per method has none for, such as QUERY or one a spec adds on its own.

func ConflictsByKey

func ConflictsByKey(routes []Route, key func(Route) string) ([]Route, []Conflict)

ConflictsByKey drops every route that has the key of an earlier one, with the reason. Routes of one key match the same requests on the router, so the later one would replace or shadow the earlier one without a word.

func ConflictsByShape

func ConflictsByShape(routes []Route) ([]Route, []Conflict)

ConflictsByShape drops every route that has the method and the shape of an earlier one's path: a repeat of it, or one whose path parameters are named otherwise. It serves routers that key parameters by position and let a later route replace an earlier one.

func Escaping

func Escaping(special string) func(string) (string, error)

Escaping writes a literal with each of the characters in special escaped by a backslash, as routers that read them as the start of a parameter take a literal one.

func EscapingColon

func EscapingColon(s string) (string, error)

EscapingColon escapes the colons of a literal, and fails on a leading colon or a star.

func Identifier

func Identifier(name string) string

Identifier is name as a router that takes Go identifiers holds it: any other character becomes an underscore, and a leading digit gets one in front.

func Params

func Params(path string) []string

Params lists the path parameters of a path, in order.

func Rejecting

func Rejecting(special string) func(string) (string, error)

Rejecting writes a literal as it is, or fails when it holds one of the characters in special, which the router reads as the start of a parameter and cannot escape.

func Rename

func Rename(path string, name func(string) string) (string, []string, error)

Rename writes each {name} of path as name gives it; a missing, repeated or merged name fails.

func RestName

func RestName(taken []string) string

RestName names the value a trailing * takes the rest of the path into: rest, with an underscore added for as long as a parameter of the path has the name.

func Same

func Same(name string) string

Same writes a name as it is.

func Shape

func Shape(pattern string) string

Shape is a pattern with its parameter names blanked, so patterns that differ in the names of their parameters alone compare equal.

func Unmarked

func Unmarked(name string) string

Unmarked writes each colon and star of name, which routers read as a pattern, as an underscore.

Types

type Colon

type Colon struct {
	Literal         func(s string) (string, error)
	Name            func(name string) string
	Wildcard        string
	IsWildcardNamed bool
	IsPrefixAllowed bool
}

Colon writes routes for a router that takes parameters as :name, running to the end of their segment. Literal writes a literal piece of the path, or fails for one the router cannot hold; Name writes the name of a parameter; Wildcard is what a trailing /* becomes. A parameter may follow a literal prefix in its segment when IsPrefixAllowed is set. IsWildcardNamed adds a free name to the wildcard.

func (Colon) Pattern

func (c Colon) Pattern(path string) (string, error)

Pattern writes path as the router takes it. Besides what Check fails on, it fails on a parameter with a suffix or one that shares its segment with another, a parameter with a prefix unless IsPrefixAllowed, a parameter without a name or named twice, a * that is not a segment of its own, and a literal Literal rejects. It also fails on two names Name makes one.

type Conflict

type Conflict struct {
	Route  Route
	Reason string
}

Conflict is a route the router cannot hold next to an earlier one.

type Framework

type Framework interface {
	Name() string
	Imports() []gomodel.Import
	// RoutePattern writes an operation's method and OpenAPI path as the router takes them, or
	// fails for a method or a path the router rejects.
	RoutePattern(method, path string) (string, error)
	// Conflicts drops every route the router cannot hold next to an earlier one, with the reason.
	Conflicts(routes []Route) ([]Route, []Conflict)
	// Handler is the shape of the adapter's handlers, with the framework's types written as the
	// file of s spells them.
	Handler(s *gocode.Scope) Handler
	// PathParam writes the expression that reads a path parameter in a handler, where r is the
	// request and c the framework's context, when the handler has one.
	PathParam(s *gocode.Scope, name string) string
	// Templates holds templates/router.tmpl, the template of the router part.
	Templates() fs.FS
}

Framework is one HTTP framework a router is generated for.

type Handler

type Handler struct {
	Signature             string
	Writer                string
	Request               string
	Epilogue              string
	ServeSignature        string
	Context               string
	ContextServeSignature string
}

Handler is the shape of the handlers the adapter gives a framework. Signature follows the handler's name: its parameters, and its result when it has one. Writer and Request are the response writer and the request the handler has; Epilogue is the statement that ends it, empty when it needs none. ServeSignature is the shape of the method that serves an operation inside the operation middleware, which takes w and r. Context is the framework's context, when the handler has one; ContextServeSignature then takes it before w and r, for an operation that reads its path parameters from it.

func ContextHandler

func ContextHandler(s *gocode.Scope, ctxType string) Handler

ContextHandler is the shape of a handler that takes the router's context and returns nil.

func HTTPHandler

func HTTPHandler(s *gocode.Scope) Handler

HTTPHandler is the handler shape of an http.HandlerFunc.

type Route

type Route struct {
	Operation string
	Method    string
	Path      string
	Pattern   string
	Names     []string
}

Route is one operation on the router. Pattern is the route as the framework writes it. Names, when set, name its path values in the order the router holds them.

func StaticFirst

func StaticFirst(routes []Route) []Route

StaticFirst orders routes for a router that takes the first route that matches: at each segment a literal comes before a parameter and a parameter before a wildcard, and routes that tie keep their order, so no route shadows a more specific one after it. A parameter next to a literal comes before one alone, the longer literal first.

Directories

Path Synopsis
Package beego is the router for github.com/beego/beego/v2.
Package beego is the router for github.com/beego/beego/v2.
Package chi is the router for github.com/go-chi/chi.
Package chi is the router for github.com/go-chi/chi.
Package echo is the router for github.com/labstack/echo, whose handlers take an echo.Context and return an error.
Package echo is the router for github.com/labstack/echo, whose handlers take an echo.Context and return an error.
Package echov5 is the router for github.com/labstack/echo/v5, whose handlers take an *echo.Context and return an error.
Package echov5 is the router for github.com/labstack/echo/v5, whose handlers take an *echo.Context and return an error.
Package fasthttp is the router for github.com/fasthttp/router over github.com/valyala/fasthttp.
Package fasthttp is the router for github.com/fasthttp/router over github.com/valyala/fasthttp.
Package fiber is the router for github.com/gofiber/fiber/v3.
Package fiber is the router for github.com/gofiber/fiber/v3.
Package gin is the router for github.com/gin-gonic/gin.
Package gin is the router for github.com/gin-gonic/gin.
Package goframe is the router for github.com/gogf/gf/v2.
Package goframe is the router for github.com/gogf/gf/v2.
Package gorillamux is the router for github.com/gorilla/mux.
Package gorillamux is the router for github.com/gorilla/mux.
Package gozero is the router for the rest package of github.com/zeromicro/go-zero: the router of its rest server, which rest.WithRouter gives one.
Package gozero is the router for the rest package of github.com/zeromicro/go-zero: the router of its rest server, which rest.WithRouter gives one.
Package hertz is the router for github.com/cloudwego/hertz.
Package hertz is the router for github.com/cloudwego/hertz.
Package iris is the router for github.com/kataras/iris/v12.
Package iris is the router for github.com/kataras/iris/v12.
Package kratos is the router for the HTTP transport of github.com/go-kratos/kratos/v2, whose handlers take a kratos Context and return an error.
Package kratos is the router for the HTTP transport of github.com/go-kratos/kratos/v2, whose handlers take a kratos Context and return an error.
Package stdhttp is the router for http.ServeMux of the standard library, with its method and wildcard patterns.
Package stdhttp is the router for http.ServeMux of the standard library, with its method and wildcard patterns.

Jump to

Keyboard shortcuts

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