-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
79 lines (66 loc) · 1.13 KB
/
utils.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
package gbfs
import (
"encoding/json"
"sync"
)
// Cache ...
type Cache interface {
Get(k string) (v Feed, ok bool)
Set(k string, v Feed)
}
// InMemoryCache ...
type InMemoryCache struct {
sync.RWMutex
m map[string]Feed
}
// Get ...
func (c *InMemoryCache) Get(k string) (v Feed, ok bool) {
c.RLock()
v, ok = c.m[k]
c.RUnlock()
return
}
// Set ...
func (c *InMemoryCache) Set(k string, v Feed) {
c.Lock()
c.m[k] = v
c.Unlock()
return
}
// NewInMemoryCache ...
func NewInMemoryCache() *InMemoryCache {
return &InMemoryCache{
m: make(map[string]Feed),
}
}
func indexInSlice(n string, h []string) int {
for k, v := range h {
if n == v {
return k
}
}
return -1
}
func inSlice(n string, h []string) bool {
return indexInSlice(n, h) > -1
}
type wrapError struct {
msg string
err error
}
func (e *wrapError) Error() string {
return e.msg
}
func (e *wrapError) Unwrap() error {
return e.err
}
func (e *wrapError) MarshalJSON() ([]byte, error) {
return json.Marshal(e.msg)
}
// NewError Wrap error
func NewError(msg string, err error) error {
if err != nil {
msg = msg + err.Error()
}
return &wrapError{msg, err}
}