UNPKG

@ultipa-graph/ultipa-node-sdk

Version:

NodeJS SDK for ultipa-server 4.0

114 lines 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PropertyUtils = void 0; const types_1 = require("../types"); var PropertyUtils; (function (PropertyUtils) { PropertyUtils.IsBasePropertyType = (...types) => { for (let index = 0; index < types.length; index++) { const t = types[index]; let isBase = [ types_1.ULTIPA.PropertyType.PROPERTY_INT32, types_1.ULTIPA.PropertyType.PROPERTY_STRING, types_1.ULTIPA.PropertyType.PROPERTY_FLOAT, types_1.ULTIPA.PropertyType.PROPERTY_DOUBLE, types_1.ULTIPA.PropertyType.PROPERTY_UINT32, types_1.ULTIPA.PropertyType.PROPERTY_INT64, types_1.ULTIPA.PropertyType.PROPERTY_UINT64, types_1.ULTIPA.PropertyType.PROPERTY_DATETIME, types_1.ULTIPA.PropertyType.PROPERTY_TIMESTAMP, types_1.ULTIPA.PropertyType.PROPERTY_TEXT, ].includes(t); if (!isBase) { return false; } } return true; }; PropertyUtils.propertyGet = (type) => { return types_1.ULTIPA.PropertyType[type]; }; PropertyUtils.PropertyTypeDescValid = (type, subTypes) => { let result = { ok: true, msg: "", }; let lengthCheck = 0; if (type === types_1.ULTIPA.PropertyType.PROPERTY_LIST || type === types_1.ULTIPA.PropertyType.PROPERTY_SET) { lengthCheck = 1; } if (type === types_1.ULTIPA.PropertyType.PROPERTY_MAP) { lengthCheck = 2; } if (((subTypes === null || subTypes === void 0 ? void 0 : subTypes.length) || 0) != lengthCheck) { result.ok = false; result.msg = `The length of the subtype of type ${PropertyUtils.propertyGet(type)} needs to be equal to ${lengthCheck}`; return result; } if ((subTypes === null || subTypes === void 0 ? void 0 : subTypes.length) > 0) { if (!PropertyUtils.IsBasePropertyType(...subTypes)) { result.ok = false; result.msg = `Type ${PropertyUtils.propertyGet(type)} does not support subtypes: ${JSON.stringify(subTypes.map(t => PropertyUtils.propertyGet(t)))}`; return result; } } return result; }; PropertyUtils.GetPropertyTypeDesc = (type, subTypes, extra) => { let validResult = PropertyUtils.PropertyTypeDescValid(type, subTypes); if (!validResult.ok) { throw new Error(validResult.msg); } if (type === types_1.ULTIPA.PropertyType.PROPERTY_LIST) { let subType = subTypes[0]; if (PropertyUtils.IsBasePropertyType(subType)) { return `${PropertyUtils.propertyGet(subType)}[]`; } } if (type === types_1.ULTIPA.PropertyType.PROPERTY_SET) { let subType = subTypes[0]; if (PropertyUtils.IsBasePropertyType(subType)) { return `set(${PropertyUtils.propertyGet(subType)})`; } } if (type === types_1.ULTIPA.PropertyType.PROPERTY_MAP) { let [subTypeKey, subTypeValue] = subTypes; if (PropertyUtils.IsBasePropertyType(subTypeKey, subTypeValue)) { return `{${PropertyUtils.propertyGet(subTypeKey)}: ${PropertyUtils.propertyGet(subTypeValue)}}`; } } if (type == types_1.ULTIPA.PropertyType.PROPERTY_DECIMAL) { return `decimal(${extra === null || extra === void 0 ? void 0 : extra.precision}, ${extra === null || extra === void 0 ? void 0 : extra.scale})`; } return PropertyUtils.propertyGet(type); }; PropertyUtils.GetPropertyType = (typeDesc) => { let result = { type: types_1.ULTIPA.PropertyType.PROPERTY_UNSET, }; if (typeDesc.endsWith("]")) { let subTypeDesc = typeDesc.replace(/\[|\]/g, "").trim(); result.type = types_1.ULTIPA.PropertyType.PROPERTY_LIST; result.subTypesDesc = [subTypeDesc]; result.subTypes = [types_1.ULTIPA.PropertyType[subTypeDesc]]; return result; } if (typeDesc.endsWith(")") && typeDesc.startsWith("set(")) { let subTypeDesc = typeDesc.slice(4, typeDesc.length - 1); result.type = types_1.ULTIPA.PropertyType.PROPERTY_SET; result.subTypesDesc = [subTypeDesc]; result.subTypes = [types_1.ULTIPA.PropertyType[subTypeDesc]]; return result; } if (typeDesc.trim().endsWith("}")) { let subTypesDesc = typeDesc.replace(/\{|\}/g, "").split(":").map(v => v.trim()); result.type = types_1.ULTIPA.PropertyType.PROPERTY_MAP; result.subTypesDesc = subTypesDesc; result.subTypes = subTypesDesc.map(desc => types_1.ULTIPA.PropertyType[desc]); return result; } result.type = types_1.ULTIPA.PropertyType[typeDesc]; return result; }; })(PropertyUtils = exports.PropertyUtils || (exports.PropertyUtils = {})); //# sourceMappingURL=property.js.map