aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/utils/service-data-type-reader.ts
blob: fd5ee2fc196b7921d7e1dc0e3516326df33d7ce8 (plain)
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
/*
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2022 Nordix Foundation. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

import {DataTypeModel, PropertyBEModel} from "../models";
import {load} from 'js-yaml';

export class ServiceDataTypeReader {

    private serviceDataType = new DataTypeModel();

    public read(dataTypeFile: File): Promise<DataTypeModel> {

        return new Promise<DataTypeModel>((resolve, reject) => {
            const reader = new FileReader();
            reader.onloadend = () => {
                try {
                    const result = <String>reader.result;
                    const loadedContent = load(result);
                    console.log("Readed content: " + loadedContent);
                    this.readName(loadedContent);
                    this.readDerivedFrom(this.getDataType(loadedContent));
                    this.readDescription(this.getDataType(loadedContent));
                    this.readProperties(this.getDataType(loadedContent));
                    resolve(this.serviceDataType);
                } catch (error) {
                    reject(error);
                }
            }
            reader.readAsText(dataTypeFile);
        });
    }

    private getDataType(fileContent:any) {
        const index = Object.keys(fileContent).indexOf("data_types",0)
        if (index == -1){
            return fileContent;
        }
        return fileContent["data_types"];
    }

    private readName(fileContent: any) {
        this.serviceDataType.name = Object.keys(fileContent).values().next().value;
    }

    private readDerivedFrom(fileContent: any) {
        let dataType = Object.keys(fileContent).values().next().value;
        this.serviceDataType.derivedFromName = fileContent[dataType]["derived_from"];
    }

    private readDescription(fileContent: any) {
        let dataType = Object.keys(fileContent).values().next().value;
        this.serviceDataType.description = fileContent[dataType]["description"];
    }

    private readProperties(fileContent: any) {
        this.serviceDataType.properties = new Array<PropertyBEModel>();
        let dataType = Object.keys(fileContent).values().next().value;
        const properties = fileContent[dataType]["properties"];
        Object.keys(properties).forEach((key )=>
            {
                let property = new PropertyBEModel();
                property.name = key;
                property.description = properties[key]["description"];
                property.type = properties[key]["type"];
                property.schemaType = properties[key]["schema"];
                property.required = properties[key]["required"];
                this.serviceDataType.properties.push(property);
            }
        );
    }
}