Skip to content

Commit 89a53f9

Browse files
committed
Convert complex type to JS object
Convert the rust Complex type to an object of the type expected by the JS encoder. This commit is copied from #275 PR
1 parent 5dd912a commit 89a53f9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/types/cql-utils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,39 @@ function convertHints(hints) {
480480
return result;
481481
}
482482

483+
/**
484+
* Convert rust ComplexType into type representation used in the driver encoder
485+
* @param {rust.ComplexType} type
486+
*/
487+
function convertComplexType(type) {
488+
let data = {
489+
code: type.baseType.valueOf(),
490+
};
491+
let fistSupport = type.getFirstSupportType();
492+
let secondSupport = type.getSecondSupportType();
493+
let otherTypes = type.getInnerTypes();
494+
if (fistSupport != null) {
495+
data.info = convertComplexType(fistSupport);
496+
if (secondSupport != null) {
497+
data.info = [data.info, convertComplexType(secondSupport)];
498+
}
499+
} else if (otherTypes.length > 0) {
500+
if (data.code == rust.CqlType.UserDefinedType) {
501+
let names = type.getUdtFieldNames();
502+
data.info = {
503+
fields: otherTypes.map((typ, index) => {
504+
let obj = { type: convertComplexType(typ) };
505+
obj.name = names[index];
506+
return obj;
507+
}),
508+
};
509+
} else {
510+
data.info = otherTypes.map((t) => convertComplexType(t));
511+
}
512+
}
513+
return data;
514+
}
515+
483516
/**
484517
*
485518
* @param {null | object | Array<object>} object
@@ -495,6 +528,7 @@ function rustConvertHint(object) {
495528
module.exports.parseParams = parseParams;
496529
module.exports.convertHints = convertHints;
497530
module.exports.rustConvertHint = rustConvertHint;
531+
module.exports.convertComplexType = convertComplexType;
498532

499533
// For unit test usage
500534
module.exports.getWrapped = getWrapped;

src/types/type_wrappers.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ impl ComplexType {
177177
}),
178178
}
179179
}
180+
181+
#[napi]
182+
pub fn get_udt_names(&self) -> Option<Vec<String>> {
183+
self.udt_metadata.as_ref().map(|e| e.field_names.clone())
184+
}
180185
}
181186

182187
impl ComplexType {

0 commit comments

Comments
 (0)