From 0b49e4c89cfddd6c121e3c91e709e1da8f17833d Mon Sep 17 00:00:00 2001 From: lvxiaojiao Date: Thu, 13 Apr 2023 20:03:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0formulaEvalErrorHandler&?= =?UTF-8?q?conditionComputeErrorHandler=E7=94=A8=E4=BA=8E=E8=B7=B3?= =?UTF-8?q?=E8=BF=87amis=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-core/__tests__/compat.test.ts | 8 ++++---- packages/amis-core/__tests__/condition.test.ts | 4 ++-- .../amis-core/src/utils/resolveCondition.ts | 9 +++++---- .../utils/resolveVariableAndFilterForAsync.ts | 17 +++++++++++------ 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/amis-core/__tests__/compat.test.ts b/packages/amis-core/__tests__/compat.test.ts index 5f869f9ea..739a088cf 100644 --- a/packages/amis-core/__tests__/compat.test.ts +++ b/packages/amis-core/__tests__/compat.test.ts @@ -4,7 +4,7 @@ import { resolveVariableAndFilter, resolveVariableAndFilterForAsync } from '../src/utils/tpl-builtin'; -import {setFormulaEvalHandler} from '../src/utils'; +import {setFormulaEvalErrorHandler} from '../src/utils'; const filters = [ { @@ -643,14 +643,14 @@ test(`compat:test2`, () => { expect(resolveVariable('obj.x', data)).toEqual(123); }); -test('compat:formulaEvalHandler', async () => { - setFormulaEvalHandler( +test('compat:formulaEvalErrorHandler', async () => { + setFormulaEvalErrorHandler( ( path?: string, data: object = {}, defaultFilter: string = '| html', fallbackValue = (value: any) => value, - skipFormulaEvalHandler: boolean = false + skipFormulaEvalErrorHandler: boolean = false ) => { return Promise.resolve(1 * 2 * 2); } diff --git a/packages/amis-core/__tests__/condition.test.ts b/packages/amis-core/__tests__/condition.test.ts index ddb68b264..2848c59f7 100644 --- a/packages/amis-core/__tests__/condition.test.ts +++ b/packages/amis-core/__tests__/condition.test.ts @@ -3,7 +3,7 @@ import { resolveCondition, guid, registerConditionComputer, - setConditionComputeHandler + setConditionComputeErrorHandler } from '../src/utils/'; const data = { @@ -512,7 +512,7 @@ test(`condition register`, async () => { test(`condition conditionComputeHander`, async () => { // 无法解析时,自定义解析逻辑 - setConditionComputeHandler( + setConditionComputeErrorHandler( (conditions: any, data: any, defaultResult: boolean) => { return Promise.resolve(true); } diff --git a/packages/amis-core/src/utils/resolveCondition.ts b/packages/amis-core/src/utils/resolveCondition.ts index e53d19f1f..bb4794daa 100644 --- a/packages/amis-core/src/utils/resolveCondition.ts +++ b/packages/amis-core/src/utils/resolveCondition.ts @@ -11,7 +11,8 @@ const conditionResolverMap: { [op: string]: (left: any, right: any, fieldType?: string) => boolean; } = {}; const DEFAULT_RESULT = true; -let conditionComputeHandler: ( + +let conditionComputeErrorHandler: ( conditions: any, data: any, defaultResult: boolean @@ -40,7 +41,7 @@ export async function resolveCondition( } catch (e) { // 如果函数未定义,则交给handler if (e.name === 'FormulaEvalError') { - return await conditionComputeHandler?.( + return await conditionComputeErrorHandler?.( conditions.children, conditions.conjunction, data @@ -313,14 +314,14 @@ export function getConditionComputers() { return conditionResolverMap; } -export function setConditionComputeHandler( +export function setConditionComputeErrorHandler( fn: ( conditions: any, data: any, defaultResult: boolean ) => boolean | Promise ) { - conditionComputeHandler = fn; + conditionComputeErrorHandler = fn; } registerConditionComputer('greater', greaterFunc); diff --git a/packages/amis-core/src/utils/resolveVariableAndFilterForAsync.ts b/packages/amis-core/src/utils/resolveVariableAndFilterForAsync.ts index a5539d4b4..a1e7ff137 100644 --- a/packages/amis-core/src/utils/resolveVariableAndFilterForAsync.ts +++ b/packages/amis-core/src/utils/resolveVariableAndFilterForAsync.ts @@ -1,6 +1,6 @@ import {AsyncEvaluator, parse} from 'amis-formula'; -let formulaEvalHandler: ( +let formulaEvalErrorHandler: ( path: string, data?: object, ...args: any[] @@ -10,10 +10,10 @@ let formulaEvalHandler: ( * 设置自定义函数,functions中找不到处理的函数时执行 * @param fn */ -export function setFormulaEvalHandler( +export function setFormulaEvalErrorHandler( fn: (path: string, data?: object, ...args: any[]) => any ): void { - formulaEvalHandler = fn; + formulaEvalErrorHandler = fn; } export const resolveVariableAndFilterForAsync = async ( @@ -21,7 +21,7 @@ export const resolveVariableAndFilterForAsync = async ( data: object = {}, defaultFilter: string = '| html', fallbackValue = (value: any) => value, - skipFormulaEvalHandler: boolean = false + skipFormulaEvalErrorHandler: boolean = false ) => { if (!path || typeof path !== 'string') { return undefined; @@ -43,9 +43,14 @@ export const resolveVariableAndFilterForAsync = async ( } catch (e) { console.warn(e); if (e.name === 'FormulaEvalError') { - if (!skipFormulaEvalHandler) { + if (!skipFormulaEvalErrorHandler) { // 无法解析时,执行handler自定义解析逻辑 - return formulaEvalHandler?.(path, data, defaultFilter, fallbackValue); + return formulaEvalErrorHandler?.( + path, + data, + defaultFilter, + fallbackValue + ); } // 跳过自定义解析逻辑,则直接抛异常 throw e;