This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri.go
98 lines (80 loc) · 1.99 KB
/
uri.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gohttp
import (
"reflect"
"strings"
)
// uri of a request
type uri struct {
fullUri string
baseUri string
params []any
hasTemplatedParams bool
}
// compileUri turns a string uri to a struct
func compileUri(value string) uri {
cleanValue := cleanUri(value)
_uri := uri{fullUri: value}
templatedParams := false
values := strings.Split(cleanValue, "/")
for _, param := range values {
if containsSupportedPlaceHolders(param) {
_uri.params = append(_uri.params, placeHolder{Placeholders[param], nil})
templatedParams = true
} else {
_uri.params = append(_uri.params, param)
}
}
_uri.baseUri = getBaseUri(values)
_uri.hasTemplatedParams = templatedParams
return _uri
}
func getBaseUri(uri []string) string {
output := "/"
for _, param := range uri {
if !containsSupportedPlaceHolders(param) {
output += param
break
}
}
return output
}
func (u *uri) uriMatches(target *uri) bool {
if u.hasTemplatedParams {
//currentUri := compileUri(uri)
if len(target.params) != len(u.params) {
return false
}
for i, _ := range target.params {
//here we basically ignore placeHolders since we can't really validate their value.
//we could validate the type eventually but for now this should be enough
//ex: /test/1234 should match /test/{int} and /test/{int}/{string} should not match /test/2/{string}
if reflect.TypeOf(u.params[i]).Kind() == reflect.String {
if u.params[i] != target.params[i] {
return false
}
} else {
// params only contains placeHolder or string - the casting is technically safe
valueToParse := target.params[i].(string)
paramType := u.params[i].(placeHolder)._type
_, err := parseValue(valueToParse, paramType)
if err != nil {
return false
}
}
}
return true
}
return u.fullUri == target.fullUri
}
func cleanUri(s string) string {
if s == "/" {
return s
}
if s[0] == '/' {
s = s[1:]
}
if s[len(s)-1] == '/' {
s = s[:len(s)-1]
}
return s
}