This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
context.go
285 lines (224 loc) · 5.78 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package router
import (
"time"
"github.com/hyperledger/fabric-chaincode-go/pkg/cid"
"github.com/hyperledger/fabric-chaincode-go/shim"
"go.uber.org/zap"
"github.com/s7techlab/cckit/state"
)
// DefaultParam default parameter name
const DefaultParam = `default`
type (
// Context of chaincode invoke
Context interface {
Clone() Context
Stub() shim.ChaincodeStubInterface
// Client returns invoker ClientIdentity
Client() (cid.ClientIdentity, error)
// Response returns response builder
Response() Response
Logger() *zap.Logger
Path() string
Handler() *HandlerMeta
SetHandler(*HandlerMeta)
State() state.State
UseState(state.State) Context
// Time returns txTimestamp
Time() (time.Time, error)
ReplaceArgs(args [][]byte) Context // replace args, for usage in preMiddleware
GetArgs() [][]byte
// Deprecated: Use Params instead.
Args() InterfaceMap
// Deprecated: Use Arg instead.
Arg(string) interface{}
// Deprecated: Use ParamString instead.
ArgString(string) string
// Deprecated: Use ParamBytes instead.
ArgBytes(string) []byte
// Deprecated: Use ParamInt instead.
ArgInt(string) int
// Deprecated: Use SetParam instead.
SetArg(string, interface{})
// Params returns parameter values.
Params() InterfaceMap
// Param returns parameter value.
Param(name ...string) interface{}
// ParamString returns parameter value as string.
ParamString(name string) string
// ParamBytes returns parameter value as bytes.
ParamBytes(name string) []byte
// ParamInt returns parameter value as int.
ParamInt(name string) int
// ParamInt32 returns parameter value as int32.
ParamInt32(name string) int32
// SetParam sets parameter value.
SetParam(name string, value interface{})
// Get retrieves data from the context.
Get(key string) interface{}
// Set saves data in the context.
Set(key string, value interface{})
// Deprecated: Use Event().Set() instead
SetEvent(string, interface{}) error
Event() state.Event
UseEvent(state.Event) Context
}
context struct {
stub shim.ChaincodeStubInterface
handler *HandlerMeta
logger *zap.Logger
state state.State
event state.Event
args [][]byte
params InterfaceMap
store InterfaceMap
}
)
// NewContext creates new instance of router.Context
func NewContext(stub shim.ChaincodeStubInterface, logger *zap.Logger) *context {
return &context{
stub: stub,
logger: logger,
}
}
func (c *context) Clone() Context {
ctx := NewContext(c.stub, c.logger)
if c.state != nil {
ctx.state = c.state.Clone()
}
return ctx
}
func (c *context) Stub() shim.ChaincodeStubInterface {
return c.stub
}
func (c *context) Client() (cid.ClientIdentity, error) {
return cid.New(c.Stub())
}
func (c *context) Response() Response {
return ContextResponse{c}
}
func (c *context) Logger() *zap.Logger {
return c.logger
}
func (c *context) Path() string {
if len(c.GetArgs()) == 0 {
return ``
}
return string(c.GetArgs()[0])
}
func (c *context) Handler() *HandlerMeta {
return c.handler
}
func (c *context) SetHandler(h *HandlerMeta) {
c.handler = h
}
func (c *context) State() state.State {
if c.state == nil {
c.state = state.NewState(c.stub, c.logger)
}
return c.state
}
func (c *context) UseState(s state.State) Context {
c.state = s
return c
}
func (c *context) Event() state.Event {
if c.event == nil {
c.event = state.NewEvent(c.stub)
}
return c.event
}
func (c *context) UseEvent(e state.Event) Context {
c.event = e
return c
}
func (c *context) Time() (time.Time, error) {
txTimestamp, err := c.stub.GetTxTimestamp()
if err != nil {
return time.Unix(0, 0), err
}
return time.Unix(txTimestamp.GetSeconds(), int64(txTimestamp.GetNanos())), nil
}
// ReplaceArgs replace args, for usage in preMiddleware
func (c *context) ReplaceArgs(args [][]byte) Context {
c.args = args
return c
}
func (c *context) GetArgs() [][]byte {
if c.args != nil {
return c.args
}
return c.stub.GetArgs()
}
// Deprecated: Use Params instead.
func (c *context) Args() InterfaceMap {
return c.Params()
}
func (c *context) Params() InterfaceMap {
return c.params
}
// Deprecated: Use SetParam instead.
func (c *context) SetArg(name string, value interface{}) {
c.SetParam(name, value)
}
func (c *context) SetParam(name string, value interface{}) {
if c.params == nil {
c.params = make(InterfaceMap)
}
c.params[name] = value
}
// Deprecated: Use Param instead.
func (c *context) Arg(name string) interface{} {
return c.Param(name)
}
func (c *context) Param(name ...string) interface{} {
var pName = DefaultParam
if len(name) > 0 {
pName = name[0]
}
return c.params[pName]
}
// Deprecated: Use ParamString instead.
func (c *context) ArgString(name string) string {
return c.ParamString(name)
}
func (c *context) ParamString(name string) string {
out, _ := c.Param(name).(string)
return out
}
// Deprecated: Use ParamBytes instead.
func (c *context) ArgBytes(name string) []byte {
return c.ParamBytes(name)
}
func (c *context) ParamBytes(name string) []byte {
out, _ := c.Param(name).([]byte)
return out
}
// Deprecated: Use ParamInt instead.
func (c *context) ArgInt(name string) int {
return c.ParamInt(name)
}
func (c *context) ParamInt(name string) int {
out, _ := c.Param(name).(int)
return out
}
// ParamInt32 returns parameter value as int32.
func (c *context) ParamInt32(name string) int32 {
out, _ := c.Param(name).(int32)
return out
}
func (c *context) Set(key string, val interface{}) {
if c.store == nil {
c.store = make(InterfaceMap)
}
c.store[key] = val
}
func (c *context) Get(key string) interface{} {
return c.store[key]
}
func (c *context) SetEvent(name string, payload interface{}) error {
return c.Event().Set(name, payload)
}
func ContextWithStateCache(ctx Context) Context {
clone := ctx.Clone()
return clone.UseState(state.WithCache(clone.State()))
}