Skip to content

Commit

Permalink
🐛 Catch and get value of FHIR_DATA_TYPES object values in AbstractFhi…
Browse files Browse the repository at this point in the history
…rPathFunctionLibrary
  • Loading branch information
camemre49 committed Nov 5, 2024
1 parent f741ecf commit 4ba4c5d
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.onfhir.path

import io.onfhir.api.FHIR_DATA_TYPES
import io.onfhir.path.grammar.FhirPathExprParser.ExpressionContext

import java.lang.reflect.InvocationTargetException

import io.onfhir.path.annotation.FhirPathFunction

import scala.reflect.runtime.currentMirror
import scala.reflect.runtime.universe._

Expand Down Expand Up @@ -44,6 +45,10 @@ abstract class AbstractFhirPathFunctionLibrary {
args.collect({
// matches 'String's in the sequence
case Literal(Constant(s: String)) => s

case rest =>
// matches 'FHIR_DATA_TYPES' fields
getFhirDataTypeValue(rest.toString())
})
})
// create an instance of FhirPathFunction
Expand Down Expand Up @@ -81,4 +86,30 @@ abstract class AbstractFhirPathFunctionLibrary {
}
}
}

/**
* Retrieves the corresponding value of a FHIR data type from its string representation.
* This function uses reflection to access fields in the FHIR_DATA_TYPES class dynamically.
* Because the values of FHIR_DATA_TYPES that comes from ToFHIR's api usage are not accessible otherwise.
* @param fhirDataTypeString The string representation of the FHIR data type.
* @return An Option containing the string value of the FHIR data type if found, or None if not.
*/
private def getFhirDataTypeValue(fhirDataTypeString: String): Option[String] = {
// Regular expression to extract the type name from a FHIR_DATA_TYPES reference
val pattern = """.*FHIR_DATA_TYPES\.(\w+)""".r
// Match the input string against the pattern to find a corresponding FHIR type
fhirDataTypeString match {
case pattern(typeName) =>
try {
// Use reflection to invoke the method corresponding to the type name
val value = FHIR_DATA_TYPES.getClass.getMethod(typeName).invoke(FHIR_DATA_TYPES)
Some(value.asInstanceOf[String]) // Return the value as an Option
} catch {
case e: Exception =>
None // Return None if any exception occurs during reflection
}
case _ =>
None // Return None if the input string does not match the pattern
}
}
}

0 comments on commit 4ba4c5d

Please sign in to comment.