queryString function

Object? queryString(
  1. String expression,
  2. String input, {
  3. Format? format,
})

Parse an input string in the given format, then evaluate expression.

If format is omitted, attempts to detect it from the content.

Throws QueryError on parse or evaluation errors. Throws FormatException if JSON input is malformed.

Implementation

Object? queryString(String expression, String input, {Format? format}) {
  final data = input_.parseInput(input, format ?? input_.sniffFormat(input));
  // parseInput produces canonical Map<String, Object?> / List<Object?> trees;
  // skip normalization.
  final result = parser_.parseQuery(expression);
  final ast = switch (result) {
    Success<ParseError, LamExpr>(:final value) => value,
    Partial<ParseError, LamExpr>() =>
      throw QueryError(_formatParseErrors(expression, result.errors)),
    Failure<ParseError, LamExpr>() =>
      throw QueryError(_formatParseErrors(expression, result.errors)),
  };
  try {
    return eval_.evaluate(ast, data);
  } on EvalException catch (e) {
    throw QueryError(e.message);
  }
}