forked from ukrbublik/react-awesome-query-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonLogic.js
More file actions
393 lines (349 loc) · 13.2 KB
/
jsonLogic.js
File metadata and controls
393 lines (349 loc) · 13.2 KB
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
import {defaultValue} from "../utils/stuff";
import {
getFieldConfig, getOperatorConfig, getFieldWidgetConfig, getFuncConfig
} from "../utils/configUtils";
import {getWidgetForFieldOp, formatFieldName} from "../utils/ruleUtils";
import {defaultConjunction} from "../utils/defaultUtils";
import {completeValue} from "../utils/funcUtils";
import {List, Map} from "immutable";
import omit from "lodash/omit";
import pick from "lodash/pick";
// http://jsonlogic.com/
export const jsonLogicFormat = (item, config) => {
//meta is mutable
let meta = {
usedFields: [],
errors: []
};
const logic = formatItem(item, config, meta, true);
// build empty data
const {errors, usedFields} = meta;
const {fieldSeparator} = config.settings;
let data = {};
for (let ff of usedFields) {
const def = getFieldConfig(config, ff) || {};
const parts = ff.split(fieldSeparator);
let tmp = data;
for (let i = 0 ; i < parts.length ; i++) {
const p = parts[i];
const pdef = getFieldConfig(config, parts.slice(0, i+1)) || {};
if (i != parts.length - 1) {
if (pdef.type == "!group" && pdef.mode != "struct") {
if (!tmp[p])
tmp[p] = [{}];
tmp = tmp[p][0];
} else {
if (!tmp[p])
tmp[p] = {};
tmp = tmp[p];
}
} else {
if (!tmp[p])
tmp[p] = null; // can use def.type for sample values
}
}
}
return {
errors,
logic,
data,
};
};
const formatItem = (item, config, meta, isRoot, parentField = null) => {
if (!item) return undefined;
const type = item.get("type");
const properties = item.get("properties") || new Map();
const isLocked = properties.get("isLocked");
const {lockedOp} = config.settings.jsonLogic;
let ret;
if (type === "group" || type === "rule_group") {
ret = formatGroup(item, config, meta, isRoot, parentField);
} else if (type === "rule") {
ret = formatRule(item, config, meta, parentField);
}
if (isLocked && ret && lockedOp) {
ret = { [lockedOp] : ret };
}
return ret;
};
const formatGroup = (item, config, meta, isRoot, parentField = null) => {
const type = item.get("type");
const properties = item.get("properties") || new Map();
const mode = properties.get("mode");
const children = item.get("children1") || new List();
const field = properties.get("field");
let conjunction = properties.get("conjunction");
if (!conjunction)
conjunction = defaultConjunction(config);
const conjunctionDefinition = config.conjunctions[conjunction];
const conj = conjunctionDefinition.jsonLogicConj || conjunction.toLowerCase();
const not = properties.get("not");
if (conj != "and" && conj != "or") {
meta.errors.push(`Conjunction ${conj} is not supported`);
return undefined;
}
const isRuleGroup = (type === "rule_group" && !isRoot);
const groupField = isRuleGroup && mode != "struct" ? field : parentField;
const groupOperator = properties.get("operator");
const groupOperatorDefinition = groupOperator && getOperatorConfig(config, groupOperator, field) || null;
const formattedValue = formatItemValue(config, properties, meta, groupOperator, parentField);
const isGroup0 = isRuleGroup && (!groupOperator || groupOperatorDefinition.cardinality == 0);
const list = children
.map((currentChild) => formatItem(currentChild, config, meta, false, groupField))
.filter((currentChild) => typeof currentChild !== "undefined");
if (isRuleGroup && mode != "struct" && !isGroup0) {
// "count" rule can have no "having" children, but should have number value
if (formattedValue == undefined)
return undefined;
} else {
if (!list.size)
return undefined;
}
let resultQuery = {};
if (list.size == 1 && !isRoot)
resultQuery = list.first();
else
resultQuery[conj] = list.toList().toJS();
// revert
if (not) {
resultQuery = { "!": resultQuery };
}
// rule_group (issue #246)
if (isRuleGroup && mode != "struct") {
const formattedField = formatField(meta, config, field, parentField);
if (isGroup0) {
// config.settings.groupOperators
const op = groupOperator || "some";
resultQuery = {
[op]: [
formattedField,
resultQuery
]
};
} else {
// there is rule for count
const filter = !list.size
? formattedField
: {
"filter": [
formattedField,
resultQuery
]
};
const count = {
"reduce": [
filter,
{ "+": [1, { var: "accumulator" }] },
0
]
};
resultQuery = formatLogic(config, properties, count, formattedValue, groupOperator);
}
}
return resultQuery;
};
const formatRule = (item, config, meta, parentField = null) => {
const properties = item.get("properties") || new Map();
const field = properties.get("field");
let operator = properties.get("operator");
let operatorOptions = properties.get("operatorOptions");
operatorOptions = operatorOptions ? operatorOptions.toJS() : null;
if (operatorOptions && !Object.keys(operatorOptions).length)
operatorOptions = null;
if (field == null || operator == null)
return undefined;
const fieldDefinition = getFieldConfig(config, field) || {};
let operatorDefinition = getOperatorConfig(config, operator, field) || {};
let reversedOp = operatorDefinition.reversedOp;
let revOperatorDefinition = getOperatorConfig(config, reversedOp, field) || {};
// check op
let isRev = false;
if (!operatorDefinition.jsonLogic && !revOperatorDefinition.jsonLogic) {
meta.errors.push(`Operator ${operator} is not supported`);
return undefined;
}
if (!operatorDefinition.jsonLogic && revOperatorDefinition.jsonLogic) {
isRev = true;
[operator, reversedOp] = [reversedOp, operator];
[operatorDefinition, revOperatorDefinition] = [revOperatorDefinition, operatorDefinition];
}
const formattedValue = formatItemValue(config, properties, meta, operator, parentField);
if (formattedValue === undefined)
return undefined;
const formattedField = formatField(meta, config, field, parentField);
return formatLogic(config, properties, formattedField, formattedValue, operator, operatorOptions, fieldDefinition, isRev);
};
const formatItemValue = (config, properties, meta, operator, parentField) => {
const field = properties.get("field");
const iValueSrc = properties.get("valueSrc");
const iValueType = properties.get("valueType");
const fieldDefinition = getFieldConfig(config, field) || {};
const operatorDefinition = getOperatorConfig(config, operator, field) || {};
const cardinality = defaultValue(operatorDefinition.cardinality, 1);
const iValue = properties.get("value");
const asyncListValues = properties.get("asyncListValues");
if (iValue == undefined)
return undefined;
let valueSrcs = [];
let valueTypes = [];
let oldUsedFields = meta.usedFields;
const fvalue = iValue.map((currentValue, ind) => {
const valueSrc = iValueSrc ? iValueSrc.get(ind) : null;
const valueType = iValueType ? iValueType.get(ind) : null;
const cValue = completeValue(currentValue, valueSrc, config);
const widget = getWidgetForFieldOp(config, field, operator, valueSrc);
const fieldWidgetDef = omit(getFieldWidgetConfig(config, field, operator, widget, valueSrc), ["factory"]);
const fv = formatValue(
meta, config, cValue, valueSrc, valueType, fieldWidgetDef, fieldDefinition, operator, operatorDefinition, parentField, asyncListValues
);
if (fv !== undefined) {
valueSrcs.push(valueSrc);
valueTypes.push(valueType);
}
return fv;
});
const hasUndefinedValues = fvalue.filter(v => v === undefined).size > 0;
if (fvalue.size < cardinality || hasUndefinedValues) {
meta.usedFields = oldUsedFields; // restore
return undefined;
}
return cardinality > 1 ? fvalue.toArray() : (cardinality == 1 ? fvalue.first() : null);
};
const formatValue = (meta, config, currentValue, valueSrc, valueType, fieldWidgetDef, fieldDef, operator, operatorDef, parentField = null, asyncListValues) => {
if (currentValue === undefined)
return undefined;
let ret;
if (valueSrc == "field") {
ret = formatField(meta, config, currentValue, parentField);
} else if (valueSrc == "func") {
ret = formatFunc(meta, config, currentValue, parentField);
} else if (typeof fieldWidgetDef.jsonLogic === "function") {
const fn = fieldWidgetDef.jsonLogic;
const args = [
currentValue,
{
...pick(fieldDef, ["fieldSettings", "listValues"]),
asyncListValues
},
//useful options: valueFormat for date/time
omit(fieldWidgetDef, ["formatValue", "mongoFormatValue", "sqlFormatValue", "jsonLogic", "elasticSearchFormatValue", "spelFormatValue"]),
];
if (operator) {
args.push(operator);
args.push(operatorDef);
}
ret = fn.call(config.ctx, ...args);
} else {
ret = currentValue;
}
return ret;
};
const formatFunc = (meta, config, currentValue, parentField = null) => {
const funcKey = currentValue.get("func");
const args = currentValue.get("args");
const funcConfig = getFuncConfig(config, funcKey);
if (!funcConfig.jsonLogic) {
meta.errors.push(`Func ${funcKey} is not supported`);
return undefined;
}
let formattedArgs = {};
for (const argKey in funcConfig.args) {
const argConfig = funcConfig.args[argKey];
const fieldDef = getFieldConfig(config, argConfig);
const argVal = args ? args.get(argKey) : undefined;
const argValue = argVal ? argVal.get("value") : undefined;
const argValueSrc = argVal ? argVal.get("valueSrc") : undefined;
const formattedArgVal = formatValue(
meta, config, argValue, argValueSrc, argConfig.type, fieldDef, argConfig, null, null, parentField
);
if (argValue != undefined && formattedArgVal === undefined) {
meta.errors.push(`Can't format value of arg ${argKey} for func ${funcKey}`);
return undefined;
}
if (formattedArgVal !== undefined) { // skip optional in the end
formattedArgs[argKey] = formattedArgVal;
}
}
const formattedArgsArr = Object.values(formattedArgs);
let ret;
if (typeof funcConfig.jsonLogic === "function") {
const fn = funcConfig.jsonLogic;
const args = [
formattedArgs,
];
ret = fn.call(config.ctx, ...args);
} else {
const funcName = funcConfig.jsonLogic || funcKey;
const isMethod = !!funcConfig.jsonLogicIsMethod;
if (isMethod) {
const [obj, ...params] = formattedArgsArr;
if (params.length) {
ret = { "method": [ obj, funcName, params ] };
} else {
ret = { "method": [ obj, funcName ] };
}
} else {
ret = { [funcName]: formattedArgsArr };
}
}
return ret;
};
const formatField = (meta, config, field, parentField = null) => {
const {fieldSeparator, jsonLogic} = config.settings;
let ret;
if (field) {
if (Array.isArray(field))
field = field.join(fieldSeparator);
const fieldDef = getFieldConfig(config, field) || {};
const fieldName = formatFieldName(field, config, meta, parentField);
let varName = fieldDef.jsonLogicVar || (fieldDef.type == "!group" ? jsonLogic.groupVarKey : "var");
ret = { [varName] : fieldName };
if (meta.usedFields.indexOf(field) == -1)
meta.usedFields.push(field);
}
return ret;
};
const buildFnToFormatOp = (operator, operatorDefinition, formattedField, formattedValue) => {
let formatteOp = operator;
const cardinality = defaultValue(operatorDefinition.cardinality, 1);
const isReverseArgs = defaultValue(operatorDefinition._jsonLogicIsRevArgs, false);
if (typeof operatorDefinition.jsonLogic == "string")
formatteOp = operatorDefinition.jsonLogic;
const rangeOps = ["<", "<=", ">", ">="];
const eqOps = ["==", "!="];
const fn = (field, op, val, opDef, opOpts) => {
if (cardinality == 0 && eqOps.includes(formatteOp))
return { [formatteOp]: [formattedField, null] };
else if (cardinality == 0)
return { [formatteOp]: formattedField };
else if (cardinality == 1 && isReverseArgs)
return { [formatteOp]: [formattedValue, formattedField] };
else if (cardinality == 1)
return { [formatteOp]: [formattedField, formattedValue] };
else if (cardinality == 2 && rangeOps.includes(formatteOp))
return { [formatteOp]: [formattedValue[0], formattedField, formattedValue[1]] };
else
return { [formatteOp]: [formattedField, ...formattedValue] };
};
return fn;
};
const formatLogic = (config, properties, formattedField, formattedValue, operator, operatorOptions = null, fieldDefinition = null, isRev = false) => {
const field = properties.get("field");
const operatorDefinition = getOperatorConfig(config, operator, field) || {};
let fn = typeof operatorDefinition.jsonLogic == "function"
? operatorDefinition.jsonLogic
: buildFnToFormatOp(operator, operatorDefinition, formattedField, formattedValue);
const args = [
formattedField,
operator,
formattedValue,
omit(operatorDefinition, ["formatOp", "mongoFormatOp", "sqlFormatOp", "jsonLogic", "spelFormatOp"]),
operatorOptions,
fieldDefinition,
];
let ruleQuery = fn.call(config.ctx, ...args);
if (isRev) {
ruleQuery = { "!": ruleQuery };
}
return ruleQuery;
};