diff options
Diffstat (limited to 'sdc-workflow-designer-ui/src/app/services')
-rw-r--r-- | sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts | 176 |
1 files changed, 119 insertions, 57 deletions
diff --git a/sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts b/sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts index a83ae2e4..b70b0ca5 100644 --- a/sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts +++ b/sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts @@ -15,75 +15,126 @@ import { TreeNode } from 'primeng/primeng'; import { ValueSource } from '../model/value-source.enum'; import { WorkflowUtil } from '../util/workflow-util'; +import { Swagger } from "../model/swagger"; import { WorkflowConfigService } from "./workflow-config.service"; @Injectable() export class SwaggerTreeConverterService { - private serviceName: string; - private serviceVersion: string; - constructor(private configService: WorkflowConfigService) { + private swagger: Swagger; - } + constructor(private workflowConfigService: WorkflowConfigService) { - public schema2TreeNode(key: string | number, serviceName: string, serviceVersion: string, schema: any): any { - this.serviceName = serviceName; - this.serviceVersion = serviceVersion; + } + public schema2TreeNode(swagger: Swagger, key: string | number, schema: any, value?: any): any { + this.swagger = swagger; if (schema.$ref) { - const treeNode = this.getTreeNodeBySwaggerDefinition(key, schema); + const treeNode = this.getTreeNodeBySwaggerDefinition(key, schema, value); return treeNode; } else { - this.setInitValue4Param(schema.value, schema); - return this.parameter2TreeNode(key, schema); + value = this.getInitValue4Param(schema, value); + return this.parameter2TreeNode(key, schema, value); } } - private getTreeNodeBySwaggerDefinition(key: string | number, schema: any): TreeNode { - const swagger = this.configService.getSwaggerInfo(this.serviceName, this.serviceVersion); - if(swagger === undefined) { - console.log(`swagger definition not exist, [${this.serviceName}].[${this.serviceVersion}]`); - return null; - } - const swaggerDefinition = this.configService.getDefinition(swagger, schema.$ref); + private getTreeNodeBySwaggerDefinition(key: string | number, schema: any, value: any): TreeNode { + const swaggerDefinition = this.workflowConfigService.getDefinition(this.swagger, schema.$ref); const definitionCopy = WorkflowUtil.deepClone(swaggerDefinition); - this.setInitValue4Param(schema.value, definitionCopy); - if (schema.value !== definitionCopy.value) { - schema.value = definitionCopy.value; + value = this.getInitValue4Param(definitionCopy, value); + + return this.schema2TreeNode(this.swagger, key, definitionCopy, value); + } + + private getInitValue4Param(definition: any, value: any) { + if (definition.$ref) { + definition = this.workflowConfigService.getDefinition(this.swagger, definition.$ref); + } + if (definition.type === 'object') { + return this.getInitValue4Object(value); + } else if (definition.type === 'array') { + return this.getInitValue4Array(value); + } else { // primary type + return this.getInitValue4Primate(value); + } + } + + private getInitValue4Object(value: any) { + const newValue = { + value: {}, + valueSource: ValueSource[ValueSource.Definition] + }; + + if(!value) { + return newValue; + } else { + if(value.valueSource !== ValueSource[ValueSource.Definition]) { + return value; + } else { + if(typeof value.value !== 'object') { + value.value = {}; + } + return value; } + } + } - return this.schema2TreeNode(key, this.serviceName, this.serviceVersion, definitionCopy); + private getInitValue4Array(value: any) { + const newValue = { + value: [], + valueSource: ValueSource[ValueSource.Definition] + }; + + if(!value) { + return newValue; + } else { + if(value.valueSource !== ValueSource[ValueSource.Definition]) { + return value; + } else { + if(!(value.value instanceof Array)) { + value.value = []; + } + return value; + } + } } - private setInitValue4Param(value: any | any[], param: any) { - param.value = value; - if (value == null) { - if (param.type === 'object') { - param.value = {}; - } else if (param.type === 'array') { - param.value = []; - } else { // primary type - param.valueSource = ValueSource[ValueSource.String]; - } + private getInitValue4Primate(value: any) { + const newValue = { + value: '', + valueSource: ValueSource[ValueSource.String] + }; + + if(!value) { + return newValue; + } else { + if(typeof value.value === 'object') { + value.value = ''; } + return value; + } } - private parameter2TreeNode(name: string | number, paramDefinition: any): any { - const nodeType = this.getTreeNodeType(paramDefinition); + private parameter2TreeNode(name: string | number, definition: any, value: any): any { + const nodeType = this.getTreeNodeType(definition); const node = { label: name, type: nodeType, + required: definition.required, children: [], - parameter: paramDefinition, + definition: definition, + value: value, }; - if (paramDefinition.type === 'object') { - node.children = this.getPropertyFromObject(paramDefinition.value, paramDefinition); - } else if (paramDefinition.type === 'array') { - this.setChildrenForArray(node, paramDefinition); + if(value.valueSource === ValueSource[ValueSource.Definition]) { + if (definition.type === 'object') { + node.children = this.getPropertyFromObject(definition, value.value); + } else if (definition.type === 'array') { + this.setChildrenForArray(definition, value.value); + } } return node; @@ -104,47 +155,58 @@ export class SwaggerTreeConverterService { } } - private setChildrenForArray(node: any, param: any) { - param.value.forEach((itemValue, index) => { - const itemCopy = WorkflowUtil.deepClone(param.items); - itemCopy.value = itemValue; - node.children.push(this.schema2TreeNode(index, this.serviceName, this.serviceVersion, itemCopy)); + private setChildrenForArray(definition: any, value: any[]): any[] { + const children = []; + value.forEach((itemValue, index) => { + const itemCopy = WorkflowUtil.deepClone(definition.items); + children.push(this.schema2TreeNode(this.swagger, index, itemCopy, itemValue)); }); + + return children; } - private getPropertyFromObject(objectValue: any, param: any): TreeNode[] { - if (param.properties) { - return this.getPropertyFromSimpleObject(objectValue, param.properties); - } else if (param.additionalProperties) { - return this.getPropertyFromMapOrDictionary(objectValue, param.additionalProperties); + private getPropertyFromObject(definition: any, value: any): TreeNode[] { + if (definition.properties) { + return this.getPropertyFromSimpleObject(definition.properties, value, definition.required); + } else if (definition.additionalProperties) { + return this.getPropertyFromMapOrDictionary(definition.additionalProperties, value); } else { + console.log('getPropertyFromObject() return [], param is:' + JSON.stringify(definition)); return []; } } - private getPropertyFromSimpleObject(objectValue: any, properties: any): TreeNode[] { + private getPropertyFromSimpleObject(properties: any, objectValue: any, required: string[]): TreeNode[] { const treeNodes: TreeNode[] = []; for (const key in properties) { - const property = properties[key]; - this.setInitValue4Param(objectValue[key], property); - - if (property.value !== objectValue[key]) { - objectValue[key] = property.value; + let property = properties[key]; + // init required property + property.required = false; + if (Array.isArray(required)) { + for (let index = 0; index < required.length; index++) { + if (required[index] === key) { + property.required = true; + break; + } + } } - treeNodes.push(this.schema2TreeNode(key, this.serviceName, this.serviceVersion, property)); + objectValue[key] = this.getInitValue4Param(property, objectValue[key]); + + const treeNode = this.schema2TreeNode(this.swagger, key, property, objectValue[key]); + treeNodes.push(treeNode); } return treeNodes; } - private getPropertyFromMapOrDictionary(mapOrDictionary: any, additionalProperties: any): TreeNode[] { + private getPropertyFromMapOrDictionary(additionalProperties: any, mapOrDictionary: any): TreeNode[] { const treeNodes: TreeNode[] = []; for (const key in mapOrDictionary) { const propertyCopy = WorkflowUtil.deepClone(additionalProperties); propertyCopy.value = mapOrDictionary[key]; - const treeNode = this.schema2TreeNode(key, this.serviceName, this.serviceVersion, propertyCopy); + const treeNode = this.schema2TreeNode(this.swagger, key, propertyCopy, propertyCopy.value); treeNode.keyEditable = true; treeNodes.push(treeNode); |