Updated comments.

This commit is contained in:
Alexander Andreev 2023-08-12 19:19:45 +04:00
parent aba211f3ec
commit a2cb6182e8
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -124,9 +124,8 @@ outer:
if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] {
// Do not allow different param names, because only the first one
// is saved, so a param won't be available by a new name.
//
// I am not the one to judge, but it is a little strange to
// expect different types of param in one place.
// Therefore, it is good to return an error because in this case
// you're doing something wrong.
if path[i+1] != child.endpoint {
return errors.New("param names " + path[i+1] + " and " + child.endpoint + " are differ")
}
@ -208,15 +207,24 @@ func (rr *Router) ServeStatic(path string, root http.FileSystem) error {
})
}
// subPath contains a root path that is being attached in front of a pattern
// passed by a Handler() func.
// subPath attaches a base path in front of a pattern.
//
// It is not a sub-router, it just passes a resulted pattern down to
// a router instance.
type subPath struct {
r *Router
base string
}
// Sub returns a sub-path with a root path, after that you can shorten patterns.
// Sub creates a group of handlers with the same base path.
//
// How to use:
//
// r := httpr.New()
// ...
// s := r.Sub("/api/v1")
// s.Handler(http.MethodGet, "/", func(w, r) {...})
// s.Handler(http.MethodGet, "/section", func(w, r) {...})
func (rr *Router) Sub(base string) *subPath {
if base[len(base)-1] == '/' {
base = base[:len(base)-1]
@ -228,13 +236,12 @@ func (rr *Router) Sub(base string) *subPath {
}
}
// Handler attaches root path to a given pattern and pass it to a router.
// Handler registers a handler for a sub-path.
func (sp *subPath) Handler(method, pattern string, handler http.HandlerFunc) error {
return sp.r.Handler(method, sp.base+pattern, handler)
}
// Param returns a URL parameter (that is set like `/a/b/:key/d`) with a key
// or an empty string if no such parameter found.
// Param returns a URL parameter set with :key, or an empty string if not found.
func Param(r *http.Request, key string) string {
if params := r.Context().Value(ParamsKey).(Params); params != nil {
return params[key]