Skip to content

Commit

Permalink
It really was this easy?
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasdejong committed Aug 28, 2024
1 parent 32add56 commit ccd7c70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
14 changes: 6 additions & 8 deletions src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,23 +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);
if (focus.Count() > 1)
{
return runSelect(ctx, [arguments.First(), (closure, _) => runIif(closure, new List<Invokee>{(_, _) => closure.GetThis(), arguments.Skip(1).First(), arguments.Skip(2).First(), arguments.Skip(3).FirstOrDefault()})]);
}

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
17 changes: 6 additions & 11 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,18 +165,12 @@ public void TestSubsetting()


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

var resultWithSelect = fixture.PatientExample.Select("Patient.name.select(iif(use = 'official', given, 'not official')).join(', ')");
var resultWithoutSelect = fixture.PatientExample.Select("Patient.name.iif(use = 'official', given, 'not official').join(', ')");
resultWithSelect.Should().BeEquivalentTo(resultWithoutSelect);
resultWithSelect.Should().ContainSingle().Subject.Should().BeEquivalentTo(new FhirString("Peter, James, not official"));

// 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 ccd7c70

Please sign in to comment.