1
0

feat: 扩展配置变量替换范围至 server/probes/targets,支持空默认值语法

This commit is contained in:
2026-05-21 18:42:42 +08:00
parent 79358ba50d
commit 6ca8b36542
6 changed files with 258 additions and 53 deletions

View File

@@ -63,14 +63,8 @@ export function resolveVariables(config: unknown): { config: unknown; issues: Co
if (!isPlainObject(config)) {
return { config, issues };
}
const configRecord = config as Record<string, unknown>;
const rawTargets: unknown = configRecord["targets"];
if (!Array.isArray(rawTargets)) {
return { config, issues };
}
const targets = rawTargets.map((target, index) => resolveTargetVariables(target, index, variables, issues));
return { config: { ...config, targets }, issues };
return { config: resolveConfigValue(config, "", variables, issues), issues };
}
function describeInvalidVariableValue(value: unknown): string {
@@ -80,6 +74,7 @@ function describeInvalidVariableValue(value: unknown): string {
}
function inferStringValue(value: string): VariableValue {
if (value === "") return value;
const numberValue = Number(value);
if (Number.isFinite(numberValue)) return numberValue;
if (value === "true") return true;
@@ -126,6 +121,31 @@ function replaceStringValue(
return escaped.reduce((result, literal, index) => result.replace(`\u0000${index}\u0000`, literal), replaced);
}
function resolveConfigValue(
value: unknown,
path: string,
variables: Map<string, VariableValue>,
issues: ConfigValidationIssue[],
): unknown {
if (!isPlainObject(value)) return value;
const result: Record<string, unknown> = {};
for (const [key, item] of Object.entries(value)) {
const itemPath = joinPath(path, key);
if (key === "variables") {
result[key] = item;
continue;
}
if (key === "targets" && Array.isArray(item)) {
result[key] = item.map((target, index) => resolveTargetVariables(target, index, variables, issues));
continue;
}
result[key] =
key === "server" || key === "probes" ? resolveValue(item, itemPath, variables, issues, { path: itemPath }) : item;
}
return result;
}
function resolveTargetVariables(
target: unknown,
index: number,
@@ -165,10 +185,9 @@ function resolveValue(
const result: Record<string, unknown> = {};
for (const [key, item] of Object.entries(value)) {
const itemPath = joinPath(path, key);
result[key] =
key === "id" || key === "type"
? item
: resolveValue(item, itemPath, variables, issues, { ...context, path: itemPath });
result[key] = shouldSkipVariableResolution(itemPath)
? item
: resolveValue(item, itemPath, variables, issues, { ...context, path: itemPath });
}
return result;
}
@@ -202,3 +221,7 @@ function resolveVariableReference(
);
return undefined;
}
function shouldSkipVariableResolution(path: string): boolean {
return /^targets\[\d+\]\.(?:id|type)$/.test(path);
}