executeFhirPath function

List executeFhirPath({
  1. required dynamic context,
  2. required ParserList parsedFhirPath,
  3. required String pathExpression,
  4. Map<String, dynamic>? resource,
  5. Map<String, dynamic>? rootResource,
  6. Map<String, dynamic>? environment,
})

Execute the FHIRPath as pre-parsed by parseFhirPath.

May be invoked repeatedly on the same parsed FHIRPath, resulting in a performance gain over walkFhirPath.

All parameters have the same meaning as for walkFhirPath.

Implementation

List<dynamic> executeFhirPath({
  ///
  required dynamic context,
  required ParserList parsedFhirPath,
  required String pathExpression,
  Map<String, dynamic>? resource,
  Map<String, dynamic>? rootResource,
  Map<String, dynamic>? environment,
}) {
  // Use passed-in environment as the starting point.
  // It will later be amended/overridden by explicitly passed resources.
  final Map<String, dynamic> passedEnvironment =
      Map<String, dynamic>.from(environment ?? <String, dynamic>{});

  // Explicitly passed context overrides context that might have been passed
  // through environment.
  passedEnvironment.context = context;

  // Explicitly passed resource overrides resource that might have been passed
  // through environment.
  if (resource != null) {
    passedEnvironment.resource = resource;
  }

  // Explicitly passed rootResource overrides rootResource that might have been passed
  // through environment.
  if (rootResource != null) {
    passedEnvironment.rootResource = rootResource;
  }

  try {
    if (parsedFhirPath.isEmpty) {
      return <dynamic>[];
    } else {
      return parsedFhirPath.execute(
          context is List ? <dynamic>[...context] : <dynamic>[context],
          passedEnvironment);
    }
  } catch (error) {
    if (error is FhirPathException) {
      rethrow;
    } else {
      throw FhirPathException(
        'Unable to execute FHIRPath expression',
        pathExpression: pathExpression,
        cause: error,
        context: context,
        environment: passedEnvironment,
      );
    }
  }
}