From 32add56e2b75af2e7fb0be979e12836f6e1b762c Mon Sep 17 00:00:00 2001 From: Kasdejong Date: Thu, 22 Aug 2024 11:11:11 +0200 Subject: [PATCH 1/2] Changed iif to work on collections --- .../FhirPath/Expressions/SymbolTableInit.cs | 6 ++++++ .../PocoTests/FhirPathEvaluatorTest.cs | 17 +++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs b/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs index 5ba897213e..48df4ef9dd 100644 --- a/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs +++ b/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs @@ -328,6 +328,12 @@ private static IEnumerable runIif(Closure ctx, IEnumerable 1) + { + return runSelect(ctx, [arguments.First(), (closure, _) => runIif(closure, new List{(_, _) => closure.GetThis(), arguments.Skip(1).First(), arguments.Skip(2).First(), arguments.Skip(3).FirstOrDefault()})]); + } var expression = arguments.Skip(1).First()(ctx, InvokeeFactory.EmptyArgs); var trueResult = arguments.Skip(2).First(); diff --git a/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs b/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs index 31254c4006..a3363485ce 100644 --- a/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs +++ b/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs @@ -166,14 +166,15 @@ public void TestSubsetting() [TestMethod] public void TestIIf() { - 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.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'"); } From ccd7c7063bef6c486951479591895bbf34f22ce9 Mon Sep 17 00:00:00 2001 From: Kasdejong Date: Wed, 28 Aug 2024 11:50:04 +0200 Subject: [PATCH 2/2] It really was this easy? --- .../FhirPath/Expressions/SymbolTableInit.cs | 14 ++++++-------- .../PocoTests/FhirPathEvaluatorTest.cs | 17 ++++++----------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs b/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs index 48df4ef9dd..e7544ce47c 100644 --- a/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs +++ b/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs @@ -328,14 +328,12 @@ private static IEnumerable runIif(Closure ctx, IEnumerable 1) - { - return runSelect(ctx, [arguments.First(), (closure, _) => runIif(closure, new List{(_, _) => 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(); @@ -343,8 +341,8 @@ private static IEnumerable runIif(Closure ctx, IEnumerable runWhere(Closure ctx, IEnumerable arguments) diff --git a/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs b/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs index a3363485ce..b2b1eff452 100644 --- a/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs +++ b/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs @@ -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 @@ -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().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().Subject.Should().Be("EveEveline"); } [TestMethod]