forked from hprose/hprose-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
154 lines (133 loc) · 4.39 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
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/context.go *
* *
* hprose tcp service for Go. *
* *
* LastModified: Oct 15, 2014 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package hprose
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{})
}
type BaseContext struct {
userData map[string]interface{}
}
func NewBaseContext() *BaseContext {
return &BaseContext{userData: make(map[string]interface{})}
}
func (context *BaseContext) UserData() map[string]interface{} {
return context.userData
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
func (context *BaseContext) GetInterface(key string) (value interface{}, ok bool) {
if value, ok := context.userData[key]; ok {
return value, true
}
return nil, false
}
func (context *BaseContext) SetInt(key string, value int) {
context.userData[key] = value
}
func (context *BaseContext) SetUInt(key string, value uint) {
context.userData[key] = value
}
func (context *BaseContext) SetInt64(key string, value int64) {
context.userData[key] = value
}
func (context *BaseContext) SetUInt64(key string, value uint64) {
context.userData[key] = value
}
func (context *BaseContext) SetFloat(key string, value float64) {
context.userData[key] = value
}
func (context *BaseContext) SetBool(key string, value bool) {
context.userData[key] = value
}
func (context *BaseContext) SetString(key string, value string) {
context.userData[key] = value
}
func (context *BaseContext) SetInterface(key string, value interface{}) {
context.userData[key] = value
}