You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
622 B
Go
30 lines
622 B
Go
3 years ago
|
package pathvar
|
||
4 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
4 years ago
|
var pathVars = contextKey("pathVars")
|
||
4 years ago
|
|
||
4 years ago
|
// Vars parses path variables and returns a map.
|
||
4 years ago
|
func Vars(r *http.Request) map[string]string {
|
||
|
vars, ok := r.Context().Value(pathVars).(map[string]string)
|
||
|
if ok {
|
||
|
return vars
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// WithVars writes params into given r and returns a new http.Request.
|
||
|
func WithVars(r *http.Request, params map[string]string) *http.Request {
|
||
4 years ago
|
return r.WithContext(context.WithValue(r.Context(), pathVars, params))
|
||
|
}
|
||
4 years ago
|
|
||
|
type contextKey string
|
||
|
|
||
|
func (c contextKey) String() string {
|
||
3 years ago
|
return "rest/pathvar/context key: " + string(c)
|
||
4 years ago
|
}
|