1
0
Fork 0
mirror of synced 2024-07-04 05:50:57 +12:00

allow all conde snippets to be either blocks or expressions

This commit is contained in:
Michael Shanks 2020-03-24 16:12:46 +00:00
parent 914afc6059
commit 823d8eea51
8 changed files with 28 additions and 29 deletions

View file

@ -9,7 +9,7 @@ import {
reduce,
find,
} from "lodash/fp"
import { compileExpression, compileCode } from "../common/compileCode"
import { compileCode } from "../common/compileCode"
import { $ } from "../common"
import { _executeAction } from "./execute"
import { BadRequestError, NotFoundError } from "../common/errors"
@ -49,7 +49,7 @@ const subscribeTriggers = (
const shouldRunTrigger = (trigger, eventContext) => {
if (!trigger.condition) return true
const shouldRun = compileExpression(trigger.condition)
const shouldRun = compileCode(trigger.condition)
return shouldRun({ context: eventContext })
}

View file

@ -1,13 +1,25 @@
import {
compileExpression as cExp,
compileCode as cCode,
} from "@nx-js/compiler-util"
import { includes } from "lodash/fp"
export const compileCode = code => {
let func
let safeCode
if (includes("return ")(code)) {
safeCode = code
} else {
let trimmed = code.trim()
trimmed = trimmed.endsWith(";")
? trimmed.substring(0, trimmed.length - 1)
: trimmed
safeCode = `return (${trimmed})`
}
try {
func = cCode(code)
func = cCode(safeCode)
} catch (e) {
e.message = `Error compiling code : ${code} : ${e.message}`
throw e
@ -15,16 +27,3 @@ export const compileCode = code => {
return func
}
export const compileExpression = code => {
let func
try {
func = cExp(code)
} catch (e) {
e.message = `Error compiling expression : ${code} : ${e.message}`
throw e
}
return func
}

View file

@ -1,5 +1,5 @@
import { has, isNumber, isUndefined } from "lodash/fp"
import { compileExpression, compileCode } from "@nx-js/compiler-util"
import { compileCode } from "../common/compileCode"
import { safeKey, apiWrapper, events, isNonEmptyString } from "../common"
import { iterateIndex } from "../indexing/read"
import {
@ -147,7 +147,7 @@ const applyItemToAggregateResult = (indexNode, result, item) => {
const thisGroupResult = result[aggGroup.name]
if (isNonEmptyString(aggGroup.condition)) {
if (!compileExpression(aggGroup.condition)({ record: item })) {
if (!compileCode(aggGroup.condition)({ record: item })) {
continue
}
}

View file

@ -1,5 +1,5 @@
import { compileExpression, compileCode } from "@nx-js/compiler-util"
import { isUndefined, keys, cloneDeep, isFunction } from "lodash/fp"
import { compileCode } from "../common/compileCode"
import { isUndefined, keys, cloneDeep, isFunction, includes } from "lodash/fp"
import { defineError } from "../common"
export const filterEval = "FILTER_EVALUATE"
@ -16,7 +16,7 @@ const getEvaluateResult = () => ({
result: null,
})
export const compileFilter = index => compileExpression(index.filter)
export const compileFilter = index => compileCode(index.filter)
export const compileMap = index => compileCode(index.map)

View file

@ -1,4 +1,4 @@
import { compileCode } from "@nx-js/compiler-util"
import { compileCode } from "../common/compileCode"
import { filter, includes, map, last } from "lodash/fp"
import {
getActualKeyOfParent,

View file

@ -1,5 +1,5 @@
import { map, reduce, filter, isEmpty, flatten, each } from "lodash/fp"
import { compileExpression } from "@nx-js/compiler-util"
import { compileCode } from "../common/compileCode"
import _ from "lodash"
import { getExactNodeForKey } from "../templateApi/hierarchy"
import { validateFieldParse, validateTypeConstraints } from "../types"
@ -35,7 +35,7 @@ const validateAllTypeConstraints = async (record, recordNode, context) => {
const runRecordValidationRules = (record, recordNode) => {
const runValidationRule = rule => {
const isValid = compileExpression(rule.expressionWhenValid)
const isValid = compileCode(rule.expressionWhenValid)
const expressionContext = { record, _ }
return isValid(expressionContext)
? { valid: true }

View file

@ -11,7 +11,7 @@ import {
isEmpty,
has,
} from "lodash/fp"
import { compileExpression, compileCode } from "@nx-js/compiler-util"
import { compileCode } from "../common/compileCode"
import {
$,
isSomething,
@ -73,7 +73,7 @@ const aggregateGroupRules = [
"condition does not compile",
a =>
isEmpty(a.condition) ||
executesWithoutException(() => compileExpression(a.condition))
executesWithoutException(() => compileCode(a.condition))
),
]
@ -196,7 +196,7 @@ const triggerRules = actions => [
t => {
if (!t.condition) return true
try {
compileExpression(t.condition)
compileCode(t.condition)
return true
} catch (_) {
return false

View file

@ -1,5 +1,5 @@
import { flatten, map, isEmpty } from "lodash/fp"
import { compileCode } from "@nx-js/compiler-util"
import { compileCode } from "../common/compileCode"
import { isNonEmptyString, executesWithoutException, $ } from "../common"
import { applyRuleSet, makerule } from "../common/validationCommon"