executeFhirPath function

List executeFhirPath({
  1. required Map<String, 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,
  7. FhirVersion version = FhirVersion.r4,
})

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

  // 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;
  }

  passedEnvironment.version = version;

  try {
    if (parsedFhirPath.isEmpty) {
      return [];
    } else {
      return parsedFhirPath.execute([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,
      );
    }
  }
}