evaluateString method

dynamic evaluateString(
  1. String? value,
  2. Map evaluatorContext
)

Evaluates a string using evaluatorContext. A string containing ${

Implementation

dynamic evaluateString(String? value, Map evaluatorContext) {
  if (value == null || value.isEmpty) {
    return null;
  }

  var startIdx = 0;
  while ((startIdx = value!.indexOf("\${", startIdx)) >= 0) {
    var endIdx = value.indexOf("}", startIdx);
    if (endIdx < 0) {
      return value;
    }

    // A scenery of something like ${state.${env.nameKey}}
    var nextStartIdx = value.indexOf("\${", startIdx + 2);
    if (nextStartIdx >= 0 && nextStartIdx < endIdx) {
      startIdx = nextStartIdx;
      continue;
    }

    var valueToResolve = value.substring(startIdx + 2, endIdx);
    var resolvedPart = evaluateStringPart(valueToResolve, evaluatorContext);
    if (value.length == endIdx - startIdx + 1) {
      return resolvedPart;
    } else {
      value =
          value.replaceRange(startIdx, endIdx + 1, "${resolvedPart ?? ""}");
    }
    startIdx = 0;
  }
  return value;
}