evaluateStringPart method

dynamic evaluateStringPart(
  1. String value,
  2. Map evaluatorContext
)

Implementation

dynamic evaluateStringPart(String value, Map evaluatorContext) {
  // Check if is a function
  final functionParts = value.split("(");
  if (functionParts.length > 1) {
    var func = evaluatorContext[functionParts.removeAt(0)];
    if (func is Function) {
      final argsString =
          functionParts[0].substring(0, functionParts[0].length - 1);

      if (argsString.isNotEmpty) {
        final args = argsString.split(",");
        if (args.length == 1) {
          return func(args[0]);
        } else {
          return func(args);
        }
      }
      return func(null);
    }
  }

  final parts = value.split(".");
  var evaluatedValue = evaluatorContext[parts.removeAt(0)];

  if (evaluatedValue == null || parts.isEmpty) {
    return evaluatedValue;
  }

  for (var part in parts) {
    if (evaluatedValue is Map) {
      if (!evaluatedValue.containsKey(part)) {
        return null;
      } else {
        evaluatedValue = evaluatedValue[part];
      }
    } else if (evaluatedValue is List<dynamic>) {
      var arrayIdx = parseInt(part, defaultValue: -1);
      if (arrayIdx < 0 || evaluatedValue.length <= arrayIdx) {
        return null;
      } else {
        evaluatedValue = evaluatedValue[arrayIdx];
      }
    } else {
      return null;
    }
  }
  return evaluatedValue;
}