-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpath_parsing.go
65 lines (45 loc) · 1.27 KB
/
path_parsing.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package goweb
import "strings"
/*
Path parsing
*/
// Tests whether a path segment is dynamic or not
func isDynamicSegment(segment string) bool {
return strings.HasPrefix(segment, "{") && strings.HasSuffix(segment, "}")
}
func isExtensionSegment(segment string) bool {
return strings.HasPrefix(segment, ".")
}
// Splits a path into its segments
func getPathSegments(path string) []string {
ext := getFileExtension(path)
// trim off the extension (if it's there)
if len(ext) > 0 {
path = path[0 : len(path)-(len(ext)+1)]
}
// split path
segments := strings.Split(strings.Trim(path, "/"), "/")
if len(ext) > 0 {
segments = append(segments, "."+ext)
}
return segments
}
// Generates a parameter value map from the specified parameter key map and path
func getParameterValueMap(keys ParameterKeyMap, path string) ParameterValueMap {
var paramValues ParameterValueMap = make(ParameterValueMap)
var segments []string = getPathSegments(path)
for k, index := range keys {
paramValues[k] = segments[index]
}
return paramValues
}
// Gets the file extension (in uppercase) from a path
func getFileExtension(path string) string {
lastDot := strings.LastIndex(path, ".")
if lastDot == -1 {
return "" /* no extension */
} else {
return path[lastDot+1:]
}
return ""
}