Support custom tosca functions in operation input values
[sdc.git] / catalog-ui / src / app / utils / tosca-function-helper.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {ToscaFunctionType} from "../models/tosca-function-type.enum";
23 import {ToscaConcatFunction} from "../models/tosca-concat-function";
24 import {ToscaGetFunction} from "../models/tosca-get-function";
25 import {YamlFunction} from "../models/yaml-function";
26 import {ToscaFunction} from "../models/tosca-function";
27 import {ToscaCustomFunction} from "../models/tosca-custom-function";
28
29 export class ToscaFunctionHelper {
30
31     public static convertObjectToToscaFunction(value: any): ToscaFunction {
32         if (!value || !this.isValueToscaFunction(value)) {
33             return undefined;
34         }
35
36         switch (value.type) {
37             case ToscaFunctionType.CONCAT:
38                 return new ToscaConcatFunction(value);
39             case ToscaFunctionType.GET_PROPERTY:
40             case ToscaFunctionType.GET_INPUT:
41             case ToscaFunctionType.GET_ATTRIBUTE:
42                 return new ToscaGetFunction(value);
43             case ToscaFunctionType.YAML:
44                 return new YamlFunction(value);
45             case ToscaFunctionType.CUSTOM:
46                 return new ToscaCustomFunction(value);
47             case ToscaFunctionType.STRING:
48                 return <ToscaFunction> {
49                     type: ToscaFunctionType.STRING,
50                     value: value.value,
51                     buildValueString(): string {
52                         return this.value;
53                     },
54                     buildValueObject(): Object {
55                         return this.value;
56                     }
57                 };
58             default:
59                 return undefined;
60         }
61     }
62
63     public static isValueToscaFunction(value: any): boolean {
64         return value instanceof Object && 'type' in value && (<any>Object).values(ToscaFunctionType).includes(value.type);
65     }
66
67 }