~samwhited/mux

ref: v0.0.4 mux/params.go -rw-r--r-- 1.1 KiB
fee92349Sam Whited mux: add changelog and release v0.0.4 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package mux

import (
	"net/http"
)

// ctxParam is a type used for context keys that contain route parameters.
type ctxParam string

// ParamInfo represents a route parameter and related metadata.
type ParamInfo struct {
	// The parsed value of the parameter (for example int64(10))
	// If and only if no such parameter existed on the route, Value will be nil.
	Value interface{}
	// The raw value of the parameter (for example "10")
	Raw string
	// The name of the route component that the parameter was matched against (for
	// example "name" in "{name int}")
	Name string
	// Type type of the route component that the parameter was matched against
	// (for example "int" in "{name int}")
	Type string

	// offset is the number of the component in the route. Eg. a param foo in the
	// route /{foo int} has offset 1 (zero being the root node, which is never a
	// parameter).
	offset uint
}

// Param returns the named route parameter from the requests context.
func Param(r *http.Request, name string) ParamInfo {
	v := r.Context().Value(ctxParam(name))
	pinfo, _ := v.(ParamInfo)
	return pinfo
}