HL7 / fhir-shorthand

FHIR Shorthand
43 stars 8 forks source link

FHIRpath expressions as parameters for rulesets #182

Closed djehring closed 3 days ago

djehring commented 3 months ago

I have a ruleset that takes three parameters to create an sdc extraction context in a questionnaire definition. It works fine for fhirpaths that are simple paths, but when the path contains an expression with brackets the parsing fails.

Here is the ruleset: RuleSet: ExtractToResource(resourceid,expression,description)

Here is a happy use of the ruleset:

Here is one that fails:

and also extension paths fail:

This is the error: Assignment rules must include at least one space both before and after the '=' sign

I have tried various uses of escape characters on the expression with no success

cmoesel commented 3 months ago

Hi @djehring. The main issue you're running into is that you have not escaped the closing parentheses ) in your ruleset arguments. From the FSH 2.0 spec:

If you need to put literal ) or , characters inside values, escape them with a backslash: \) and \,, respectively.

The error you see about spaces before and after the '=' sign is misleading (I know why it's happening, but it's hard to explain). If you look further, however, you'll see another error that's a little more helpful:

Incorrect number of parameters applied to RuleSet ExtractToResource

When you don't escape the ), then the FSH compiler thinks it has hit the end of the argument list when it sees ) -- and thus it thinks there aren't enough arguments. So once you escape the ), things work much better:

* insert ExtractToResource("homeTelephone", "http://hl7.org/fhir/StructureDefinition/Patient#Patient.telecom.where(use='home' and system='phone'\).value", "Patient home phone")

and:

* insert ExtractToResource("interpreterRequired", "http://hl7.org/fhir/StructureDefinition/Patient#Patient.extension(http://hl7.org/fhir/StructureDefinition/patient-interpreterRequired\).valueBoolean", "Patient requires interpreter")

Escaping ) and , can get annoying in FHIRPath expressions, so in FSH 3.0, we introduced a new escape syntax. Wrap any ruleset argument in [[ and ]] and then you don't need to escape the ) and , anymore:

* insert ExtractToResource("homeTelephone", [["http://hl7.org/fhir/StructureDefinition/Patient#Patient.telecom.where(use='home' and system='phone').value"]], "Patient home phone")

I made an example on FSH Online to reproduce your issue and show that it works with the escapes: https://fshschool.org/FSHOnline/#/share/4bTbptX