@allgemein/expressions
Version:
Library for mango expressions
199 lines • 6.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExprDesc = void 0;
const KeyDesc_1 = require("./KeyDesc");
const base_1 = require("@allgemein/base");
const ValueDesc_1 = require("./ValueDesc");
const ExpressionValidationError_1 = require("../exceptions/ExpressionValidationError");
const lodash_1 = require("lodash");
class ExprDesc {
constructor() {
this.type = 'cond';
}
get key() {
return this.__key;
}
set key(value) {
this.__key = value;
}
isOp() {
return false;
}
isGroup() {
return false;
}
getSourceKeys() {
let keys = [];
if (this.isOp()) {
keys.push(this.key);
/*
let childKeys = this.value.getSourceKeys();
if(!isEmpty(childKeys)){
keys = concat(keys, childKeys);
}*/
}
else if (this.isGroup()) {
keys = (0, lodash_1.concat)(keys, ...(0, lodash_1.map)(this.values, v => v.getSourceKeys()));
}
return (0, lodash_1.uniq)(keys);
}
getTargetKeys() {
let keys = [];
if (this.isOp()) {
if (this.value instanceof KeyDesc_1.KeyDesc) {
keys.push(this.value.key);
}
}
else if (this.isGroup()) {
keys = (0, lodash_1.concat)(keys, ...(0, lodash_1.map)(this.values, v => v.getTargetKeys()));
}
return (0, lodash_1.uniq)(keys);
}
applyOn(target, source, force = false) {
if (this.type == 'eq') {
if (!(0, lodash_1.has)(target, this.key) || false) {
if (this.value instanceof ValueDesc_1.ValueDesc) {
// @ts-ignore
target[this.key] = this.value.value;
}
else if (this.value instanceof KeyDesc_1.KeyDesc) {
// @ts-ignore
target[this.key] = source[this.value.key];
}
else {
throw new base_1.NotYetImplementedError();
}
}
}
else if (this.type == 'and') {
(0, lodash_1.map)(this.values, v => v.applyOn(target, source, force));
}
else if (this.type == 'or') {
(0, lodash_1.map)(this.values, v => v.applyOn(target, source, force));
}
else {
throw new base_1.NotYetImplementedError();
}
}
applyReverseOn(target, source, force = false) {
if (this.type == 'eq') {
if (!(0, lodash_1.has)(target, this.key) || false) {
if (this.value instanceof KeyDesc_1.KeyDesc) {
// @ts-ignore
target[this.value.key] = source[this.key];
}
else {
throw new base_1.NotYetImplementedError();
}
}
}
else if (this.type == 'and') {
(0, lodash_1.map)(this.values, v => v.applyReverseOn(target, source, force));
}
else if (this.type == 'or') {
(0, lodash_1.map)(this.values, v => v.applyReverseOn(target, source, force));
}
else {
throw new base_1.NotYetImplementedError();
}
}
/**
* Returns key-value map of all key and referenced key or values
* (sometimes needed for join definitions).
*/
getMap() {
let map = {};
if (this.type == 'eq') {
if (this.value instanceof KeyDesc_1.KeyDesc) {
map[this.key] = this.value.key;
}
else if (this.value instanceof ValueDesc_1.ValueDesc) {
map[this.key] = '\'' + this.value.value + '\'';
}
else {
throw new base_1.NotYetImplementedError();
}
}
else if (this.type == 'and') {
(0, lodash_1.merge)(map, ...this.values.map((v) => v.getMap()));
}
else if (this.type == 'or') {
(0, lodash_1.merge)(map, ...this.values.map((v) => v.getMap()));
}
else {
throw new base_1.NotYetImplementedError();
}
return map;
}
lookup(source) {
throw new base_1.NotYetImplementedError();
}
for(target, keyMap = {}) {
throw new base_1.NotYetImplementedError();
}
/**
* Test if conditions matching class properties and references
*/
test(sourceRef, errors = []) {
let sourceKeys = this.getSourceKeys();
for (let sourceKey of sourceKeys) {
let keyChain = sourceKey.split('.');
let root = sourceRef;
while (keyChain.length > 0) {
let _k = keyChain.shift();
let p = root.getPropertyRef(_k);
if (p) {
if (p.isReference()) {
root = p.getTargetRef();
}
}
else {
errors.push('key ' + _k + ' is no property of ' + root.name);
}
}
}
return errors.length == 0;
}
/**
* Validate if defined keys match source and target element
* - target = referred
* - source = referrer
*/
validate(registry, targetRef, sourceRef, throwing = true) {
let sourceKeys = this.getSourceKeys();
let targetKeys = this.getTargetKeys();
let targetProps = targetRef.getPropertyRefs().map(p => p.name).filter(pn => sourceKeys.indexOf(pn) !== -1);
let sourceProps = sourceRef ? sourceRef.getPropertyRefs().map(p => p.name).filter(pn => targetKeys.indexOf(pn) !== -1) : [];
if (sourceKeys.length != targetProps.length) {
if (throwing) {
throw new ExpressionValidationError_1.ExpressionValidationError('referred key(s) ' + sourceKeys.filter(k => targetProps.indexOf(k) === -1).join(',') + ' not in sourceRef');
}
return false;
}
if (targetKeys.length != sourceProps.length) {
if (throwing) {
throw new ExpressionValidationError_1.ExpressionValidationError('referrer key(s) ' + targetKeys.filter(k => sourceProps.indexOf(k) === -1).join(',') + ' not in targetRef');
}
return false;
}
return true;
}
toJson() {
if (this.isGroup()) {
let res = {};
res['$' + this.type] = (0, lodash_1.map)(this.values, x => x.toJson());
return res;
}
else if (this.isOp()) {
let res = {};
res[this.key] = {};
res[this.key]['$' + this.type] = this.value.toJson ? this.value.toJson() : this.value;
return res;
}
else {
throw new base_1.NotSupportedError('toJson to this condition not supported');
}
}
}
exports.ExprDesc = ExprDesc;
//# sourceMappingURL=ExprDesc.js.map