mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat: Add assignment component with drag and drop to Set node (#8283)
Co-authored-by: Giulio Andreini <andreini@netseven.it>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { isObject } from 'lodash-es';
|
||||
import type { AssignmentValue, IDataObject } from 'n8n-workflow';
|
||||
import { resolveParameter } from '@/mixins/workflowHelpers';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
export function nameFromExpression(expression: string): string {
|
||||
return expression.replace(/^{{\s*|\s*}}$/g, '').replace('$json.', '');
|
||||
}
|
||||
|
||||
export function inferAssignmentType(value: unknown): string {
|
||||
if (typeof value === 'boolean') return 'boolean';
|
||||
if (typeof value === 'number') return 'number';
|
||||
if (typeof value === 'string') return 'string';
|
||||
if (Array.isArray(value)) return 'array';
|
||||
if (isObject(value)) return 'object';
|
||||
return 'string';
|
||||
}
|
||||
|
||||
export function typeFromExpression(expression: string): string {
|
||||
try {
|
||||
const resolved = resolveParameter(`=${expression}`);
|
||||
return inferAssignmentType(resolved);
|
||||
} catch (error) {
|
||||
return 'string';
|
||||
}
|
||||
}
|
||||
|
||||
export function inputDataToAssignments(input: IDataObject): AssignmentValue[] {
|
||||
const assignments: AssignmentValue[] = [];
|
||||
|
||||
function processValue(value: IDataObject, path: Array<string | number> = []) {
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((element, index) => {
|
||||
processValue(element, [...path, index]);
|
||||
});
|
||||
} else if (isObject(value)) {
|
||||
for (const [key, objectValue] of Object.entries(value)) {
|
||||
processValue(objectValue as IDataObject, [...path, key]);
|
||||
}
|
||||
} else {
|
||||
const stringPath = path.reduce((fullPath: string, part) => {
|
||||
if (typeof part === 'number') {
|
||||
return `${fullPath}[${part}]`;
|
||||
}
|
||||
return `${fullPath}.${part}`;
|
||||
}, '$json');
|
||||
|
||||
const expression = `={{ ${stringPath} }}`;
|
||||
assignments.push({
|
||||
id: uuid(),
|
||||
name: stringPath.replace('$json.', ''),
|
||||
value: expression,
|
||||
type: inferAssignmentType(value),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
processValue(input);
|
||||
|
||||
return assignments;
|
||||
}
|
||||
Reference in New Issue
Block a user