-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathrejson.go
310 lines (267 loc) · 8.93 KB
/
rejson.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package rejson
import (
"github.com/nitishm/go-rejson/v4/rjs"
)
type Handler struct {
clientName string
implementation ReJSON
}
func NewReJSONHandler() *Handler {
return &Handler{clientName: rjs.ClientInactive}
}
// ReJSON provides an interface for various Go Redis Clients to implement ReJSON commands
type ReJSON interface {
JSONSet(key, path string, obj interface{}, opts ...rjs.SetOption) (res interface{}, err error)
JSONGet(key, path string, opts ...rjs.GetOption) (res interface{}, err error)
JSONMGet(path string, keys ...string) (res interface{}, err error)
JSONDel(key, path string) (res interface{}, err error)
JSONType(key, path string) (res interface{}, err error)
JSONNumIncrBy(key, path string, number int) (res interface{}, err error)
JSONNumMultBy(key, path string, number int) (res interface{}, err error)
JSONStrAppend(key, path string, jsonstring string) (res interface{}, err error)
JSONStrLen(key, path string) (res interface{}, err error)
JSONArrAppend(key, path string, values ...interface{}) (res interface{}, err error)
JSONArrLen(key, path string) (res interface{}, err error)
JSONArrPop(key, path string, index int) (res interface{}, err error)
JSONArrIndex(key, path string, jsonValue interface{}, optionalRange ...int) (res interface{}, err error)
JSONArrTrim(key, path string, start, end int) (res interface{}, err error)
JSONArrInsert(key, path string, index int, values ...interface{}) (res interface{}, err error)
JSONObjKeys(key, path string) (res interface{}, err error)
JSONObjLen(key, path string) (res interface{}, err error)
JSONDebug(subCmd rjs.DebugSubCommand, key, path string) (res interface{}, err error)
JSONForget(key, path string) (res interface{}, err error)
JSONResp(key, path string) (res interface{}, err error)
}
// JSONSet used to set a json object
//
// ReJSON syntax:
//
// JSON.SET <key> <path> <json>
// [NX | XX]
func (r *Handler) JSONSet(key string, path string, obj interface{}, opts ...rjs.SetOption) (
res interface{}, err error,
) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONSet(key, path, obj, opts...)
}
// JSONGet used to get a json object
//
// ReJSON syntax:
//
// JSON.GET <key>
// [INDENT indentation-string]
// [NEWLINE line-break-string]
// [SPACE space-string]
// [NOESCAPE]
// [path ...]
func (r *Handler) JSONGet(key, path string, opts ...rjs.GetOption) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONGet(key, path, opts...)
}
// JSONMGet used to get path values from multiple keys
//
// ReJSON syntax:
//
// JSON.MGET <key> [key ...] <path>
func (r *Handler) JSONMGet(path string, keys ...string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONMGet(path, keys...)
}
// JSONDel to delete a json object
//
// ReJSON syntax:
//
// JSON.DEL <key> <path>
func (r *Handler) JSONDel(key string, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONDel(key, path)
}
// JSONType to get the type of key or member at path.
//
// ReJSON syntax:
//
// JSON.TYPE <key> [path]
func (r *Handler) JSONType(key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONType(key, path)
}
// JSONNumIncrBy to increment a number by provided amount
//
// ReJSON syntax:
//
// JSON.NUMINCRBY <key> <path> <number>
func (r *Handler) JSONNumIncrBy(key, path string, number int) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONNumIncrBy(key, path, number)
}
// JSONNumMultBy to increment a number by provided amount
//
// ReJSON syntax:
//
// JSON.NUMMULTBY <key> <path> <number>
func (r *Handler) JSONNumMultBy(key, path string, number int) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONNumMultBy(key, path, number)
}
// JSONStrAppend to append a jsonstring to an existing member
//
// ReJSON syntax:
//
// JSON.STRAPPEND <key> [path] <json-string>
func (r *Handler) JSONStrAppend(key, path, jsonstring string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONStrAppend(key, path, jsonstring)
}
// JSONStrLen to return the length of a string member
//
// ReJSON syntax:
//
// JSON.STRLEN <key> [path]
func (r *Handler) JSONStrLen(key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONStrLen(key, path)
}
// JSONArrAppend to append json value into array at path
//
// ReJSON syntax:
//
// JSON.ARRAPPEND <key> <path> <json> [json ...]
func (r *Handler) JSONArrAppend(key, path string, values ...interface{}) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONArrAppend(key, path, values...)
}
// JSONArrLen returns the length of the json array at path
//
// ReJSON syntax:
//
// JSON.ARRLEN <key> [path]
func (r *Handler) JSONArrLen(key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONArrLen(key, path)
}
// JSONArrPop removes and returns element from the index in the array
// to pop last element use rejson.PopArrLast
//
// ReJSON syntax:
//
// JSON.ARRPOP <key> [path [index]]
func (r *Handler) JSONArrPop(key, path string, index int) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONArrPop(key, path, index)
}
// JSONArrIndex returns the index of the json element provided and return -1 if element is not present
//
// ReJSON syntax:
//
// JSON.ARRINDEX <key> <path> <json-scalar> [start [stop]]
func (r *Handler) JSONArrIndex(key, path string, jsonValue interface{}, optionalRange ...int) (
res interface{}, err error,
) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONArrIndex(key, path, jsonValue, optionalRange...)
}
// JSONArrTrim trims an array so that it contains only the specified inclusive range of elements
//
// ReJSON syntax:
//
// JSON.ARRTRIM <key> <path> <start> <stop>
func (r *Handler) JSONArrTrim(key, path string, start, end int) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONArrTrim(key, path, start, end)
}
// JSONArrInsert inserts the json value(s) into the array at path before the index (shifts to the right).
//
// ReJSON syntax:
//
// JSON.ARRINSERT <key> <path> <index> <json> [json ...]
func (r *Handler) JSONArrInsert(key, path string, index int, values ...interface{}) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONArrInsert(key, path, index, values...)
}
// JSONObjKeys returns the keys in the object that's referenced by path
//
// ReJSON syntax:
//
// JSON.OBJKEYS <key> [path]
func (r *Handler) JSONObjKeys(key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONObjKeys(key, path)
}
// JSONObjLen report the number of keys in the JSON Object at path in key
//
// ReJSON syntax:
//
// JSON.OBJLEN <key> [path]
func (r *Handler) JSONObjLen(key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONObjLen(key, path)
}
// JSONDebug reports information
//
// ReJSON syntax:
//
// JSON.DEBUG <subcommand & arguments>
// JSON.DEBUG MEMORY <key> [path] - report the memory usage in bytes of a value. path defaults to root if not provided.
// JSON.DEBUG HELP - reply with a helpful message
func (r *Handler) JSONDebug(subCmd rjs.DebugSubCommand, key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONDebug(subCmd, key, path)
}
// JSONForget is an alias for JSONDel
//
// ReJSON syntax:
//
// JSON.FORGET <key> [path]
func (r *Handler) JSONForget(key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONForget(key, path)
}
// JSONResp returns the JSON in key in Redis Serialization Protocol (RESP).
//
// ReJSON syntax:
//
// JSON.RESP <key> [path]
func (r *Handler) JSONResp(key, path string) (res interface{}, err error) {
if r.clientName == rjs.ClientInactive {
return nil, rjs.ErrNoClientSet
}
return r.implementation.JSONResp(key, path)
}