1
0
Fork 0
mirror of synced 2024-10-04 03:54:37 +13: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 7462b55a9a
commit 8afbf12021
8 changed files with 28 additions and 29 deletions

View file

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

View file

@ -1,13 +1,25 @@
import { import {
compileExpression as cExp,
compileCode as cCode, compileCode as cCode,
} from "@nx-js/compiler-util" } from "@nx-js/compiler-util"
import { includes } from "lodash/fp"
export const compileCode = code => { export const compileCode = code => {
let func 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 { try {
func = cCode(code) func = cCode(safeCode)
} catch (e) { } catch (e) {
e.message = `Error compiling code : ${code} : ${e.message}` e.message = `Error compiling code : ${code} : ${e.message}`
throw e throw e
@ -15,16 +27,3 @@ export const compileCode = code => {
return func 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 { 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 { safeKey, apiWrapper, events, isNonEmptyString } from "../common"
import { iterateIndex } from "../indexing/read" import { iterateIndex } from "../indexing/read"
import { import {
@ -147,7 +147,7 @@ const applyItemToAggregateResult = (indexNode, result, item) => {
const thisGroupResult = result[aggGroup.name] const thisGroupResult = result[aggGroup.name]
if (isNonEmptyString(aggGroup.condition)) { if (isNonEmptyString(aggGroup.condition)) {
if (!compileExpression(aggGroup.condition)({ record: item })) { if (!compileCode(aggGroup.condition)({ record: item })) {
continue continue
} }
} }

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
import { flatten, map, isEmpty } from "lodash/fp" 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 { isNonEmptyString, executesWithoutException, $ } from "../common"
import { applyRuleSet, makerule } from "../common/validationCommon" import { applyRuleSet, makerule } from "../common/validationCommon"