added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / hooks / convertToYAMLRecursive.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 const YAML = require('yamljs');\r
18 \r
19 module.exports = function (convertTo) {\r
20 \r
21         const toConvert = ['testDataTemplate'];\r
22 \r
23         function convert(p) {\r
24                 for (var key in p) {\r
25                         if (p.hasOwnProperty(key) && (typeof p[key] === 'object' || typeof p[key] === 'array') ) {\r
26                                 if (toConvert.indexOf(key) < 0) {\r
27                                         convert(p[key])\r
28                                 } else {\r
29                                         \r
30                                         if(convertTo == 'yaml'){\r
31                                                 p[key] = YAML.stringify(p[key]);\r
32                                         }\r
33                                         if(convertTo == 'json'){\r
34                                                 console.log(key)\r
35                                                 console.log(p[key]);\r
36                                                 p[key] = convertTabs(p[key]);\r
37                                                 p[key] = YAML.parse(p[key]);\r
38                                                 console.log(p[key]);\r
39                                         }\r
40                                 }\r
41                         }else{\r
42                                 if (toConvert.indexOf(key) >= 0) {\r
43                                                 \r
44                                         if(convertTo == 'yaml'){\r
45                                                 p[key] = YAML.stringify(p[key]);\r
46                                         }\r
47                                         if(convertTo == 'json'){\r
48                                                 p[key] = convertTabs(p[key]);\r
49                                                 p[key] = YAML.parse(p[key]);\r
50                                                 console.log(p[key])\r
51                                         }\r
52                                 }\r
53                         }\r
54                 }\r
55         }\r
56 \r
57         function convertTabs(str){\r
58                 if(typeof str === 'string'){\r
59                         return str.replace(/\t/g, '    ');\r
60                 }\r
61     }\r
62 \r
63         return async context => {\r
64                 if(context.result)\r
65                         convert(context.result);\r
66                 if(context.data)\r
67                         convert(context.data);\r
68                 return context;\r
69         };\r
70 };\r