-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.go
418 lines (383 loc) · 12.4 KB
/
bridge.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package cblcgo
/*
#cgo LDFLAGS: -L. -lCouchbaseLiteC
#include <stdlib.h>
#include <stdio.h>
#include "include/CouchbaseLite.h"
void gatewayDatabaseChangeGoCallback(void *context, const CBLDatabase* db _cbl_nonnull, unsigned numDocs, const char **docIDs _cbl_nonnull);
void gatewayDocumentChangeGoCallback(void *context, const CBLDatabase* db _cbl_nonnull, const char *docID _cbl_nonnull);
void gatewayQueryChangeGoCallback(void *context, CBLQuery* query _cbl_nonnull);
void notificationReadyCallback(void *context, CBLDatabase* db _cbl_nonnull);
bool gatewayPushFilterCallback(void *context, CBLDocument* doc, bool isDeleted);
bool gatewayPullFilterCallback(void *context, CBLDocument* doc, bool isDeleted);
void gatewayReplicatorChangeCallback(void *context, CBLReplicator *replicator _cbl_nonnull, const CBLReplicatorStatus *status _cbl_nonnull);
void gatewayReplicatedDocumentCallback(void *context, CBLReplicator *replicator _cbl_nonnull, bool isPush, unsigned numDocuments, const CBLReplicatedDocument* documents);
const CBLDocument* gatewayConflictResolverCallback(void *context, const char *documentID, const CBLDocument *localDocument, const CBLDocument *remoteDocument);
char * getDocIDFromArray(char **docIds, unsigned index); // Implemented in database.go
FLValue FLArray_AsValue(FLArray);
FLValue FLDict_AsValue(FLDict);
bool is_Null(void *);
void SetProxyType(CBLProxySettings * proxy, CBLProxyType);
void Set_Null(void *);
*/
import "C"
import "unsafe"
import "context"
import "reflect"
//export databaseListenerBridge
func databaseListenerBridge(c unsafe.Pointer, db *C.CBLDatabase, numDocs C.unsigned, docIDs **C.char) {
props, _ := getKeyValuePropMap((C.FLDict)(c))
ids := make([]string, numDocs)
var i, count_docs uint
count_docs = uint(numDocs)
for i=0; i < count_docs; i++ {
// docPtr := uintptr(unsafe.Pointer(*docIDs)) + uintptr(i)
// docIdPtr := unsafe.Pointer(docPtr)
// ids[i] = C.GoString((*C.char)(docIdPtr))
ids[i] = C.GoString(C.getDocIDFromArray(docIDs, C.unsigned(i)))
}
database := Database{}
database.db = db
// Place the props in a context to fwd to the callback
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
v := ctx.Value(uuid).(string)
fn, ok := dbCallbacks[v]
if ok {
fn(ctx, &database, ids)
}
}
//export documentListenerBridge
func documentListenerBridge(c unsafe.Pointer, db *C.CBLDatabase, c_docID *C.char) {
// c is now a C.FLMutableDict
props, _ := getKeyValuePropMap((C.FLDict)(c))
docId := C.GoString(c_docID)
database := Database{}
database.db = db
// v := (*ctx).Value(uuid).(string)
// (documentChangeListeners[v])(*ctx, &database, docId)
// callback := (*ctx).Value(callback).(DocumentChangeListener)
// Place the props in a context to fwd to the callback
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
// FunctionPointer struct that contains the unsafe.Pointer fn
//callback :=
//(*callback)(ctx, &database, docId)
v := ctx.Value(uuid).(string)
fn, ok := docCallbacks[v]
if ok {
fn(ctx, &database, docId)
}
}
//export notificationBridge
func notificationBridge(c unsafe.Pointer, db *C.CBLDatabase) {
props, _ := getKeyValuePropMap((C.FLDict)(c))
d := Database{}
d.db = db
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
if notificationCallback != nil {
notificationCallback(ctx, &d)
}
}
//export queryListenerBride
func queryListenerBride(c unsafe.Pointer, query *C.CBLQuery) {
props, _ := getKeyValuePropMap((C.FLDict)(c))
q := Query{query}
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
v := ctx.Value(uuid).(string)
fn, ok := queryCallbacks[v]
if ok {
fn(ctx, &q)
}
}
//export pushFilterBridge
func pushFilterBridge(c unsafe.Pointer, doc *C.CBLDocument, isDeleted C.bool) C.bool {
props, _ := getKeyValuePropMap((C.FLDict)(c))
d := Document{}
d.doc = doc
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
v := ctx.Value(pushCallback).(string)
fn, ok := pushFilterCallbacks[v]
if ok {
return C.bool(fn(ctx, &d, bool(isDeleted)))
}
return C.bool(false)
}
//export pullFilterBridge
func pullFilterBridge(c unsafe.Pointer, doc *C.CBLDocument, isDeleted C.bool) C.bool {
props, _ := getKeyValuePropMap((C.FLDict)(c))
d := Document{}
d.doc = doc
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
v := ctx.Value(pullCallback).(string)
fn, ok := pullFilterCallbacks[v]
if ok {
return C.bool(fn(ctx, &d, bool(isDeleted)))
}
return C.bool(false)
}
//export replicatorChangeBridge
func replicatorChangeBridge(c unsafe.Pointer, replicator *C.CBLReplicator, status *C.CBLReplicatorStatus) {
props, _ := getKeyValuePropMap((C.FLDict)(c))
rep := Replicator{replicator}
e := Error{uint32(status.error.internal_info), uint32(status.error.code), uint32(status.error.domain)}
activity := ReplicatorActivityLevel(status.activity)
progress := ReplicatorProgress{float32(status.progress.fractionComplete), uint64(status.progress.documentCount)}
repStatus := ReplicatorStatus{activity, progress, e}
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
v := ctx.Value(uuid).(string)
fn, ok := replicatorCallbacks[v]
if ok {
fn(ctx, &rep, &repStatus)
}
}
//export replicatedDocumentBridge
func replicatedDocumentBridge(c unsafe.Pointer, replicator *C.CBLReplicator, isPush C.bool,
numDocument C.unsigned, documents *C.CBLReplicatedDocument) {
props, _ := getKeyValuePropMap((C.FLDict)(c))
rep := Replicator{replicator}
e := Error{uint32(documents.error.internal_info), uint32(documents.error.code), uint32(documents.error.domain)}
id := C.GoString(documents.ID)
doc_flags := DocumentFlags(documents.flags)
rep_doc := ReplicatedDocument{id, doc_flags, e}
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
v := ctx.Value(uuid).(string)
fn, ok := replicatedDocCallbacks[v]
if ok {
fn(ctx, &rep, bool(isPush), uint(numDocument), &rep_doc)
}
}
//export conflictResolverBridge
func conflictResolverBridge(c unsafe.Pointer, documentID *C.char, localDocument *C.CBLDocument, remoteDocument *C.CBLDocument) *C.CBLDocument {
props, _ := getKeyValuePropMap((C.FLDict)(c))
docId := C.GoString(documentID)
localDoc := Document{}
localDoc.doc = localDocument
remoteDoc := Document{}
remoteDoc.doc = remoteDocument
ctx := context.Background()
for k, v := range props {
ctx = context.WithValue(ctx, k, v)
}
v := ctx.Value(conflictResolver).(string)
fn, ok := conflictResolverCallbacks[v]
if ok {
// We need to return the underlying CBLDocument pointer.
cblcgo_doc := fn(ctx, docId, &localDoc, &remoteDoc)
return cblcgo_doc.doc
}
return localDocument
}
func getFLValueToGoValue(fl_val C.FLValue) (interface{}, error) {
var val interface{}
switch C.FLValue_GetType(fl_val) {
///< Type of a NULL pointer, i.e. no such value, like JSON `undefined`. Also the type of a value created by FLEncoder_WriteUndefined().
case C.kFLUndefined:
///< Equivalent to a JSON 'null'
case C.kFLNull:
val = nil
return val, nil
case C.kFLBoolean:
val = bool(C.FLValue_AsBool(fl_val))
return val, nil
///< A numeric value, either integer or floating-point
case C.kFLNumber:
if C.FLValue_IsInteger(fl_val) {
val = int64(C.FLValue_AsInt(fl_val))
} else if C.FLValue_IsUnsigned(fl_val) {
val = uint64(C.FLValue_AsUnsigned(fl_val))
} else if C.FLValue_IsDouble(fl_val) {
val = float64(C.FLValue_AsDouble(fl_val))
} else {
val = float32(C.FLValue_AsFloat(fl_val))
}
return val, nil
case C.kFLString:
fl_str := C.FLValue_AsString(fl_val)
val = C.GoStringN((*C.char)(fl_str.buf), C.int(fl_str.size))
return val, nil
case C.kFLData:
fl_data_slice := C.FLValue_AsData(fl_val)
val = C.GoBytes(fl_data_slice.buf, C.int(fl_data_slice.size))
//val = FunctionPointer{unsafe.Pointer(fl_data_slice.buf)}
return val, nil
case C.kFLArray:
// This could be a homogenous array or a hetero one.
// Return the bytes and let the developer decide.
// Hetero arrays can be processed by converting []byte to []interface{}
fl_array, err := C.FLValue_AsArray(fl_val)
if err == nil {
is_empty := bool(C.FLArray_IsEmpty(fl_array))
if !is_empty {
val = C.GoBytes(unsafe.Pointer(fl_array), C.int(C.FLArray_Count(fl_array)))
} else {
val = nil
}
}
return val, nil
case C.kFLDict:
// Determine if dictionary is a Blob
if isBlob(C.FLValue_AsDict(fl_val)) {
if blob, err := getBlob(C.FLValue_AsDict(fl_val)); err == nil {
return blob, nil
}
return nil, ErrProblemGettingBlobWithData
}
// Deep iterate over the value which is a map
iter := C.FLDeepIterator_New(fl_val)
var value C.FLValue
props := make(map[string]interface{})
for value = C.FLDeepIterator_GetValue(iter); value != nil; value = C.FLDeepIterator_GetValue(iter) {
// FLString
key := C.FLDeepIterator_GetKey(iter)
str_key := C.GoStringN((*C.char)(key.buf), C.int(key.size))
i, e := getFLValueToGoValue(value)
if e == nil {
props[str_key] = i
}
C.FLDeepIterator_Next(iter)
}
C.FLDeepIterator_Free(iter)
return props, nil
default:
return nil, ErrInvalidCBLType
}
return nil, ErrInvalidCBLType
}
func storeGoValueInSlot(fl_slot C.FLSlot, v interface{}) error {
switch val := reflect.TypeOf(v); val.Kind() {
case reflect.String:
value := v.(string)
s := C.CString(value)
// Create a key in the dict. Returns an FLSlot, and set the slot with the value
C.FLSlot_SetString(fl_slot, C.FLStr(s))
C.free(unsafe.Pointer(s))
break
case reflect.Array:
return ErrUnsupportedGoType
case reflect.Slice:
//case []byte:
// We have to iterate through the array.
mutable_array := C.FLMutableArray_New()
v_arr := v.([]interface{})
for i:=0; i < len(v_arr); i++ {
v_slot := C.FLMutableArray_Append(mutable_array)
storeGoValueInSlot(v_slot, v_arr[i]);
}
fl_arr := C.FLMutableArray_GetSource(mutable_array)
C.FLSlot_SetValue(fl_slot, C.FLArray_AsValue(fl_arr))
// C.FLArray_Release(fl_arr)
// C.FLMutableArray_Release(mutable_array)
break
case reflect.Int:
value := v.(int)
C.FLSlot_SetInt(fl_slot, C.int64_t(value))
break
case reflect.Int8:
value := v.(int8)
C.FLSlot_SetInt(fl_slot, C.int64_t(value))
break
case reflect.Int16:
value := v.(int16)
C.FLSlot_SetInt(fl_slot, C.int64_t(value))
break
case reflect.Int32:
value := v.(int32)
C.FLSlot_SetInt(fl_slot, C.int64_t(value))
break
case reflect.Int64:
value := v.(int64)
C.FLSlot_SetInt(fl_slot, C.int64_t(value))
break
case reflect.Uint:
value := v.(uint)
C.FLSlot_SetUInt(fl_slot, C.uint64_t(value))
break
case reflect.Uintptr:
value := v.(uintptr)
C.FLSlot_SetUInt(fl_slot, C.uint64_t(value))
break
case reflect.Uint8:
value := v.(uint8)
C.FLSlot_SetUInt(fl_slot, C.uint64_t(value))
break
case reflect.Uint16:
value := v.(uint16)
C.FLSlot_SetUInt(fl_slot, C.uint64_t(value))
break
case reflect.Uint32:
value := v.(uint32)
C.FLSlot_SetUInt(fl_slot, C.uint64_t(value))
break
case reflect.Uint64:
value := v.(uint64)
C.FLSlot_SetUInt(fl_slot, C.uint64_t(value))
break
case reflect.Float32:
value := v.(float32)
C.FLSlot_SetFloat(fl_slot, C.float(value))
break
case reflect.Float64:
value := v.(float64)
C.FLSlot_SetDouble(fl_slot, C.double(value))
// double
break;
case reflect.Bool:
value := v.(bool)
C.FLSlot_SetBool(fl_slot, C.bool(value))
break
case reflect.Map:
switch v.(type) {
case map[string]interface{}:
v_map := v.(map[string]interface{})
mutable_dict := C.FLMutableDict_New()
for key, val := range v_map {
c_key := C.CString(key)
v_slot := C.FLMutableDict_Set(mutable_dict, C.FLStr(c_key))
storeGoValueInSlot(v_slot, val)
C.free(unsafe.Pointer(c_key))
}
fl_dict := C.FLMutableDict_GetSource(mutable_dict)
C.FLSlot_SetValue(fl_slot, C.FLDict_AsValue(fl_dict))
// C.FLDict_Release(fl_dict)
// C.FLMutableDict_Release(mutable_dict)
break
default:
return ErrUnsupportedGoType
}
default:
return ErrUnsupportedGoType
}
return nil
}
func storeContextInMutableDict(ctx context.Context, keys []string) C.FLMutableDict {
mutableDict := C.FLMutableDict_New()
for i:=0; i < len(keys); i++ {
c_key := C.CString(keys[i])
fl_slot := C.FLMutableDict_Set(mutableDict, C.FLStr(c_key))
storeGoValueInSlot(fl_slot, ctx.Value(keys[i]))
}
return mutableDict
}