Skip to content

Commit dc1698c

Browse files
committed
feat: added support for nested values
1 parent 5fdd118 commit dc1698c

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

action_read_yaml/index.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*
88
* The variables are in the format `$(variableName)` and are replaced by their corresponding
99
* values in the resolved object. If a variable is not defined, an error will be thrown.
10+
*
11+
* If the YAML configuration file contains nested values, the output keys will use a dot notation
12+
* to represent the hierarchy of the values. See README in the root of the repo for examples.
1013
*/
1114

1215
const core = require("@actions/core");
@@ -20,20 +23,35 @@ const yaml = require("js-yaml");
2023
* @return {string} The string with all variables replaced
2124
*/
2225
const replaceVariables = (value, resolved) => {
23-
let match = value.match(/\$\(([^\)]+)\)/);
26+
if (typeof value === "object") {
27+
// Recursively replace variables in nested objects
28+
const nestedObj = {};
29+
30+
for (const key in value) {
31+
nestedObj[key] = replaceVariables(value[key], resolved);
32+
}
2433

25-
while (match !== null) {
26-
const varName = match[1];
34+
return nestedObj;
35+
} else if (typeof value === "string") {
36+
// Replace variables in string values
37+
let match = value.match(/\$\(([^\)]+)\)/);
2738

28-
if (!resolved[varName]) {
29-
throw new Error(`Variable "${varName}" is not defined`);
39+
while (match !== null) {
40+
const varName = match[1];
41+
42+
if (!resolved[varName]) {
43+
throw new Error(`Variable "${varName}" is not defined`);
44+
}
45+
46+
value = value.replace(match[0], resolved[varName]);
47+
match = value.match(/\$\(([^\)]+)\)/);
3048
}
3149

32-
value = value.replace(match[0], resolved[varName]);
33-
match = value.match(/\$\(([^\)]+)\)/);
50+
return value;
51+
} else {
52+
// Non-string, non-object values are returned as-is
53+
return value;
3454
}
35-
36-
return value;
3755
};
3856

3957
/**
@@ -55,10 +73,17 @@ async function main() {
5573
const configYaml = yaml.load(data, { schema: SCHEMA });
5674
const resolved = {};
5775

58-
// Resolve variables for each field in the config
59-
for (const key in configYaml) {
60-
resolved[key] = replaceVariables(configYaml[key], resolved);
61-
}
76+
const resolveFields = (obj, prefix = "") => {
77+
for (const [key, value] of Object.entries(obj)) {
78+
if (typeof value === "object" && value !== null) {
79+
resolveFields(value, prefix + key + ".");
80+
} else {
81+
resolved[prefix + key] = replaceVariables(value, resolved);
82+
}
83+
}
84+
};
85+
86+
resolveFields(configYaml);
6287

6388
Object.entries(resolved).map((val) => {
6489
const key = val[0];

0 commit comments

Comments
 (0)