Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / emicklei / go-restful / path_processor.go
1 package restful
2
3 import (
4         "bytes"
5         "strings"
6 )
7
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.
11
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
17 }
18
19 type defaultPathProcessor struct{}
20
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 {
26                 var value string
27                 if i >= len(urlParts) {
28                         value = ""
29                 } else {
30                         value = urlParts[i]
31                 }
32                 if strings.HasPrefix(key, "{") { // path-parameter
33                         if colon := strings.Index(key, ":"); colon != -1 {
34                                 // extract by regex
35                                 regPart := key[colon+1 : len(key)-1]
36                                 keyPart := key[1:colon]
37                                 if regPart == "*" {
38                                         pathParameters[keyPart] = untokenizePath(i, urlParts)
39                                         break
40                                 } else {
41                                         pathParameters[keyPart] = value
42                                 }
43                         } else {
44                                 // without enclosing {}
45                                 pathParameters[key[1:len(key)-1]] = value
46                         }
47                 }
48         }
49         return pathParameters
50 }
51
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])
57                 // do not end
58                 if p < len(parts)-1 {
59                         buffer.WriteString("/")
60                 }
61         }
62         return buffer.String()
63 }