resolveReferences function
Resolves variable references within the environment map.
If a value contains ${VAR_NAME}
, it is replaced with the actual value of VAR_NAME
.
If the referenced variable does not exist, it is replaced with an empty string.
Example:
final env = {'APP_URL': 'http://localhost', 'API_PATH': '${APP_URL}/api'};
final resolved = resolveReferences(env);
print(resolved['API_PATH']); // Output: http://localhost/api
Implementation
Map<String, String> resolveReferences(Map<String, String> envMap) {
final regex = RegExp(r'\$\{([A-Z0-9_]+)\}'); // Match ${VAR_NAME}
envMap.updateAll((key, value) {
return value.replaceAllMapped(regex, (match) {
String refKey = match.group(1) ?? '';
return envMap[refKey] ??
''; // Replace with referenced value or empty string
});
});
return envMap;
}