-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
53 lines (41 loc) · 1.34 KB
/
service.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
package third_party_api_double
func NewRequestHub() *RequestHub {
requestHub := RequestHub{}
return &requestHub
}
func NewRequest(path string, method string, data map[string]string) Request {
request := Request{path, method, data}
return request
}
type compationFunc func(Request) bool
type Request struct {
Path string
Method string
Data map[string]string
}
type RequestHub struct {
Requests []Request
}
func (requestHub *RequestHub) Store(path string, method string, data map[string]string) {
request := NewRequest(path, method, data)
newRequestSlice := append(requestHub.Requests, request)
requestHub.Requests = newRequestSlice
}
func (requestHub *RequestHub) Reset() {
requestHub.Requests = []Request{}
}
func (requestHub RequestHub) Find_by_path(path string) []Request {
return requestHub.find_by(func(request Request) bool { return request.Path == path })
}
func (requestHub RequestHub) Find_by_path_and_method(path string, method string) []Request {
return requestHub.find_by(func(request Request) bool { return request.Path == path && request.Method == method })
}
func (requestHub RequestHub) find_by(compFunc compationFunc) []Request {
matchRequests := requestHub.Requests[:0]
for _, request := range requestHub.Requests {
if compFunc(request) {
matchRequests = append(matchRequests, request)
}
}
return matchRequests
}