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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 import {PropertySource} from "./property-source";
23 import {ToscaGetFunctionType} from "./tosca-get-function-type";
24 import {ToscaFunction} from "./tosca-function";
25 import {ToscaFunctionType} from "./tosca-function-type.enum";
26 import {ToscaFunctionParameter} from "./tosca-function-parameter";
28 export class ToscaGetFunction implements ToscaFunction, ToscaFunctionParameter {
29 type: ToscaFunctionType;
30 propertyUniqueId: string;
32 propertySource: PropertySource;
33 sourceUniqueId: string;
35 functionType: ToscaGetFunctionType;
36 propertyPathFromSource: Array<string>;
39 constructor(toscaGetFunction?: ToscaGetFunction) {
40 if (!toscaGetFunction) {
43 this.type = toscaGetFunction.type;
44 this.value = toscaGetFunction.value;
45 this.propertyUniqueId = toscaGetFunction.propertyUniqueId;
46 this.propertyName = toscaGetFunction.propertyName;
47 this.propertySource = toscaGetFunction.propertySource;
48 this.sourceUniqueId = toscaGetFunction.sourceUniqueId;
49 this.sourceName = toscaGetFunction.sourceName;
50 this.functionType = toscaGetFunction.functionType;
51 if (toscaGetFunction.propertyPathFromSource) {
52 this.propertyPathFromSource = [...toscaGetFunction.propertyPathFromSource];
56 public buildValueString(): string {
57 return JSON.stringify(this.buildValueObject());
60 public buildValueObject(): Object {
61 if (this.functionType == ToscaGetFunctionType.GET_PROPERTY || this.functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
62 return this.buildFunctionValueWithPropertySource();
64 if (this.functionType == ToscaGetFunctionType.GET_INPUT) {
65 return this.buildGetInputFunctionValue();
70 private buildGetInputFunctionValue(): Object {
71 if (this.propertyPathFromSource.length === 1) {
72 return {[this.functionType.toLowerCase()]: this.propertyPathFromSource[0]};
74 return {[this.functionType.toLowerCase()]: this.propertyPathFromSource};
77 private buildFunctionValueWithPropertySource(): Object {
78 if (this.propertySource == PropertySource.SELF) {
80 [this.functionType.toLowerCase()]: [PropertySource.SELF, ...this.propertyPathFromSource]
83 if (this.propertySource == PropertySource.INSTANCE) {
85 [this.functionType.toLowerCase()]: [this.sourceName, ...this.propertyPathFromSource]