forked from ModelEngine-Group/fit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodMetaDataParser.js
More file actions
181 lines (171 loc) · 6.02 KB
/
Copy pathMethodMetaDataParser.js
File metadata and controls
181 lines (171 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {v4 as uuidv4} from 'uuid';
import {FROM_TYPE} from '@/common/Consts.js';
/**
* 将接口出参元数据信息转换成如下的格式
*
* {
* id: "output_" + uuidv4(),
* name: "output",
* type: "Object",
* value: [
* {
* id: uuidv4(),
* name: "age",
* type: "Integer",
* value: "Integer"
* },
* {
* id: uuidv4(),
* name: "height",
* type: "Integer",
* value: "Integer"
*
* }]
* }
*
* @param input 入参数据,格式如下:{
* "type": "object",
* "properties": {
* "testStr": {
* "type": "object",
* "properties": {
* "P3": {
* "type": "object",
* "properties": {
* "P4": {
* "type": "string"
* }
* }
* }
* }
* }
* }
* }
*
* @return {{name: string, id: string, type: string, value: *[]}}
*/
export const convertReturnFormat = input => {
const processObjectProperty = (name, obj) => {
const result = [];
if (obj.type === "object") {
for (const prop in obj.properties) {
const property = obj.properties[prop];
if (property.type === "object") {
result.push(...processObjectProperty(prop, property));
} else {
result.push({
id: uuidv4(),
name: prop,
type: property.type.capitalize(),
value: property.type.capitalize()
});
}
}
}
return [{
id: 'output_' + uuidv4(),
name: name,
type: 'Object',
value: result
}];
};
const output = {
id: "output_" + uuidv4(),
name: "output",
type: "",
value: []
};
if (input.type === "object") {
output.type = "Object";
const isMap = input.hasOwnProperty("additionalProperties") && input.additionalProperties.hasOwnProperty("type");
if (isMap) {
return output;
}
const properties = input.properties;
for (const prop in properties) {
const property = properties[prop];
if (property.type === "object") {
output.value.push(...processObjectProperty(prop, property));
} else {
output.value.push({
id: uuidv4(),
name: prop,
type: property.type.capitalize(),
value: property.type.capitalize()
});
}
}
} else {
output.type = input.type.capitalize();
}
return output;
};
/**
* 将接口入参元数据信息转换成如下的格式:
* {
* id: "p1_" + uuidv4(),
* name: "p1",
* type: "String",
* from: "Reference",
* referenceNode: "",
* referenceId: "",
* referenceKey: "",
* value: []
* }
*
* @param param 对象 {propertyName : "p1", property: {
* "type": "string",
* "default": "default_value"
* }}
* @return {{referenceNode: string, name, from: (string), id: string, type: (string|*), value: *[], referenceId: string, referenceKey: string}}
*/
export const convertParameter = param => {
const isMap = param.property.hasOwnProperty("additionalProperties") && param.property.additionalProperties.hasOwnProperty("type");
const _getFromType = () => {
if (Object.prototype.hasOwnProperty.call(param.property, 'default')) {
return FROM_TYPE.INPUT;
}
if (param.property.type === 'object') {
if (!isMap && param.property.properties) {
return FROM_TYPE.EXPAND;
} else {
return FROM_TYPE.REFERENCE;
}
}
return FROM_TYPE.REFERENCE;
};
const result = {
id: param.propertyName + "_" + uuidv4(),
name: param.propertyName,
type: param.property.type === 'object' ? 'Object' : param.property.type.capitalize(),
description: param.property.description,
// 对象默认展开,map直接为引用
from: _getFromType(),
isRequired: param.isRequired,
referenceNode: "",
referenceId: "",
referenceKey: "",
value: param.property.default ? param.property.default : [],
};
if (isMap) {
result.generic = "Map";
}
// 如果入参为map,或者properties为空,则不进行属性展开
if (param.property.type === 'object' && !isMap && param.property.properties) {
const properties = param.property.properties;
result.value = Object.keys(properties).map(key => {
return convertParameter({
propertyName: key,
property: properties[key],
isRequired: Array.isArray(param.property.required) ? param.property.required.includes(key) : false,
});
});
result.props = [...result.value];
}
return result;
};