From 8e59e117029a04196c187f8c9d58eed954e64d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E9=B9=B0?= <17kungfuboy@gmail.com> Date: Wed, 3 Aug 2022 16:40:46 +0800 Subject: [PATCH] fix: xml import params --- .../params-import/params-import.component.ts | 2 +- .../data-transfer/data-transfer.utils.ts | 26 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/workbench/browser/src/app/shared/components/params-import/params-import.component.ts b/src/workbench/browser/src/app/shared/components/params-import/params-import.component.ts index 9e5973eda..a5dbf1755 100644 --- a/src/workbench/browser/src/app/shared/components/params-import/params-import.component.ts +++ b/src/workbench/browser/src/app/shared/components/params-import/params-import.component.ts @@ -3,7 +3,7 @@ import { EoMessageService } from 'eo/workbench/browser/src/app/eoui/message/eo-m import { whatType } from '../../../utils'; import { flatData } from '../../../utils/tree/tree.utils'; import qs from 'qs'; -import { form2json, parseTree, xml2UiData, xml2json, isXML } from '../../../utils/data-transfer/data-transfer.utils'; +import { form2json, parseTree, xml2UiData, isXML } from '../../../utils/data-transfer/data-transfer.utils'; @Component({ selector: 'params-import', templateUrl: './params-import.component.html', diff --git a/src/workbench/browser/src/app/utils/data-transfer/data-transfer.utils.ts b/src/workbench/browser/src/app/utils/data-transfer/data-transfer.utils.ts index 0877d3f55..88d40f3ca 100644 --- a/src/workbench/browser/src/app/utils/data-transfer/data-transfer.utils.ts +++ b/src/workbench/browser/src/app/utils/data-transfer/data-transfer.utils.ts @@ -138,24 +138,24 @@ export const xml2json = (tmpl) => { return result; }; -interface uiData { +type uiData = { textType: ApiBodyType | string; rootType: JsonRootType | string; data: any; -} +}; export const xml2UiData = (text) => { const data: any[] = xml2json(text); - const result = {}; - const mapAttr = (obj: any) => { - const { tagName, attr, children, content } = obj; - return { - [tagName]: children.length ? mapAttr(children[0]) : attr, - }; - }; - data.forEach((it) => { - const { tagName, attr, children } = it; - result[tagName] = children.length ? mapAttr(children[0]) : attr; - }); + const deep = (list = []) => + list.reduce( + (total, { tagName, content, attr, children }) => ({ + ...total, + [tagName]: children?.length > 0 ? deep(children || []) : content, + // attribute: attr, // * not support the key for now cause ui has not show it + }), + {} + ); + const result = deep(data); + console.log('result', result); return JSON.parse(JSON.stringify(result)); }; /**