forked from hprose/hprose-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
206 lines (185 loc) · 5.47 KB
/
context.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* rpc/context.go *
* *
* hprose context for Go. *
* *
* LastModified: Nov 1, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package rpc
// Context is the hprose context
type Context interface {
UserData() map[string]interface{}
GetInt(key string, defaultValue ...int) int
GetUInt(key string, defaultValue ...uint) uint
GetInt64(key string, defaultValue ...int64) int64
GetUInt64(key string, defaultValue ...uint64) uint64
GetFloat(key string, defaultValue ...float64) float64
GetBool(key string, defaultValue ...bool) bool
GetString(key string, defaultValue ...string) string
GetInterface(key string, defaultValue ...interface{}) interface{}
SetInt(key string, value int)
SetUInt(key string, value uint)
SetInt64(key string, value int64)
SetUInt64(key string, value uint64)
SetFloat(key string, value float64)
SetBool(key string, value bool)
SetString(key string, value string)
SetInterface(key string, value interface{})
}
// BaseContext is the base context
type BaseContext struct {
userData map[string]interface{}
}
// InitBaseContext initializes BaseContext
func (context *BaseContext) InitBaseContext() {
context.userData = make(map[string]interface{})
}
// UserData return the user data
func (context *BaseContext) UserData() map[string]interface{} {
return context.userData
}
// GetInt from hprose context
func (context *BaseContext) GetInt(
key string, defaultValue ...int) int {
if value, ok := context.userData[key]; ok {
if value, ok := value.(int); ok {
return value
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
// GetUInt from hprose context
func (context *BaseContext) GetUInt(
key string, defaultValue ...uint) uint {
if value, ok := context.userData[key]; ok {
if value, ok := value.(uint); ok {
return value
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
// GetInt64 from hprose context
func (context *BaseContext) GetInt64(
key string, defaultValue ...int64) int64 {
if value, ok := context.userData[key]; ok {
if value, ok := value.(int64); ok {
return value
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
// GetUInt64 from hprose context
func (context *BaseContext) GetUInt64(
key string, defaultValue ...uint64) uint64 {
if value, ok := context.userData[key]; ok {
if value, ok := value.(uint64); ok {
return value
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
// GetFloat from hprose context
func (context *BaseContext) GetFloat(
key string, defaultValue ...float64) float64 {
if value, ok := context.userData[key]; ok {
if value, ok := value.(float64); ok {
return value
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
// GetBool from hprose context
func (context *BaseContext) GetBool(
key string, defaultValue ...bool) bool {
if value, ok := context.userData[key]; ok {
if value, ok := value.(bool); ok {
return value
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return false
}
// GetString from hprose context
func (context *BaseContext) GetString(
key string, defaultValue ...string) string {
if value, ok := context.userData[key]; ok {
if value, ok := value.(string); ok {
return value
}
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return ""
}
// GetInterface from hprose context
func (context *BaseContext) GetInterface(
key string, defaultValue ...interface{}) interface{} {
if value, ok := context.userData[key]; ok {
return value
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return nil
}
// SetInt to hprose context
func (context *BaseContext) SetInt(key string, value int) {
context.userData[key] = value
}
// SetUInt to hprose context
func (context *BaseContext) SetUInt(key string, value uint) {
context.userData[key] = value
}
// SetInt64 to hprose context
func (context *BaseContext) SetInt64(key string, value int64) {
context.userData[key] = value
}
// SetUInt64 to hprose context
func (context *BaseContext) SetUInt64(key string, value uint64) {
context.userData[key] = value
}
// SetFloat to hprose context
func (context *BaseContext) SetFloat(key string, value float64) {
context.userData[key] = value
}
// SetBool to hprose context
func (context *BaseContext) SetBool(key string, value bool) {
context.userData[key] = value
}
// SetString to hprose context
func (context *BaseContext) SetString(key string, value string) {
context.userData[key] = value
}
// SetInterface to hprose context
func (context *BaseContext) SetInterface(key string, value interface{}) {
context.userData[key] = value
}