-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
156 lines (138 loc) · 2.86 KB
/
helpers.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package ergo
import (
"strings"
"github.com/wlMalk/ergo/constants"
"github.com/wlMalk/ergo/validation"
)
func preparePath(path string) string {
path = strings.Trim(path, "/")
if path == "" {
return "/"
}
return "/" + path
}
type paramer interface {
GetParams() map[string]*validation.Param
setParams(map[string]*validation.Param)
GetParamsSlice() []*validation.Param
setParamsSlice(...*validation.Param)
}
// addParams is additive, meaning that it will keep adding
// params as long as they are different in names.
// No two params can share the same name even if they are
// in different places.
func addParams(pa paramer, params []*validation.Param) {
ps := pa.GetParamsSlice()
for _, p := range params {
ps = append(ps, p)
}
pa.setParamsSlice(ps...)
}
func ignoreParams(pa paramer, params []string) {
ps := pa.GetParams()
for _, p := range params {
delete(ps, p)
}
pa.setParams(ps)
}
func ignoreParamsBut(pa paramer, params []string) {
nparams := map[string]*validation.Param{}
ps := pa.GetParams()
for _, p := range params {
n, ok := ps[p]
if ok {
nparams[p] = n
}
}
pa.setParams(nparams)
}
func prepareArgsSlice(args []string, f func(s string) bool) []string {
if len(args) == 0 {
return args
}
var nArgs []string
for _, a := range args {
a = strings.ToLower(a)
duplicate := false
for _, b := range nArgs {
if a == b {
duplicate = true
}
}
if !duplicate && f(a) {
nArgs = append(nArgs, a)
}
}
return nArgs
}
type schemer interface {
GetSchemes() []string
setSchemes([]string)
}
func schemes(s schemer, schemes []string) {
schemes = prepareArgsSlice(schemes, func(scheme string) bool {
if scheme == constants.SCHEME_HTTP ||
scheme == constants.SCHEME_HTTPS {
return true
}
return false
})
if len(schemes) > 0 {
s.setSchemes(schemes)
}
}
type consumer interface {
GetConsumes() []string
setConsumes([]string)
}
func consumes(c consumer, mimes []string) {
mimes = prepareArgsSlice(mimes, func(mime string) bool {
if mime == constants.MIME_JSON ||
mime == constants.MIME_XML {
return true
}
return false
})
if len(mimes) > 0 {
c.setConsumes(mimes)
}
}
type producer interface {
GetProduces() []string
setProduces([]string)
}
func produces(p producer, mimes []string) {
mimes = prepareArgsSlice(mimes, func(mime string) bool {
if mime == constants.MIME_JSON ||
mime == constants.MIME_XML {
return true
}
return false
})
if len(mimes) > 0 {
p.setProduces(mimes)
}
}
func getHandler(h Handler, handlers []Middleware) Handler {
final := h
for i := len(handlers) - 1; i >= 0; i-- {
final = handlers[i].Run(final)
}
return final
}
func containsString(vals []string, a string) bool {
for _, v := range vals {
if a == v {
return true
}
}
return false
}
func containsInt(vals []int, a int) bool {
for _, v := range vals {
if a == v {
return true
}
}
return false
}