8 // Copyright 2018 Ernest Micklei. All rights reserved.
9 // Use of this source code is governed by a license
10 // that can be found in the LICENSE file.
12 // PathProcessor is extra behaviour that a Router can provide to extract path parameters from the path.
13 // If a Router does not implement this interface then the default behaviour will be used.
14 type PathProcessor interface {
15 // ExtractParameters gets the path parameters defined in the route and webService from the urlPath
16 ExtractParameters(route *Route, webService *WebService, urlPath string) map[string]string
19 type defaultPathProcessor struct{}
21 // Extract the parameters from the request url path
22 func (d defaultPathProcessor) ExtractParameters(r *Route, _ *WebService, urlPath string) map[string]string {
23 urlParts := tokenizePath(urlPath)
24 pathParameters := map[string]string{}
25 for i, key := range r.pathParts {
27 if i >= len(urlParts) {
32 if strings.HasPrefix(key, "{") { // path-parameter
33 if colon := strings.Index(key, ":"); colon != -1 {
35 regPart := key[colon+1 : len(key)-1]
36 keyPart := key[1:colon]
38 pathParameters[keyPart] = untokenizePath(i, urlParts)
41 pathParameters[keyPart] = value
44 // without enclosing {}
45 pathParameters[key[1:len(key)-1]] = value
52 // Untokenize back into an URL path using the slash separator
53 func untokenizePath(offset int, parts []string) string {
54 var buffer bytes.Buffer
55 for p := offset; p < len(parts); p++ {
56 buffer.WriteString(parts[p])
59 buffer.WriteString("/")
62 return buffer.String()