Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): Add mapping support for data paths #5191

Merged
merged 20 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/editor-ui/src/components/ParameterInputFull.vue
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,31 @@ export default mixins(showMessage).extend({
}
},
onDrop(data: string) {
this.forceShowExpression = true;
const useDataPath = !!this.parameter.requiresDataPath && data.startsWith('{{ $json');
if (!useDataPath) {
this.forceShowExpression = true;
}
setTimeout(() => {
if (this.node) {
const prevValue = this.isResourceLocator ? this.value.value : this.value;
let updatedValue: string;
if (typeof prevValue === 'string' && prevValue.startsWith('=') && prevValue.length > 1) {
if (useDataPath) {
const newValue = data
.replace('{{ $json', '')
.replace(new RegExp('^\\.'), '')
.replace(new RegExp('}}$'), '')
.trim();

if (prevValue && this.parameter.requiresDataPath === 'multiple') {
updatedValue = `${prevValue}, ${newValue}`;
} else {
updatedValue = newValue;
}
} else if (
typeof prevValue === 'string' &&
prevValue.startsWith('=') &&
prevValue.length > 1
) {
updatedValue = `${prevValue} ${data}`;
} else {
updatedValue = `=${data}`;
Expand Down
13 changes: 8 additions & 5 deletions packages/editor-ui/src/components/RunDataJson.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import { externalHooks } from '@/mixins/externalHooks';
import { mapStores } from 'pinia';
import { useNDVStore } from '@/stores/ndv';
import MappingPill from './MappingPill.vue';
import { getMappedExpression } from '@/utils/mappingUtils';

const runDataJsonActions = () => import('@/components/RunDataJsonActions.vue');

Expand Down Expand Up @@ -169,11 +170,13 @@ export default mixins(externalHooks).extend({
return shorten(el.dataset.name || '', 16, 2);
},
getJsonParameterPath(path: string): string {
const convertedPath = convertPath(path);
return `{{ ${convertedPath.replace(
/^(\["?\d"?])/,
this.distanceFromActive === 1 ? '$json' : `$node["${this.node!.name}"].json`,
)} }}`;
const subPath = path.replace(/^(\["?\d"?])/, ''); // remove item position

return getMappedExpression({
nodeName: this.node.name,
distanceFromActive: this.distanceFromActive,
path: subPath,
});
},
onDragStart(el: HTMLElement) {
if (el && el.dataset.path) {
Expand Down
19 changes: 19 additions & 0 deletions packages/editor-ui/src/components/RunDataSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,23 @@ describe('RunDataJsonSchema.vue', () => {
});
expect(container).toMatchSnapshot();
});

it('renders schema with spaces and dots', () => {
renderOptions.props.data = [
{
'hello world': [
{
test: {
'more to think about': 1,
},
'test.how': 'ignore',
},
],
},
];
const { container } = render(RunDataJsonSchema, renderOptions, (vue) => {
vue.use(PiniaVuePlugin);
});
expect(container).toMatchSnapshot();
});
});
8 changes: 7 additions & 1 deletion packages/editor-ui/src/components/RunDataSchemaItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { computed } from 'vue';
import { INodeUi, Schema } from '@/Interface';
import { checkExhaustive, shorten } from '@/utils';
import { getMappedExpression } from '@/utils/mappingUtils';

type Props = {
schema: Schema;
Expand Down Expand Up @@ -35,7 +36,12 @@ const text = computed(() =>
);

const getJsonParameterPath = (path: string): string =>
`{{ ${props.distanceFromActive === 1 ? '$json' : `$node["${props.node!.name}"].json`}${path} }}`;
getMappedExpression({
nodeName: props.node!.name,
distanceFromActive: props.distanceFromActive,
path,
});

const transitionDelay = (i: number) => `${i * 0.033}s`;

const getIconBySchemaType = (type: Schema['type']): string => {
Expand Down
30 changes: 11 additions & 19 deletions packages/editor-ui/src/components/RunDataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ import { mapStores } from 'pinia';
import { useWorkflowsStore } from '@/stores/workflows';
import { useNDVStore } from '@/stores/ndv';
import MappingPill from './MappingPill.vue';
import { getMappedExpression } from '@/utils/mappingUtils';

const MAX_COLUMNS_LIMIT = 40;

Expand Down Expand Up @@ -315,11 +316,11 @@ export default mixins(externalHooks).extend({
return '';
}

if (this.distanceFromActive === 1) {
return `{{ $json["${column}"] }}`;
}

return `{{ $node["${this.node.name}"].json["${column}"] }}`;
return getMappedExpression({
nodeName: this.node.name,
distanceFromActive: this.distanceFromActive,
path: [column],
});
},
getPathNameFromTarget(el: HTMLElement) {
if (!el) {
Expand All @@ -343,21 +344,12 @@ export default mixins(externalHooks).extend({
if (!this.node) {
return '';
}

const expr = path.reduce((accu: string, key: string | number) => {
if (typeof key === 'number') {
return `${accu}[${key}]`;
}

return `${accu}["${key}"]`;
}, '');
const column = this.tableData.columns[colIndex];

if (this.distanceFromActive === 1) {
return `{{ $json["${column}"]${expr} }}`;
}

return `{{ $node["${this.node.name}"].json["${column}"]${expr} }}`;
return getMappedExpression({
nodeName: this.node.name,
distanceFromActive: this.distanceFromActive,
path: [column, ...path],
});
},
isEmpty(value: unknown): boolean {
return (
Expand Down
Loading