-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
80 lines (72 loc) · 1.72 KB
/
data.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
package servermock
import (
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"sync"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func storeData(sm *sync.Map, trace string, data Request) {
val, ok := sm.Load(trace)
if !ok {
sm.Store(trace, []Request{data})
return
}
valBytes := val.([]Request)
if len(valBytes) == 0 {
sm.Store(trace, []Request{data})
return
}
sm.Store(trace, append([]Request{data}, valBytes...)) //[n-1] is always the element to be read (and deleted) first
}
func loadAllData(sm *sync.Map, key string) ([]Request, bool) {
val, ok := sm.Load(key)
if !ok {
return nil, false
}
valBytes := val.([]Request)
if len(valBytes) == 0 {
return nil, false
}
return valBytes, true
}
func loadData(sm *sync.Map, method string, f func(string) []string) (Request, error) {
d, ok := loadAllData(sm, method)
if !ok {
return Request{}, status.Error(codes.Unknown, "Unknown request")
}
var entry Request
for i := len(d) - 1; i >= 0; i-- {
dd := d[i]
mdd := f(dd.HeaderKeys.Key)
if reflect.DeepEqual(mdd, dd.HeaderKeys.Val) {
entry = dd
if entry.IsQueue {
sm.Store(method, append(d[:i], d[i+1:]...))
}
break
}
}
return entry, nil
}
func resetData(sm *sync.Map) {
sm.Range(func(key, _ interface{}) bool {
sm.Delete(key)
return true
})
}
func setData(wr http.ResponseWriter, r *http.Request, log Logger, sm *sync.Map, Endpoint string) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log("Error setting Data for request: %s\n", Endpoint)
}
entry := Request{}
if err = json.Unmarshal(b, &entry); err != nil {
wr.WriteHeader(500)
return
}
storeData(sm, Endpoint, entry)
log("Setting Data for request: %s Length: %d\n", Endpoint, len(entry.Body))
}