resolveVariable method

dynamic resolveVariable(
  1. dynamic input
)

Resolves the given input with any potential variable on the controller. Variable values use the mustache format and must begin with {{ and end with }}. If the input is not marked as a variable or there is no variable with the matching key registered then the input will be return unaltered.

Implementation

dynamic resolveVariable(dynamic input) {
  dynamic result = input;
  if (input is String && input.contains('{{') && input.contains('}}')) {
    final regex = RegExp(r'\{\{[^(})]*}}]*');

    var matches = regex.allMatches(input);
    if (matches.isNotEmpty == true) {
      if (matches.length == 1 &&
          input.startsWith('{{') &&
          input.endsWith('}}')) {
        final match = matches.first;
        final variableName =
            result.substring(match.start + 2, match.end - 2).trim();
        final value = getVariable(variableName);
        if (value != null ||
            _testVariables.containsKey(variableName) == true ||
            _globalVariables.containsKey(variableName) == true) {
          result = value;
        }
      } else {
        matches = matches.toList().reversed;

        for (var match in matches) {
          final variableName =
              result.substring(match.start + 2, match.end - 2).trim();

          final value = getVariable(variableName);
          if (value != null ||
              _testVariables.containsKey(variableName) == true ||
              _globalVariables.containsKey(variableName) == true) {
            result = value == null
                ? null
                : '${result.substring(0, match.start)}$value${result.substring(match.end, result.length)}';
          }
        }
      }
    }
  }

  return result;
}