evaluateLiteralExpression function

dynamic evaluateLiteralExpression(
  1. MetaObject context,
  2. String expression
)

Implementation

dynamic evaluateLiteralExpression(MetaObject context, String expression) {
  if (expression == null) return null;
  QuickExpressionEvalutator evaluator = QuickExpressionEvalutator();
  List<String> templateParts = [];
  List<String> expList = [];
  int pos = 0;
  while (true) {
    var start = expression.indexOf('\${', pos);
    var end = expression.indexOf('}', pos);
    if (start == -1) {
      if (pos < expression.length) {
        templateParts.add(expression.substring(pos));
      }
      break;
    }
    if (start > pos) {
      templateParts.add(expression.substring(pos, start));
    }
    if (end != -1) {
      expList.add(expression.substring(start + 2, end));
      pos = end + 1;
    }
  }

  if (templateParts.length == 0 && expList.length == 1) {
    var exp = expList[0];
    return evaluator.evaluate(context.getMetaData(), exp);
  } else {
    String r = "";
    int i = 0, j = 0;
    while (i < templateParts.length || j < expList.length) {
      if (i < templateParts.length) {
        r += templateParts[i];
        i++;
      }
      if (j < expList.length) {
        var exp = expList[j];
        j++;
        var expresult = evaluator.evaluate(context.getMetaData(), exp);
        if (expresult != null) {
          r += expresult.toString();
        }
      }
    }
    return r;
  }
}