forked from godror/godror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
738 lines (669 loc) · 23.1 KB
/
json.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
package godror
/*
#include <stdlib.h>
#include "dpiImpl.h"
// All CGO functions are used because handling unions, indexing,
// nested callocs for substructures is hard to handle in Go code
void godror_allocate_dpiNode(dpiJsonNode **dpijsonnode) {
*dpijsonnode = (dpiJsonNode *)(calloc(1, sizeof(dpiJsonNode)));
dpiDataBuffer *dpijsonDataBuffer = (dpiDataBuffer *)(calloc(1, sizeof(dpiDataBuffer)));
(*dpijsonnode)->value = dpijsonDataBuffer;
}
void godror_free_dpiNode(dpiJsonNode *node) {
if(node == NULL) {
return;
}
if (node->value) {
free(node->value);
node->value = NULL;
}
free(node);
node = NULL;
}
void godror_setObjectFields(dpiJsonObject * jsonobj, int i, dpiJsonNode **jnode)
{
*jnode = &(jsonobj->fields[i]);
jsonobj->fields[i].value = &jsonobj->fieldValues[i];
}
void godror_dpiJsonObject_setKey(dpiJsonNode *dpijsonnode, int index, const char *key, uint32_t keyLength) {
dpiJsonObject *dpijsonobj = &(dpijsonnode->value->asJsonObject);
dpijsonobj->fieldNames[index] = calloc((keyLength + 1), sizeof(char));
memcpy(dpijsonobj->fieldNames[index], key, keyLength);
dpijsonobj->fieldNames[index][keyLength] = '\0';
dpijsonobj->fieldNameLengths[index] = keyLength;
}
void godror_dpiasJsonObject(dpiJsonNode *dpijsonnode, dpiJsonObject **dpijsonobj)
{
*dpijsonobj = &(dpijsonnode->value->asJsonObject);
}
void godror_dpiasJsonArray(dpiJsonNode *dpijsonnode, dpiJsonArray **dpijsonobj)
{
*dpijsonobj = &(dpijsonnode->value->asJsonArray);
}
void godror_setArrayElements(dpiJsonArray * jsonarr, int i, dpiJsonNode **jnode)
{
*jnode = &(jsonarr->elements[i]);
jsonarr->elements[i].value = &jsonarr->elementValues[i];
}
void godror_dpiJson_setDouble(dpiJsonNode *topNode, double value) {
topNode->oracleTypeNum = DPI_ORACLE_TYPE_NUMBER;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_DOUBLE;
topNode->value->asDouble = value;
}
void godror_dpiJson_setInt64(dpiJsonNode *topNode, int64_t value) {
topNode->oracleTypeNum = DPI_ORACLE_TYPE_NUMBER;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_INT64;
topNode->value->asInt64 = value;
}
void godror_dpiJson_setUint64(dpiJsonNode *topNode, uint64_t value) {
topNode->oracleTypeNum = DPI_ORACLE_TYPE_NUMBER;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_UINT64;
topNode->value->asUint64 = value;
}
void godror_dpiJson_setTime(dpiJsonNode *topNode, dpiData *data) {
topNode->oracleTypeNum = DPI_ORACLE_TYPE_TIMESTAMP;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_TIMESTAMP;
topNode->value->asTimestamp = data->value.asTimestamp;
}
void godror_dpiJson_setBytes(dpiJsonNode *topNode, dpiData *data) {
uint32_t size = data->value.asBytes.length;
topNode->oracleTypeNum = DPI_ORACLE_TYPE_RAW;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_BYTES;
topNode->value->asBytes.ptr = calloc(1, size);
memcpy(topNode->value->asBytes.ptr, data->value.asBytes.ptr, size);
topNode->value->asBytes.length = size;
}
void godror_dpiJson_setNumber(dpiJsonNode *topNode, const _GoString_ value) {
uint32_t length;
length = _GoStringLen(value);
topNode->oracleTypeNum = DPI_ORACLE_TYPE_NUMBER;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_BYTES;
topNode->value->asBytes.ptr = calloc(1, length);
memcpy(topNode->value->asBytes.ptr, _GoStringPtr(value), length);
topNode->value->asBytes.length = length;
}
void godror_dpiJson_setIntervalDS(dpiJsonNode *topNode, dpiData *data) {
topNode->oracleTypeNum = DPI_ORACLE_TYPE_INTERVAL_DS;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_INTERVAL_DS;
topNode->value->asIntervalDS = data->value.asIntervalDS;
}
void godror_dpiJson_setBool(dpiJsonNode *topNode, dpiData *data) {
topNode->oracleTypeNum = DPI_ORACLE_TYPE_BOOLEAN;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_BOOLEAN;
topNode->value->asBoolean = data->value.asBoolean;
}
void godror_dpiJson_setString(dpiJsonNode *topNode, dpiData *data) {
uint32_t size = data->value.asBytes.length;
topNode->oracleTypeNum = DPI_ORACLE_TYPE_VARCHAR;
topNode->nativeTypeNum = DPI_NATIVE_TYPE_BYTES;
// make a copy before passing to C?
topNode->value->asBytes.ptr = calloc(1, size);
memcpy(topNode->value->asBytes.ptr, data->value.asBytes.ptr, size);
topNode->value->asBytes.length = size;
}
void godror_dpiJsonObject_initialize(dpiJsonNode **dpijsonnode, uint32_t numfields) {
dpiJsonObject dpijsonobjtmp;
dpiJsonObject *dpijsonobj = &dpijsonobjtmp;
(*dpijsonnode)->oracleTypeNum = DPI_ORACLE_TYPE_JSON_OBJECT;
(*dpijsonnode)->nativeTypeNum = DPI_NATIVE_TYPE_JSON_OBJECT;
dpijsonobj->fieldNames = (calloc(numfields, sizeof(char *)));
dpijsonobj->fields = (dpiJsonNode *)(calloc(numfields, sizeof(dpiJsonNode)));
dpijsonobj->fieldNameLengths = calloc(numfields, sizeof(uint32_t));
dpijsonobj->fieldValues = (dpiDataBuffer *)calloc(numfields, sizeof(dpiDataBuffer));
dpijsonobj->numFields = numfields;
(*dpijsonnode)->value->asJsonObject = *dpijsonobj;
}
void godror_dpiJsonArray_initialize(dpiJsonNode **dpijsonnode, uint32_t numelem) {
dpiJsonArray dpijsonarrtmp;
dpiJsonArray *dpijsonarr = &dpijsonarrtmp;
(*dpijsonnode)->oracleTypeNum = DPI_ORACLE_TYPE_JSON_ARRAY;
(*dpijsonnode)->nativeTypeNum = DPI_NATIVE_TYPE_JSON_ARRAY;
dpijsonarr->elements = calloc(numelem, sizeof(dpiJsonNode));
dpijsonarr->elementValues = (dpiDataBuffer *)calloc(numelem, sizeof(dpiDataBuffer));
dpijsonarr->numElements = numelem;
(*dpijsonnode)->value->asJsonArray = *dpijsonarr;
}
void godror_dpiJsonNodeFree(dpiJsonNode *node)
{
dpiJsonArray *array;
dpiJsonObject *obj;
uint32_t i;
if (node == NULL)
{
return;
}
switch (node->nativeTypeNum) {
case DPI_NATIVE_TYPE_BYTES:
if(node->value->asBytes.ptr) {
free(node->value->asBytes.ptr);
node->value->asBytes.ptr = NULL;
}
break;
case DPI_NATIVE_TYPE_JSON_ARRAY:
array = &node->value->asJsonArray;
if (array->elements) {
for (i = 0; i < array->numElements; i++) {
if (array->elements[i].value) {
godror_dpiJsonNodeFree(&array->elements[i]);
}
}
free(array->elements);
array->elements = NULL;
}
if (array->elementValues) {
free(array->elementValues);
array->elementValues = NULL;
}
break;
case DPI_NATIVE_TYPE_JSON_OBJECT:
obj = &node->value->asJsonObject;
if (obj->fields) {
for (i = 0; i < obj->numFields; i++) {
if (obj->fields[i].value)
godror_dpiJsonNodeFree(&obj->fields[i]);
}
free(obj->fields);
obj->fields = NULL;
}
if (obj->fieldNames) {
for (i = 0; i < obj->numFields; i++) {
if (obj->fieldNames[i]) {
free(obj->fieldNames[i]);
obj->fieldNames[i] = NULL;
}
}
free(obj->fieldNames);
obj->fieldNames = NULL;
}
if (obj->fieldNameLengths) {
free(obj->fieldNameLengths);
obj->fieldNameLengths = NULL;
}
if (obj->fieldValues) {
free(obj->fieldValues);
obj->fieldValues = NULL;
}
break;
}
}
void godror_dpiJsonfreeMem(dpiJsonNode *node) {
if (node == NULL)
{
return;
}
godror_dpiJsonNodeFree(node);
godror_free_dpiNode(node);
}
*/
import "C"
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"time"
"unsafe"
)
// JSONOption provides an option to retrieve scalar values
// from JSON tree.
//
// DPI_JSON_OPT_NUMBER_AS_STRING - returns value stored as NUMBER in DB
// as godror.Number.
// DPI_JSON_OPT_DEAFULT - returns value stored as NUMBER in DB as float64.
type JSONOption uint8
var ErrInvalidJSON = errors.New("invalid JSON Document")
var ErrInvalidType = errors.New("invalid JSON Scalar Type")
const (
JSONOptDefault = JSONOption(C.DPI_JSON_OPT_DEFAULT)
JSONOptNumberAsString = JSONOption(C.DPI_JSON_OPT_NUMBER_AS_STRING)
)
// JSON holds the JSON data to/from Oracle.
// It is like a root node in JSON tree.
type JSON struct {
dpiJson *C.dpiJson
}
// Get retrieves the data stored in JSON based on option, opts.
func (j JSON) Get(data *Data, opts JSONOption) error {
var node *C.dpiJsonNode
if C.dpiJson_getValue(j.dpiJson, C.uint32_t(opts), (**C.dpiJsonNode)(unsafe.Pointer(&node))) == C.DPI_FAILURE {
return ErrInvalidJSON
}
jsonNodeToData(data, node)
return nil
}
// GetJSONObject retrieves JSONObject from JSON based on option, opts.
// It returns error if JSON doesnt represent object type.
func (j JSON) GetJSONObject(opts JSONOption) (JSONObject, error) {
var node *C.dpiJsonNode
var d Data
if C.dpiJson_getValue(j.dpiJson, C.uint32_t(opts), (**C.dpiJsonNode)(unsafe.Pointer(&node))) == C.DPI_FAILURE {
return JSONObject{}, ErrInvalidJSON
}
jsonNodeToData(&d, node)
if C.dpiOracleTypeNum(node.oracleTypeNum) != C.DPI_ORACLE_TYPE_JSON_OBJECT {
return JSONObject{}, ErrInvalidType
}
return JSONObject{dpiJsonObject: C.dpiData_getJsonObject(&(d.dpiData))}, nil
}
// GetJSONArray retrieves JSONArray from JSON based on option, opts.
// It returns error if JSON doesnt represent array type.
func (j JSON) GetJSONArray(opts JSONOption) (JSONArray, error) {
var node *C.dpiJsonNode
if C.dpiJson_getValue(j.dpiJson, C.uint32_t(opts), (**C.dpiJsonNode)(unsafe.Pointer(&node))) == C.DPI_FAILURE {
return JSONArray{}, ErrInvalidJSON
}
var d Data
jsonNodeToData(&d, node)
if C.dpiOracleTypeNum(node.oracleTypeNum) != C.DPI_ORACLE_TYPE_JSON_ARRAY {
return JSONArray{}, ErrInvalidType
}
return JSONArray{dpiJsonArray: C.dpiData_getJsonArray(&(d.dpiData))}, nil
}
// GetJSONScalar retrieves JSONScalar from JSON based on option, opts.
func (j JSON) GetJSONScalar(opts JSONOption) (JSONScalar, error) {
var node *C.dpiJsonNode
if C.dpiJson_getValue(j.dpiJson, C.uint32_t(opts), (**C.dpiJsonNode)(unsafe.Pointer(&node))) == C.DPI_FAILURE {
return JSONScalar{}, ErrInvalidJSON
}
return JSONScalar{dpiJsonNode: node}, nil
}
// GetValue converts the native DB type stored in JSON into an interface value.
// The scalar values stored in JSON get converted as below.
//
// map[string]interface{}, for JSON object type
// []interface{}, for JSON arrays
// godror.Number or float64 based on options for NUMBER
// bool , for boolean
// byte[], for RAW
// time.Duration, for INTERVAL DAY TO SECOND
// time.Time, for TIMESTAMP
// string, for VARCHAR2(string)
func (j JSON) GetValue(opts JSONOption) (interface{}, error) {
jScalar, err := j.GetJSONScalar(opts)
if err != nil {
// later
} else if val, err2 := jScalar.GetValue(); err2 != nil {
err = err2
} else {
return val, nil
}
if logger := getLogger(context.TODO()); logger != nil {
logger.Error("JSON.GetValue", "error", err)
}
return nil, err
}
// String returns standard JSON formatted string
func (j JSON) String() string {
// json library is used, it will be removed
// with direct call to get JSON string from JSON.
// Returning empty string for error case, fix?
jScalar, err := j.GetJSONScalar(JSONOptNumberAsString)
if err != nil {
// later
} else if jScalarVal, err2 := jScalar.GetValue(); err2 != nil {
err = err2
} else if data, err2 := json.Marshal(jScalarVal); err2 != nil {
err = err2
} else {
return string(data)
}
if logger := getLogger(context.TODO()); logger != nil {
logger.Error("JSON.String", "error", err)
}
return ""
}
// jsonNodeToData gets the data from dpiJsonNode
func jsonNodeToData(data *Data, node *C.dpiJsonNode) {
if node.value == nil {
data.dpiData.isNull = 1
return
}
data.dpiData.value = *node.value
data.NativeTypeNum = node.nativeTypeNum
}
// JSONStringFlags represents the input JSON string format.
// It can be standard JSON format or it can include ORACLE extended types,
// BSON extended types.
type JSONStringFlags uint
// JSONString encapsulates JSON formatted string.
type JSONString struct {
Flags JSONStringFlags // standard , extended types for OSON, BSON
Value string // JSON input
}
// JSONValue indicates the input bind value provided for DB column type JSON.
// Valid inputs: int, int8, int16, int32, int64, uint, uint8, uint16,
// uint32, uint64, float32, float64, string, map, array, string and bool.
//
// for int, int8, int16, int32, int64, uint, uint8, uint16, uint32,
// uint64, float32, float64; DB native type NUMBER is used.
// for string; DB native type VARCHAR2 is used.
// for time.Time; DB native type TIMESTAMP is used.
// for time.Duration; DB native type INTERVAL DAY TO SECOND is used.
// for []byte; DB native type RAW is used.
// for bool; DB native type boolean is used.
// for map[string]interface{}; DB type JSON Object is used.
// for []interface{}; DB native type JSON Array is used.
type JSONValue struct {
Value interface{}
}
// JSONScalar holds the JSON data to/from Oracle.
// It includes all scalar values such as int, int8, int16, int32, int64,
// uint, uint8, uint16, uint32, uint64, float32, float64, string,
// map, array, string, byte[], time.Time, time.Duration, godror.Number and bool.
type JSONScalar struct {
dpiJsonNode *C.dpiJsonNode
}
// GetValue converts native DB type stored in JSONScalar to an interface value.
// The scalar value stored in JSONScalar gets converted as below.
//
// map[string]interface{}, for JSON object type
// []interface{}, for JSON arrays
// godror.Number or float64 based on options for NUMBER
// bool , for JSON boolean
// byte[], for JSON RAW
// time.Duration, for INTERVAL DAY TO SECOND
// time.Time, for TIMESTAMP
// string, for VARCHAR2(string)
func (j JSONScalar) GetValue() (val interface{}, err error) {
var d Data
jsonNodeToData(&d, j.dpiJsonNode)
if j.dpiJsonNode.oracleTypeNum == C.DPI_ORACLE_TYPE_NUMBER {
val = getJSONScalarNumber(d)
} else if j.dpiJsonNode.oracleTypeNum == C.DPI_ORACLE_TYPE_VARCHAR {
val, err = getJSONScalarString(d)
} else {
val = d.Get()
if j.dpiJsonNode.oracleTypeNum == C.DPI_ORACLE_TYPE_JSON_OBJECT {
jobj := val.(JSONObject)
val, err = jobj.GetValue()
} else if j.dpiJsonNode.oracleTypeNum == C.DPI_ORACLE_TYPE_JSON_ARRAY {
jarr := val.(JSONArray)
val, err = jarr.GetValue()
} else {
err = nil
}
}
return
}
// getJSONScalarNumber returns DB NUMBER as godror.Number for option,
// JSONOptNumberAsString and float64 for option, JSONOptDefault.
func getJSONScalarNumber(d Data) (val interface{}) {
b := d.Get()
if d.NativeTypeNum == C.DPI_NATIVE_TYPE_BYTES {
val = Number(b.([]byte))
} else {
val = b.(float64)
}
return
}
// getJSONScalarString converts the byte array of VARCHAR2 to string
func getJSONScalarString(d Data) (string, error) {
b := d.Get()
return string(b.([]byte)), nil
}
// JSONArray represents the array input.
type JSONArray struct {
dpiJsonArray *C.dpiJsonArray
}
// Len returns the number of elements in the JSONArray.
func (j JSONArray) Len() int { return int(j.dpiJsonArray.numElements) }
// GetElement returns the ith element in JSONArray as data.
func (j JSONArray) GetElement(i int) Data {
elts := jsonArraySlice(j.dpiJsonArray)
var d Data
jsonNodeToData(&d, &elts[i])
return d
}
// Get returns the data array from JSONArray
func (j JSONArray) Get(nodes []Data) []Data {
elts := jsonArraySlice(j.dpiJsonArray)
for i := range elts {
var d Data
jsonNodeToData(&d, &elts[i])
nodes = append(nodes, d)
}
return nodes
}
// GetValue converts native DB type, array into []interface{}.
func (j JSONArray) GetValue() (nodes []interface{}, err error) {
elts := jsonArraySlice(j.dpiJsonArray)
for i := range elts {
var d Data
jsonNodeToData(&d, &elts[i])
if d.NativeTypeNum == C.DPI_NATIVE_TYPE_JSON_OBJECT {
jsobj := JSONObject{dpiJsonObject: C.dpiData_getJsonObject(&(d.dpiData))}
m, err := jsobj.GetValue()
if err != nil {
return nil, err
}
nodes = append(nodes, m)
} else if d.NativeTypeNum == C.DPI_NATIVE_TYPE_JSON_ARRAY {
jsarr := JSONArray{dpiJsonArray: C.dpiData_getJsonArray(&(d.dpiData))}
ua, err := jsarr.GetValue()
if err != nil {
return nil, err
}
nodes = append(nodes, ua)
} else {
if elts[i].oracleTypeNum == C.DPI_ORACLE_TYPE_VARCHAR {
keyval, err := getJSONScalarString(d)
if err == nil {
nodes = append(nodes, keyval)
} else {
return nil, err
}
} else if elts[i].oracleTypeNum == C.DPI_ORACLE_TYPE_NUMBER {
nodes = append(nodes, getJSONScalarNumber(d))
} else {
nodes = append(nodes, d.Get())
}
}
}
return nodes, nil
}
func jsonArraySlice(arr *C.dpiJsonArray) []C.dpiJsonNode {
n := int(arr.numElements)
//return ((*[maxArraySize]C.dpiJsonNode)(unsafe.Pointer(arr.elements)))[:n:n]
return unsafe.Slice(arr.elements, n)
}
// JSONObject represents the map input.
type JSONObject struct {
dpiJsonObject *C.dpiJsonObject
}
// Len returns the number of keys in the JSONObject
func (j JSONObject) Len() int { return int(j.dpiJsonObject.numFields) }
// Get returns the map, map[string]Data from JSONObject
func (j JSONObject) Get() map[string]Data {
ff := jsonObjectFields(j.dpiJsonObject)
m := make(map[string]Data, len(ff))
for _, f := range ff {
var d Data
jsonNodeToData(&d, f.Value)
m[f.Name] = d
}
return m
}
// GetValue converts native DB type, array into map[string]interface{}.
func (j JSONObject) GetValue() (m map[string]interface{}, err error) {
m = make(map[string]interface{})
for _, f := range jsonObjectFields(j.dpiJsonObject) {
var d Data
jsonNodeToData(&d, f.Value)
if d.NativeTypeNum == C.DPI_NATIVE_TYPE_JSON_OBJECT {
jsobj := JSONObject{dpiJsonObject: C.dpiData_getJsonObject(&(d.dpiData))}
um, err := jsobj.GetValue()
if err != nil {
return nil, err
}
m[f.Name] = um
} else if d.NativeTypeNum == C.DPI_NATIVE_TYPE_JSON_ARRAY {
jsobj := JSONArray{dpiJsonArray: C.dpiData_getJsonArray(&(d.dpiData))}
ua, err := jsobj.GetValue()
if err != nil {
return nil, err
}
m[f.Name] = ua
} else if f.Value.oracleTypeNum == C.DPI_ORACLE_TYPE_VARCHAR {
keyval, err := getJSONScalarString(d)
if err == nil {
m[f.Name] = keyval
} else {
return nil, err
}
} else if f.Value.oracleTypeNum == C.DPI_ORACLE_TYPE_NUMBER {
m[f.Name] = getJSONScalarNumber(d)
} else {
m[f.Name] = d.Get()
}
}
return m, nil
}
// GetInto takes pointer to struct and populates the fields.
// The struct name fields are matched with DB JSON keynames but
// not the struct json tags.
func (j JSONObject) GetInto(v interface{}) {
rv := reflect.ValueOf(v).Elem()
for _, f := range jsonObjectFields(j.dpiJsonObject) {
var d Data
jsonNodeToData(&d, f.Value)
// nosemgrep: go.lang.security.audit.unsafe-reflect-by-name.unsafe-reflect-by-name
rv.FieldByName(f.Name).Set(reflect.ValueOf(d.Get()))
}
}
type jsonField struct {
Name string
Value *C.dpiJsonNode
}
func jsonObjectFields(obj *C.dpiJsonObject) []jsonField {
n := int(obj.numFields)
//names := ((*[maxArraySize]*C.char)(unsafe.Pointer(obj.fieldNames)))[:n:n]
names := unsafe.Slice(obj.fieldNames, n)
//nameLengths := ((*[maxArraySize]C.uint32_t)(unsafe.Pointer(obj.fieldNameLengths)))[:n:n]
nameLengths := unsafe.Slice(obj.fieldNameLengths, n)
//fields := ((*[maxArraySize]C.dpiJsonNode)(unsafe.Pointer(obj.fields)))[:n:n]
fields := unsafe.Slice(obj.fields, n)
ff := make([]jsonField, n)
for i := range fields {
ff[i] = jsonField{
Name: C.GoStringN(names[i], C.int(nameLengths[i])),
Value: &fields[i],
}
C.free(unsafe.Pointer(names[i]))
}
return ff
}
// populateJSONNode populates dpiJsonNode from user inputs.
// It creates a seperate memory for the new output value, jsonnode.
// memory from user input, in is not shared with jsonnode.
// Caller has to explicitly free using godror_dpiJsonfreeMem
func populateJSONNode(jsonnode *C.dpiJsonNode, in interface{}) error {
switch x := in.(type) {
case []interface{}:
arr, _ := in.([]interface{})
C.godror_dpiJsonArray_initialize((**C.dpiJsonNode)(unsafe.Pointer(&jsonnode)), C.uint32_t(len(arr)))
var dpijsonarr *C.dpiJsonArray
C.godror_dpiasJsonArray(jsonnode, (**C.dpiJsonArray)(unsafe.Pointer(&dpijsonarr)))
for index, entry := range arr {
var jsonnodelocal *C.dpiJsonNode
C.godror_setArrayElements(dpijsonarr, C.int(index), (**C.dpiJsonNode)(unsafe.Pointer(&jsonnodelocal)))
err := populateJSONNode(jsonnodelocal, entry)
if err != nil {
return err
}
}
case map[string]interface{}:
m, _ := in.(map[string]interface{})
// Initialize dpiJsonObjectNode
C.godror_dpiJsonObject_initialize((**C.dpiJsonNode)(unsafe.Pointer(&jsonnode)), C.uint32_t(len(m)))
var dpijsonobj *C.dpiJsonObject
C.godror_dpiasJsonObject(jsonnode, (**C.dpiJsonObject)(unsafe.Pointer(&dpijsonobj)))
var i C.int = 0
var cKey *C.char
for k, v := range m {
cKey = C.CString(k)
C.godror_dpiJsonObject_setKey(jsonnode, i, cKey, C.uint32_t(len(k)))
C.free(unsafe.Pointer(cKey))
var jsonnodelocal *C.dpiJsonNode
C.godror_setObjectFields(dpijsonobj, i, (**C.dpiJsonNode)(unsafe.Pointer(&jsonnodelocal)))
err := populateJSONNode(jsonnodelocal, v)
if err != nil {
return err
}
i = i + 1
}
case int:
C.godror_dpiJson_setInt64(jsonnode, C.int64_t(x))
case int8:
C.godror_dpiJson_setInt64(jsonnode, C.int64_t(x))
case int16:
C.godror_dpiJson_setInt64(jsonnode, C.int64_t(x))
case int32:
C.godror_dpiJson_setInt64(jsonnode, C.int64_t(x))
case int64:
C.godror_dpiJson_setInt64(jsonnode, C.int64_t(x))
case uint:
C.godror_dpiJson_setUint64(jsonnode, C.uint64_t(x))
case uint8:
C.godror_dpiJson_setUint64(jsonnode, C.uint64_t(x))
case uint16:
C.godror_dpiJson_setUint64(jsonnode, C.uint64_t(x))
case uint32:
C.godror_dpiJson_setUint64(jsonnode, C.uint64_t(x))
case uint64:
C.godror_dpiJson_setUint64(jsonnode, C.uint64_t(x))
case float32:
C.godror_dpiJson_setDouble(jsonnode, C.double(x))
case float64:
C.godror_dpiJson_setDouble(jsonnode, C.double(x))
case Number:
C.godror_dpiJson_setNumber(jsonnode, x.String())
case string:
data, err := NewData(x)
if err != nil {
return err
}
C.godror_dpiJson_setString(jsonnode, &(data.dpiData))
case time.Time:
data, err := NewData(x)
if err != nil {
return err
}
C.godror_dpiJson_setTime(jsonnode, &(data.dpiData))
case time.Duration:
data, err := NewData(x)
if err != nil {
return err
}
C.godror_dpiJson_setIntervalDS(jsonnode, &(data.dpiData))
case []byte:
data, err := NewData(x)
if err != nil {
return err
}
C.godror_dpiJson_setBytes(jsonnode, &(data.dpiData))
case bool:
data, err := NewData(x)
if err != nil {
return err
}
C.godror_dpiJson_setBool(jsonnode, &(data.dpiData))
default:
return fmt.Errorf("unsupported type %T", in)
}
return nil
}
// freedpiJSONNode deallocates the dpiJsonNode
func freedpiJSONNode(node *C.dpiJsonNode) error {
C.godror_dpiJsonfreeMem(node)
return nil
}
// allocdpiJSONNode allocates dpiJsonNode from interface value, val
func allocdpiJSONNode(val interface{}, node **C.dpiJsonNode) error {
C.godror_allocate_dpiNode((**C.dpiJsonNode)(unsafe.Pointer(node)))
err := populateJSONNode(*node, val)
if err != nil {
C.godror_dpiJsonfreeMem(*node)
}
return err
}