-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx_kv.go
123 lines (94 loc) · 2.26 KB
/
ctx_kv.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
package kit
import "context"
func (ctx *Context) setRoute(route string) *Context {
ctx.route = append(ctx.route[:0], route...)
return ctx
}
func (ctx *Context) Route() string {
return string(ctx.route)
}
func (ctx *Context) setServiceName(s string) *Context {
ctx.serviceName = append(ctx.serviceName[:0], s...)
return ctx
}
func (ctx *Context) ServiceName() string {
return string(ctx.serviceName)
}
func (ctx *Context) setContractID(contractID string) *Context {
ctx.contractID = append(ctx.contractID[:0], contractID...)
return ctx
}
func (ctx *Context) ContractID() string {
return string(ctx.contractID)
}
func (ctx *Context) Set(key string, val any) *Context {
ctx.Lock()
ctx.kv[key] = val
ctx.ctx = context.WithValue(ctx.ctx, key, val)
ctx.Unlock()
return ctx
}
func (ctx *Context) Get(key string) any {
ctx.Lock()
v := ctx.kv[key]
ctx.Unlock()
return v
}
func (ctx *Context) Walk(f func(key string, val any) bool) {
for k, v := range ctx.kv {
if !f(k, v) {
return
}
}
}
func (ctx *Context) Exists(key string) bool {
return ctx.Get(key) != nil
}
func (ctx *Context) GetInt64(key string, defaultValue int64) int64 {
v, ok := ctx.Get(key).(int64)
if !ok {
return defaultValue
}
return v
}
func (ctx *Context) GetInt32(key string, defaultValue int32) int32 {
v, ok := ctx.Get(key).(int32)
if !ok {
return defaultValue
}
return v
}
func (ctx *Context) GetUint64(key string, defaultValue uint64) uint64 {
v, ok := ctx.Get(key).(uint64)
if !ok {
return defaultValue
}
return v
}
func (ctx *Context) GetUint32(key string, defaultValue uint32) uint32 {
v, ok := ctx.Get(key).(uint32)
if !ok {
return defaultValue
}
return v
}
func (ctx *Context) GetString(key string, defaultValue string) string {
v, ok := ctx.Get(key).(string)
if !ok {
return defaultValue
}
return v
}
func (ctx *Context) GetBytes(key string, defaultValue []byte) []byte {
v, ok := ctx.Get(key).([]byte)
if !ok {
return defaultValue
}
return v
}
// LocalStore returns a local store which could be used to share some key-values between different
// requests. If you need to have a shared key-value store between different instances of your server
// then you need to use ClusterStore method.
func (ctx *Context) LocalStore() Store {
return ctx.ls
}