-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcontext.go
176 lines (155 loc) · 5.06 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
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/context.go *
* *
* hprose context for Go. *
* *
* LastModified: May 24, 2015 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package hprose
// Context is the hprose context
type Context interface {
UserData() map[string]interface{}
GetInt(key string) (value int, ok bool)
GetUInt(key string) (value uint, ok bool)
GetInt64(key string) (value int64, ok bool)
GetUInt64(key string) (value uint64, ok bool)
GetFloat(key string) (value float64, ok bool)
GetBool(key string) (value bool, ok bool)
GetString(key string) (value string, ok bool)
GetInterface(key string) (value interface{}, ok bool)
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 hprose base context
type BaseContext struct {
userData map[string]interface{}
}
// NewBaseContext is the constructor of BaseContext
func NewBaseContext() (context *BaseContext) {
context = new(BaseContext)
context.userData = make(map[string]interface{})
return
}
// 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) (value int, ok bool) {
if value, ok := context.userData[key]; ok {
if value, ok := value.(int); ok {
return value, true
}
}
return 0, false
}
// GetUInt from hprose context
func (context *BaseContext) GetUInt(key string) (value uint, ok bool) {
if value, ok := context.userData[key]; ok {
if value, ok := value.(uint); ok {
return value, true
}
}
return 0, false
}
// GetInt64 from hprose context
func (context *BaseContext) GetInt64(key string) (value int64, ok bool) {
if value, ok := context.userData[key]; ok {
if value, ok := value.(int64); ok {
return value, true
}
}
return 0, false
}
// GetUInt64 from hprose context
func (context *BaseContext) GetUInt64(key string) (value uint64, ok bool) {
if value, ok := context.userData[key]; ok {
if value, ok := value.(uint64); ok {
return value, true
}
}
return 0, false
}
// GetFloat from hprose context
func (context *BaseContext) GetFloat(key string) (value float64, ok bool) {
if value, ok := context.userData[key]; ok {
if value, ok := value.(float64); ok {
return value, true
}
}
return 0, false
}
// GetBool from hprose context
func (context *BaseContext) GetBool(key string) (value bool, ok bool) {
if value, ok := context.userData[key]; ok {
if value, ok := value.(bool); ok {
return value, true
}
}
return false, false
}
// GetString from hprose context
func (context *BaseContext) GetString(key string) (value string, ok bool) {
if value, ok := context.userData[key]; ok {
if value, ok := value.(string); ok {
return value, true
}
}
return "", false
}
// GetInterface from hprose context
func (context *BaseContext) GetInterface(key string) (value interface{}, ok bool) {
if value, ok := context.userData[key]; ok {
return value, true
}
return nil, false
}
// 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
}