forked from pradyuk/datadog-to-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
70 lines (63 loc) · 1.73 KB
/
utils.js
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
function literalString(value) {
if (typeof value === "string") {
if (value.includes("\n")) {
return `<<EOF\n${value}\nEOF`;
}
return `"${value}"`;
}
if (Array.isArray(value)) {
let result = "[";
value.forEach((elem, index) => {
result += literalString(elem);
if (index !== value.length - 1) result += ",";
});
return result + "]";
}
return value;
}
export function assignmentString(key, value) {
if (value === null) return "";
if (key === "query") {
value = JSON.stringify(value).slice(1, -1);
}
const displayValue = literalString(value);
return `\n${key} = ${displayValue}`;
}
export function map(contents, converter) {
let result = "";
const ordered = Object.keys(contents)
.sort()
.reduce((v, k) => {
v[k] = contents[k];
return v;
}, {});
Object.entries(ordered).forEach(([k, v]) => {
result += converter(k, v);
});
return result;
}
export function block(name, contents, converter) {
return `\n${name} {${map(contents, converter)}\n}`;
}
function queryBlock(name, contents, converter) {
return `\nquery {\n\n ${name} {${map(contents, converter)}\n}}`;
}
export function blockList(array, blockName, contentConverter) {
let result = "\n";
array.forEach((elem) => {
result += block(blockName, elem, contentConverter);
});
return result;
}
export function queryBlockList(array, contentConverter) {
let result = ["\n"];
array.forEach((elem) => {
result.push(queryBlock("metric_query", elem, contentConverter));
});
result = result.join("");
return result;
}
export function convertFromDefinition(definitionSet, k, v) {
if (typeof definitionSet[k] !== "function") throw `Can't convert key '${k}'`;
return definitionSet[k](v);
}