Browse Source

allow all conde snippets to be either blocks or expressions

pull/168/head
Michael Shanks 6 years ago
parent
commit
823d8eea51
  1. 4
      packages/core/src/actionsApi/initialise.js
  2. 27
      packages/core/src/common/compileCode.js
  3. 4
      packages/core/src/indexApi/aggregates.js
  4. 6
      packages/core/src/indexing/evaluate.js
  5. 2
      packages/core/src/indexing/sharding.js
  6. 4
      packages/core/src/recordApi/validate.js
  7. 6
      packages/core/src/templateApi/validate.js
  8. 2
      packages/core/src/templateApi/validateAggregate.js

4
packages/core/src/actionsApi/initialise.js

@ -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 })
}

27
packages/core/src/common/compileCode.js

@ -1,28 +1,27 @@
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
try {
func = cCode(code)
} catch (e) {
e.message = `Error compiling code : ${code} : ${e.message}`
throw e
if (includes("return ")(code)) {
safeCode = code
} else {
let trimmed = code.trim()
trimmed = trimmed.endsWith(";")
? trimmed.substring(0, trimmed.length - 1)
: trimmed
safeCode = `return (${trimmed})`
}
return func
}
export const compileExpression = code => {
let func
try {
func = cExp(code)
func = cCode(safeCode)
} catch (e) {
e.message = `Error compiling expression : ${code} : ${e.message}`
e.message = `Error compiling code : ${code} : ${e.message}`
throw e
}

4
packages/core/src/indexApi/aggregates.js

@ -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
}
}

6
packages/core/src/indexing/evaluate.js

@ -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)

2
packages/core/src/indexing/sharding.js

@ -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,

4
packages/core/src/recordApi/validate.js

@ -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 }

6
packages/core/src/templateApi/validate.js

@ -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

2
packages/core/src/templateApi/validateAggregate.js

@ -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"

Loading…
Cancel
Save