Skip to content

Commit

Permalink
Merge pull request #2842 from FirelyTeam/feature/FP-iif-context
Browse files Browse the repository at this point in the history
Changed iif to work with the correct context
  • Loading branch information
ewoutkramer authored Sep 2, 2024
2 parents 195a559 + ccd7c70 commit f859b8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 7 additions & 3 deletions src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,21 @@ private static IEnumerable<ITypedElement> runIif(Closure ctx, IEnumerable<Invoke
{
// iif(criterion: expression, true-result: collection [, otherwise-result: collection]) : collection
// note: short-circuit behavior is expected in this function
var focus = arguments.First()(ctx, InvokeeFactory.EmptyArgs);

var expression = arguments.Skip(1).First()(ctx, InvokeeFactory.EmptyArgs);
var newContext = ctx.Nest(focus);
newContext.SetThis(focus);

var expression = arguments.Skip(1).First()(newContext, InvokeeFactory.EmptyArgs);
var trueResult = arguments.Skip(2).First();
var otherResult = arguments.Skip(3).FirstOrDefault();

if (expression.Count() > 1)
throw Error.InvalidOperation($"Result of {nameof(expression)} is not of type boolean");

return (expression.BooleanEval() ?? false)
? trueResult(ctx, InvokeeFactory.EmptyArgs)
: otherResult == null ? ElementNode.EmptyList : otherResult(ctx, InvokeeFactory.EmptyArgs);
? trueResult(newContext, InvokeeFactory.EmptyArgs) // share focus with this function
: otherResult == null ? ElementNode.EmptyList : otherResult(newContext, InvokeeFactory.EmptyArgs);
}

private static IEnumerable<ITypedElement> runWhere(Closure ctx, IEnumerable<Invokee> arguments)
Expand Down
16 changes: 6 additions & 10 deletions src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Xml.Linq;

namespace Hl7.FhirPath.R4.Tests
Expand Down Expand Up @@ -164,17 +165,12 @@ public void TestSubsetting()


[TestMethod]
public void TestIIf()
public void TestIIfContext()
{
fixture.IsTrue(@"Patient.name.iif(exists(), 'named', 'unnamed') = 'named'");
fixture.IsTrue(@"Patient.name.iif(empty(), 'unnamed', 'named') = 'named'");

fixture.IsTrue(@"Patient.contained[0].name.iif(exists(), 'named', 'unnamed') = 'named'");
fixture.IsTrue(@"Patient.contained[0].name.iif(empty(), 'unnamed', 'named') = 'named'");

fixture.IsTrue(@"Patient.name.iif({}, 'named', 'unnamed') = 'unnamed'");

// fixture.IsTrue(@"Patient.name[0].family.iif(length()-8 != 0, 5/(length()-8), 'no result') = 'no result'");
var res = fixture.TestInput.Scalar("Patient.contained[0].name[0].given[0].iif(true, single())");
res.Should().BeOfType<string>().Subject.Should().Be("Eve");
res = fixture.TestInput.Scalar("Patient.contained[0].name.iif(use = 'official', 'given', given.join(''))"); // comparison between collection and string
res.Should().BeOfType<string>().Subject.Should().Be("EveEveline");
}

[TestMethod]
Expand Down

0 comments on commit f859b8d

Please sign in to comment.