UNPKG

2.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const _ = require("lodash");
4const automationMetadata_1 = require("../metadata/automationMetadata");
5/**
6 * Populate the parameters of the command handler instance,
7 * performing type coercion if necessary
8 * @param instanceToPopulate parameters instance (may be handler instance itself)
9 * @param hm handler metadata
10 * @param args string args
11 */
12function populateParameters(instanceToPopulate, hm, args) {
13 args.forEach(arg => {
14 if (arg.value !== undefined) {
15 const parameter = hm.parameters.find(p => p.name === arg.name);
16 if (parameter) {
17 _.update(instanceToPopulate, parameter.name, () => computeValue(parameter, arg.value));
18 }
19 }
20 });
21}
22exports.populateParameters = populateParameters;
23function populateValues(instanceToPopulate, am, configuration) {
24 (am.values || []).forEach(v => {
25 let configValue;
26 if (!v.path || v.path.length === 0) {
27 configValue = configuration;
28 }
29 else {
30 configValue = _.get(configuration, v.path);
31 }
32 if (!configValue && v.required) {
33 throw new Error(`Required @Value '${v.path}' in '${instanceToPopulate.constructor.name}' is not available in configuration`);
34 }
35 else {
36 _.update(instanceToPopulate, v.name, () => computeValue({ name: v.name, type: v.type }, configValue));
37 }
38 });
39}
40exports.populateValues = populateValues;
41function computeValue(parameter, value) {
42 // Convert type if necessary
43 switch (parameter.type) {
44 case "string":
45 case undefined:
46 // It's a string. Keep the value the same
47 break;
48 case automationMetadata_1.FreeChoices:
49 // It's a string array. Keep the value the same
50 break;
51 case "boolean":
52 if (typeof value !== "boolean") {
53 value = value === "true" || value === "yes" || value === "1";
54 }
55 break;
56 case "number":
57 if (typeof value === "string") {
58 value = parseInt(value, 10);
59 }
60 else {
61 throw new Error(`Parameter '${parameter.name}' has array value, but is numeric`);
62 }
63 break;
64 default:
65 // It's a Chooser
66 const chooser = parameter.type;
67 if (chooser.pickOne) {
68 if (typeof value !== "string") {
69 throw new Error(`Parameter '${parameter.name}' has array value, but should be string`);
70 }
71 }
72 else {
73 if (typeof value.value === "string") {
74 throw new Error(`Parameter '${parameter.name}' has string value, but should be array`);
75 }
76 }
77 break;
78 }
79 return value;
80}
81//# sourceMappingURL=parameterPopulation.js.map
\No newline at end of file