fix(evaluators): fix date result in variable (#1781)
This commit is contained in:
parent
23ad507261
commit
4db3b283b0
@ -36,7 +36,7 @@ const ConstantTypes = {
|
|||||||
string: {
|
string: {
|
||||||
label: `{{t("String")}}`,
|
label: `{{t("String")}}`,
|
||||||
value: 'string',
|
value: 'string',
|
||||||
component({ onChange, value }) {
|
component: function StringComponent({ onChange, value }) {
|
||||||
return <AntInput value={value} onChange={(ev) => onChange(ev.target.value)} />;
|
return <AntInput value={value} onChange={(ev) => onChange(ev.target.value)} />;
|
||||||
},
|
},
|
||||||
default: '',
|
default: '',
|
||||||
@ -44,7 +44,7 @@ const ConstantTypes = {
|
|||||||
number: {
|
number: {
|
||||||
label: '{{t("Number")}}',
|
label: '{{t("Number")}}',
|
||||||
value: 'number',
|
value: 'number',
|
||||||
component({ onChange, value }) {
|
component: function NumberComponent({ onChange, value }) {
|
||||||
return <InputNumber value={value} onChange={onChange} />;
|
return <InputNumber value={value} onChange={onChange} />;
|
||||||
},
|
},
|
||||||
default: 0,
|
default: 0,
|
||||||
@ -52,7 +52,7 @@ const ConstantTypes = {
|
|||||||
boolean: {
|
boolean: {
|
||||||
label: `{{t("Boolean")}}`,
|
label: `{{t("Boolean")}}`,
|
||||||
value: 'boolean',
|
value: 'boolean',
|
||||||
component: function Com({ onChange, value }) {
|
component: function BooleanComponent({ onChange, value }) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
return (
|
return (
|
||||||
<Select
|
<Select
|
||||||
@ -71,7 +71,7 @@ const ConstantTypes = {
|
|||||||
date: {
|
date: {
|
||||||
label: '{{t("Date")}}',
|
label: '{{t("Date")}}',
|
||||||
value: 'date',
|
value: 'date',
|
||||||
component({ onChange, value }) {
|
component: function DateComponent({ onChange, value }) {
|
||||||
return (
|
return (
|
||||||
<DatePicker
|
<DatePicker
|
||||||
value={moment(value)}
|
value={moment(value)}
|
||||||
@ -89,7 +89,7 @@ const ConstantTypes = {
|
|||||||
null: {
|
null: {
|
||||||
label: `{{t("Null")}}`,
|
label: `{{t("Null")}}`,
|
||||||
value: 'null',
|
value: 'null',
|
||||||
component: function Com() {
|
component: function NullComponent() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
return <AntInput readOnly placeholder={t('Null')} className="null-value" />;
|
return <AntInput readOnly placeholder={t('Null')} className="null-value" />;
|
||||||
},
|
},
|
||||||
|
@ -27,6 +27,15 @@ describe('evaluate', () => {
|
|||||||
expect(result).toBe(1);
|
expect(result).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('function result Date', () => {
|
||||||
|
const result = formulaEval('{{a}}', {
|
||||||
|
a() {
|
||||||
|
return new Date();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(result).toMatch(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z/);
|
||||||
|
});
|
||||||
|
|
||||||
it('number path to array item 0 (math.js)', () => {
|
it('number path to array item 0 (math.js)', () => {
|
||||||
expect(() => mathEval('{{a.0}}', { a: [1, 2, 3] })).toThrow();
|
expect(() => mathEval('{{a.0}}', { a: [1, 2, 3] })).toThrow();
|
||||||
});
|
});
|
||||||
|
@ -55,6 +55,10 @@ export function evaluate(this: Evaluator, expression: string, scope: Scope = {})
|
|||||||
result = replaceNumberIndex(v, context);
|
result = replaceNumberIndex(v, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result instanceof Date) {
|
||||||
|
result = `'${result.toISOString()}'`;
|
||||||
|
}
|
||||||
|
|
||||||
return ` ${result} `;
|
return ` ${result} `;
|
||||||
});
|
});
|
||||||
return this(exp, context);
|
return this(exp, context);
|
||||||
|
@ -16,7 +16,7 @@ import JobModel from './models/Job';
|
|||||||
import WorkflowModel from './models/Workflow';
|
import WorkflowModel from './models/Workflow';
|
||||||
import Processor from './Processor';
|
import Processor from './Processor';
|
||||||
import initTriggers, { Trigger } from './triggers';
|
import initTriggers, { Trigger } from './triggers';
|
||||||
import initFunctions from './functions';
|
import initFunctions, { CustomFunction } from './functions';
|
||||||
import { createLogger, Logger, LoggerOptions, getLoggerLevel, getLoggerFilePath } from '@nocobase/logger';
|
import { createLogger, Logger, LoggerOptions, getLoggerLevel, getLoggerFilePath } from '@nocobase/logger';
|
||||||
|
|
||||||
type Pending = [ExecutionModel, JobModel?];
|
type Pending = [ExecutionModel, JobModel?];
|
||||||
@ -25,7 +25,7 @@ type ID = number | string;
|
|||||||
export default class WorkflowPlugin extends Plugin {
|
export default class WorkflowPlugin extends Plugin {
|
||||||
instructions: Registry<Instruction> = new Registry();
|
instructions: Registry<Instruction> = new Registry();
|
||||||
triggers: Registry<Trigger> = new Registry();
|
triggers: Registry<Trigger> = new Registry();
|
||||||
functions: Registry<Function> = new Registry();
|
functions: Registry<CustomFunction> = new Registry();
|
||||||
private executing: ExecutionModel | null = null;
|
private executing: ExecutionModel | null = null;
|
||||||
private pending: Pending[] = [];
|
private pending: Pending[] = [];
|
||||||
private events: [WorkflowModel, any, { context?: any }][] = [];
|
private events: [WorkflowModel, any, { context?: any }][] = [];
|
||||||
@ -193,7 +193,7 @@ export default class WorkflowPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public trigger(workflow: WorkflowModel, context: Object, options: { context?: any } = {}): void {
|
public trigger(workflow: WorkflowModel, context: { [key: string]: any }, options: { context?: any } = {}): void {
|
||||||
// `null` means not to trigger
|
// `null` means not to trigger
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -130,6 +130,24 @@ describe('workflow > instructions > calculation', () => {
|
|||||||
const [job] = await execution.getJobs();
|
const [job] = await execution.getJobs();
|
||||||
expect(job.result).toBe(2);
|
expect(job.result).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('$system.now()', async () => {
|
||||||
|
const n1 = await workflow.createNode({
|
||||||
|
type: 'calculation',
|
||||||
|
config: {
|
||||||
|
engine: 'math.js',
|
||||||
|
expression: '{{$system.now}}',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const post = await PostRepo.create({ values: { title: 't1' } });
|
||||||
|
|
||||||
|
await sleep(500);
|
||||||
|
|
||||||
|
const [execution] = await workflow.getExecutions();
|
||||||
|
const [job] = await execution.getJobs();
|
||||||
|
expect(job.result).toMatch(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z/);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('formula.js', () => {
|
describe('formula.js', () => {
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
import Plugin from '..';
|
import Plugin from '..';
|
||||||
|
import ExecutionModel from '../models/Execution';
|
||||||
|
import FlowNodeModel from '../models/FlowNode';
|
||||||
|
|
||||||
|
export type CustomFunction = (this: { execution: ExecutionModel; node?: FlowNodeModel }) => any;
|
||||||
|
|
||||||
function now() {
|
function now() {
|
||||||
return new Date();
|
return new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ({ functions }: Plugin, more: { [key: string]: Function } = {}) {
|
export default function ({ functions }: Plugin, more: { [key: string]: CustomFunction } = {}) {
|
||||||
functions.register('now', now);
|
functions.register('now', now);
|
||||||
|
|
||||||
for (const [name, fn] of Object.entries(more)) {
|
for (const [name, fn] of Object.entries(more)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user