2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 Nordix Foundation. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 import {DataTypeModel, PropertyBEModel} from "../models";
22 import {load} from 'js-yaml';
24 export class ServiceDataTypeReader {
26 private serviceDataType = new DataTypeModel();
28 public read(dataTypeFile: File): Promise<DataTypeModel> {
30 return new Promise<DataTypeModel>((resolve, reject) => {
31 const reader = new FileReader();
32 reader.onloadend = () => {
34 const result = <String>reader.result;
35 const loadedContent = load(result);
36 console.log("Readed content: " + loadedContent);
37 this.readName(this.getDataType(loadedContent));
38 this.readDerivedFrom(this.getDataType(loadedContent));
39 this.readDescription(this.getDataType(loadedContent));
40 this.readProperties(this.getDataType(loadedContent));
41 resolve(this.serviceDataType);
46 reader.readAsText(dataTypeFile);
50 private getDataType(fileContent:any) {
51 const index = Object.keys(fileContent).indexOf("data_types",0)
55 return fileContent["data_types"];
58 private readName(fileContent: any) {
59 this.serviceDataType.name = Object.keys(fileContent).values().next().value;
62 private readDerivedFrom(fileContent: any) {
63 let dataType = Object.keys(fileContent).values().next().value;
64 this.serviceDataType.derivedFromName = fileContent[dataType]["derived_from"];
67 private readDescription(fileContent: any) {
68 let dataType = Object.keys(fileContent).values().next().value;
69 this.serviceDataType.description = fileContent[dataType]["description"];
72 private readProperties(fileContent: any) {
73 this.serviceDataType.properties = new Array<PropertyBEModel>();
74 let dataType = Object.keys(fileContent).values().next().value;
75 const properties = fileContent[dataType]["properties"];
76 Object.keys(properties).forEach((key )=>
78 let property = new PropertyBEModel();
80 property.description = properties[key]["description"];
81 property.type = properties[key]["type"];
82 property.schemaType = properties[key]["schema"];
83 property.required = properties[key]["required"];
84 this.serviceDataType.properties.push(property);