ts-flex-query
Version:
Flexible and type-safe data queries
84 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FunctionSerializer = void 0;
const main_1 = require("../../functions/main");
const definitions_1 = require("../helpers/definitions");
const serializers = {
aggregation: {
count: (collection) => (collection ? `${collection}/` : '') + definitions_1.oDataDataSetAggregationFunctions.aggregation.count,
maximum: null,
minimum: null,
average: null,
countDistinct: null,
sum: null
},
boolean: {
and: (v1, v2) => `${v1} and ${v2}`,
or: (v1, v2) => `${v1} or ${v2}`,
not: (v) => `not ${v}`,
xor: (v1, v2) => `(${v1} and not ${v2}) or (not ${v1} and ${v2})`
},
collections: {
in: (v1, v2) => `${v1} in ${v2}`,
distinct: null,
first: null
},
comparison: {
equal: (v1, v2) => `${v1} eq ${v2}`,
notEqual: (v1, v2) => `${v1} ne ${v2}`,
greater: (v1, v2) => `${v1} gt ${v2}`,
greaterOrEqual: (v1, v2) => `${v1} ge ${v2}`,
has: (v1, v2) => `${v1} has ${v2}`,
lower: (v1, v2) => `${v1} lt ${v2}`,
lowerOrEqual: (v1, v2) => `${v1} le ${v2}`
},
internal: {
ifUndefined: null,
mergeObjects: null
},
mathematics: {
add: (v1, v2) => `${v1} add ${v2}`,
subtract: (v1, v2) => `${v1} sub ${v2}`,
multiply: (v1, v2) => `${v1} mul ${v2}`,
divide: (v1, v2) => `${v1} divby ${v2}`,
divideInteger: (v1, v2) => `${v1} div ${v2}`,
modulo: (v1, v2) => `${v1} mod ${v2}`
},
text: {
concat: (v1, v2) => `concat(${v1}, ${v2})`,
startsWith: (v1, v2) => `startswith(${v1}, ${v2})`,
endsWith: (v1, v2) => `endswith(${v1}, ${v2})`,
lowerCase: (v) => `tolower(${v})`,
upperCase: (v) => `toupper(${v})`,
contains: (v1, v2) => `contains(${v1}, ${v2})`,
indexOf: (v1, v2) => `indexof(${v1}, ${v2})`,
getLength: (v) => `length(${v})`,
asString: null
}
};
class FunctionSerializer {
constructor(baseSerializer, initialVariables) {
this.baseSerializer = baseSerializer;
this.initialVariables = initialVariables;
}
serialize(expr) {
const containerName = (0, main_1.getFunctionContainerName)(expr.container);
if (!containerName) {
throw new Error(`Function container ${expr.container.constructor.name} is not supported. Member: ${expr.member}`);
}
const serializer = serializers[containerName][expr.member];
if (!serializer) {
throw new Error(`Function ${expr.container.constructor.name}.${expr.member} is not supported.`);
}
return serializer(...expr.args.map((e) => this.serializeExpectNotNull(e)));
}
serializeExpectNotNull(expression, variables) {
const result = this.baseSerializer(expression, variables !== null && variables !== void 0 ? variables : this.initialVariables);
if (!result) {
throw new Error(`Expected serialized value to be not null for expression ${expression.constructor.name}.`);
}
return result;
}
}
exports.FunctionSerializer = FunctionSerializer;
//# sourceMappingURL=function-serializer.js.map