public static IEnumerable <ITypedElement> Extension(this IEnumerable <ITypedElement> focus, string url) { return(focus.Navigate("extension") .Where(es => es.Navigate("url") .Single() .IsEqualTo(ElementNode.ForPrimitive(url)))); }
private IEnumerable <(string name, object valueObj, object typeObj)> ToTabularInternal(ElementNode fhirRoot, DefinitionNode definationRoot) { foreach (ColumnDefinition column in definationRoot.ColumnDefinitions) { if (!string.IsNullOrEmpty(column.Type)) { ElementNode valueNode = fhirRoot; if (!string.IsNullOrEmpty(column.FhirExpression)) { var valueFromExpression = fhirRoot.Select(column.FhirExpression).FirstOrDefault(); valueFromExpression = valueFromExpression ?? ElementNode.ForPrimitive(string.Empty); valueNode = ElementNode.FromElement(ElementNode.FromElement(valueFromExpression)); } (object valueObj, object typeObj) = ConvertElementNode(valueNode, column.Type); yield return(column.Name, valueObj, typeObj); } } foreach (var(propertyName, definationNode) in definationRoot.Children) { var fhirChildNode = fhirRoot; if (fhirRoot != null && !string.IsNullOrEmpty(propertyName)) { fhirChildNode = fhirRoot[propertyName]?.FirstOrDefault(); } foreach (var subResult in ToTabularInternal(fhirChildNode, definationNode)) { yield return(subResult); } } }
public void GivenAResourceWithDifferentSecurityLabels_WhenTryAddSecurityLabels_SecurityLabelsShouldBeAddedWithoutRemovingOriginalOnes() { var resource = new Patient() { Meta = new Meta() { Security = new List <Coding>() { new Coding() { Code = "MASKED" } } } }; var result = new ProcessResult(); result.AddProcessRecord(AnonymizationOperations.Redact, ElementNode.ForPrimitive(1)); var resourceNode = ElementNode.FromElement(resource.ToTypedElement()); resourceNode.AddSecurityTag(result); resource = resourceNode.ToPoco <Patient>(); Assert.Equal(2, resource.Meta.Security.Count); Assert.Equal("MASKED", resource.Meta.Security[0].Code); Assert.Equal(SecurityLabels.REDACT.Code, resource.Meta.Security[1].Code); }
public void GivenAResourceWithSameSecurityLabels_WhenTryAddSecurityLabels_SecurityLabelsShouldNotBeAddedAgain() { var resource = new Patient() { Meta = new Meta() { Security = new List <Coding>() { new Coding() { Code = "REDACTED" } } } }; var result = new ProcessResult(); result.AddProcessRecord(AnonymizationOperations.Redact, ElementNode.ForPrimitive(1)); var resourceNode = ElementNode.FromElement(resource.ToTypedElement()); resourceNode.AddSecurityTag(result); resource = resourceNode.ToPoco <Patient>(); Assert.Single(resource.Meta.Security); Assert.Equal(SecurityLabels.REDACT.Code, resource.Meta.Security.First().Code); }
public void GivenAResourceWithSecurityLabels_WhenTryAddMultipleSecurityLabels_SecurityLabelsShouldBeAddedAgain() { var resource = new Patient() { Meta = new Meta() { Security = new List <Coding>() { new Coding() { Code = "REDACTED" }, new Coding() { Code = "ADDITION" } } } }; var result = new ProcessResult(); result.AddProcessRecord(AnonymizationOperations.Redact, ElementNode.ForPrimitive(1)); result.AddProcessRecord(AnonymizationOperations.Abstract, ElementNode.ForPrimitive(1)); result.AddProcessRecord(AnonymizationOperations.Perturb, ElementNode.ForPrimitive(1)); var resourceNode = ElementNode.FromElement(resource.ToTypedElement()); resourceNode.AddSecurityTag(result); resource = resourceNode.ToPoco <Patient>(); Assert.Equal(4, resource.Meta.Security.Count); Assert.Contains(SecurityLabels.REDACT.Code, resource.Meta.Security.Select(s => s.Code)); Assert.Contains(SecurityLabels.ABSTRED.Code, resource.Meta.Security.Select(s => s.Code)); Assert.Contains(SecurityLabels.PERTURBED.Code, resource.Meta.Security.Select(s => s.Code)); }
public void IsDistinctWorksOnEmptyCollections() { ITypedElement dummy = ElementNode.ForPrimitive(true); Assert.IsTrue(dummy.IsBoolean("{}.isDistinct()", true)); Assert.IsTrue(dummy.IsBoolean("(2).isDistinct()", true)); }
private ElementNode GetPrimitiveNode(string value) { var node = ElementNode.FromElement(ElementNode.ForPrimitive(value ?? string.Empty)); if (value == null) { // Set empty node value to null to ensure a correct serialization result node.Value = null; } return(node); }
public void TestUnbox() { Assert.IsNull(Typecasts.UnboxTo(emptyColl, typeof(string))); Assert.AreEqual(collection, Typecasts.UnboxTo(collection, typeof(IEnumerable <ITypedElement>))); Assert.AreEqual(complex, Typecasts.UnboxTo(singleC, typeof(ITypedElement))); Assert.AreEqual(4L, Typecasts.UnboxTo(singleV, typeof(long))); Assert.AreEqual(4L, Typecasts.UnboxTo(ElementNode.ForPrimitive(4L), typeof(long))); Assert.AreEqual(complex, Typecasts.UnboxTo(complex, typeof(ITypedElement))); Assert.IsNull(Typecasts.UnboxTo(null, typeof(string))); Assert.AreEqual(4L, Typecasts.UnboxTo(4L, typeof(long))); Assert.AreEqual("hi!", Typecasts.UnboxTo("hi!", typeof(string))); }
public void GivenAResourceWithoutSecurityLabels_WhenTryAddSecurityLabels_SecurityLabelsShouldBeAdded() { var resource = new Patient(); var result = new ProcessResult(); result.AddProcessRecord(AnonymizationOperations.Redact, ElementNode.ForPrimitive(1)); var resourceNode = ElementNode.FromElement(resource.ToTypedElement()); resourceNode.AddSecurityTag(result); resource = resourceNode.ToPoco <Patient>(); Assert.Single(resource.Meta.Security); Assert.Equal(SecurityLabels.REDACT.Code, resource.Meta.Security.First().Code); }
public void CreateFromPrimitive() { var node = ElementNode.ForPrimitive("hi!"); Assert.AreEqual("hi!", node.Value); Assert.AreEqual("System.String", node.InstanceType); // should really be System.String I think. node = ElementNode.ForPrimitive(TestAdministrativeGender.Female); Assert.AreEqual("female", node.Value); Assert.AreEqual("System.Code", node.InstanceType); node = ElementNode.ForPrimitive(AdHoc.Now); Assert.AreEqual("Now", node.Value); Assert.AreEqual("System.Code", node.InstanceType); }
public void ConvertToString() { var inputs = create("hoi", 3, 4L, 3.4m, true, false, P.Time.Parse("15:47:00+01:00"), P.DateTime.Parse("2019-01-11T15:47:00+01:00"), P.Date.Parse("1972-11-30"), new P.Quantity(4m, "kg")); var vals = new[] { "hoi", "3", "4", "3.4", "true", "false", "15:47:00+01:00", "2019-01-11T15:47:00+01:00", "1972-11-30", "4 'kg'" }; inputs.Zip(vals, (i, v) => (i, v)) .ToList() .ForEach(c => Assert.AreEqual(c.v, c.i.ToStringRepresentation())); inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToString())); Assert.AreEqual("true", ElementNode.ForPrimitive(true).Scalar("true.toString()")); Assert.IsNull(ElementNode.ForPrimitive(true).Scalar("{}.toString()")); }
public void ConvertToDecimal() { var inputs = create(1, 1L, 2m, "2", "3.14", -4.4m, true, false); var vals = new[] { 1m, 1m, 2m, 2m, 3.14m, -4.4m, 1m, 0m }; inputs.Zip(vals, (i, v) => (i, v)) .ToList() .ForEach(c => Assert.AreEqual(c.i.ToDecimal(), c.v)); inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToDecimal())); var wrong = create("hi", "++6", "2,6", "no", "false", DateTimeOffset.Now); wrong.ForEach(c => Assert.IsNull(c.ToDecimal())); wrong.ForEach(c => Assert.IsFalse(c.ConvertsToDecimal())); Assert.AreEqual(3.14m, ElementNode.ForPrimitive(true).Scalar("'3.14'.toDecimal()")); Assert.IsNull(ElementNode.ForPrimitive(true).Scalar("{}.toDecimal()")); }
public void ConvertToLong() { var inputs = create(1, "2", -4, "-5", "+4", 1234L, true, false); var vals = new[] { 1L, 2L, -4L, -5L, 4L, 1234L, 1L, 0L }; inputs.Zip(vals, (i, v) => (i, v)) .ToList() .ForEach(c => Assert.AreEqual(c.i.ToLong(), c.v)); inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToLong())); var wrong = create("2.4", "++6", "2,6", "no", "false", DateTimeOffset.Now); wrong.ForEach(c => Assert.IsNull(c.ToLong())); wrong.ForEach(c => Assert.IsFalse(c.ConvertsToLong())); Assert.AreEqual(2L, ElementNode.ForPrimitive(true).Scalar("'2'.toLong()")); Assert.IsNull(ElementNode.ForPrimitive(true).Scalar("{}.toLong()")); }
public void GivenAResourceWithVersionId_WhenTryAddSecurityLabels_VersionIdShouldBeKept() { var resource = new Patient() { Meta = new Meta() { VersionId = "Test" } }; var result = new ProcessResult(); result.AddProcessRecord(AnonymizationOperations.Redact, ElementNode.ForPrimitive(1)); var resourceNode = ElementNode.FromElement(resource.ToTypedElement()); resourceNode.AddSecurityTag(result); resource = resourceNode.ToPoco <Patient>(); Assert.Equal("Test", resource.Meta.VersionId); }
public void ConvertToTime() { var now = P.Time.Parse("15:47:00+01:00"); var inputs = create(now, "12:05:45"); var vals = new[] { now, P.Time.Parse("12:05:45") }; inputs.Zip(vals, (i, v) => (i, v)) .ToList() .ForEach(c => Assert.AreEqual(c.i.ToTime(), c.v)); inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToTime())); var wrong = create(new DateTimeOffset(2019, 1, 11, 15, 47, 00, new TimeSpan(1, 0, 0)), "hi", 2.6m, false); wrong.ForEach(c => Assert.IsNull(c.ToTime())); wrong.ForEach(c => Assert.IsFalse(c.ConvertsToTime())); Assert.AreEqual(P.Time.Parse("15:00:01"), ElementNode.ForPrimitive(true).Scalar("'15:00:01'.toTime()")); Assert.IsNull(ElementNode.ForPrimitive(true).Scalar("{}.toTime()")); }
public void ConvertToBoolean() { var areTrue = create(true, "TruE", "Yes", "y", "t", "1", "1.0", 1, 1L, 1m, 1.0m); areTrue.ForEach(o => Assert.IsTrue(o.ToBoolean().Value)); areTrue.ForEach(o => Assert.IsTrue(o.ConvertsToBoolean())); var areFalse = create(false, "fAlse", "nO", "N", "f", "0", "0.0", 0, 0L, 0m, 0.0m); areFalse.ForEach(o => Assert.IsFalse(o.ToBoolean().Value)); areFalse.ForEach(o => Assert.IsTrue(o.ConvertsToBoolean())); var wrong = create("truex", "falsx", "not", "tr", -8, -1L, 2L, 2.0m, 1.1m); wrong.ForEach(o => Assert.IsNull(o.ToBoolean())); wrong.ForEach(o => Assert.IsFalse(o.ConvertsToBoolean())); Assert.AreEqual(true, ElementNode.ForPrimitive(true).Scalar("'TruE'.toBoolean()")); Assert.IsNull(ElementNode.ForPrimitive(true).Scalar("{}.toBoolean()")); }
public void ConvertToQuantity() { var inputs = create(5, 75L, 75.6m, "30 'wk'", false, true, new P.Quantity(80.0m, "kg")); var vals = new[] { new P.Quantity(5m), new P.Quantity(75m), new P.Quantity(75.6m, P.Quantity.UCUM_UNIT), new P.Quantity(30m, "wk"), new P.Quantity(0.0m), new P.Quantity(1.0m), new P.Quantity(80m, "kg") }; inputs.Zip(vals, (i, v) => (i, v)) .ToList() .ForEach(c => Assert.AreEqual(c.v, c.i.ToQuantity())); inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToQuantity())); var wrong = create("hi", "++6", "2,6", "no", "false", DateTimeOffset.Now); wrong.ForEach(c => Assert.IsNull(c.ToQuantity())); wrong.ForEach(c => Assert.IsFalse(c.ConvertsToQuantity())); Assert.AreEqual(new P.Quantity(3m, "wk"), ElementNode.ForPrimitive(true).Scalar("'3 \\'wk\\''.toQuantity()")); Assert.IsNull(ElementNode.ForPrimitive(true).Scalar("{}.toQuantity()")); }
public void ConvertToDate() { var then = P.DateTime.Parse("2019-01-11T15:47:00+01:00"); var then2 = P.DateTime.Parse("2019-01-11T15:47:00"); var bd = P.Date.Parse("1972-11-30"); var inputs = create("1972-11", bd, then, then2); var vals = new[] { P.Date.Parse("1972-11"), bd, P.Date.Parse("2019-01-11+01:00"), P.Date.Parse("2019-01-11") }; inputs.Zip(vals, (i, v) => (i, v)) .ToList() .ForEach(c => Assert.AreEqual(c.i.ToDate(), c.v)); inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToDate())); var wrong = create("hi", 2.6m, false, P.Time.Parse("16:05:49")); wrong.ForEach(c => Assert.IsNull(c.ToDate())); wrong.ForEach(c => Assert.IsFalse(c.ConvertsToDate())); Assert.AreEqual(bd, ElementNode.ForPrimitive(true).Scalar("'1972-11-30'.toDate()")); Assert.IsNull(ElementNode.ForPrimitive(true).Scalar("{}.toDate()")); }
public void TestStringSplit() { ITypedElement dummy = ElementNode.ForPrimitive("a,b,c,d"); var result = dummy.Select("split(',')"); Assert.IsNotNull(result); CollectionAssert.AreEqual(new[] { "a", "b", "c", "d" }, result.Select(r => r.Value.ToString()).ToArray()); dummy = ElementNode.ForPrimitive("a,,b,c,d"); // Empty element should be removed result = dummy.Select("split(',')"); Assert.IsNotNull(result); CollectionAssert.AreEqual(new[] { "a", "b", "c", "d" }, result.Select(r => r.Value.ToString()).ToArray()); dummy = ElementNode.ForPrimitive(""); result = dummy.Select("split(',')"); Assert.IsNotNull(result); dummy = ElementNode.ForPrimitive("[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]"); result = dummy.Select("split('[stop]')"); Assert.IsNotNull(result); CollectionAssert.AreEqual(new[] { "ONE", "TWO", "THREE" }, result.Select(r => r.Value.ToString()).ToArray()); }
public void StringConcatenationAndEmpty() { ITypedElement dummy = ElementNode.ForPrimitive(true); Assert.AreEqual("ABCDEF", dummy.Scalar("'ABC' + '' + 'DEF'")); Assert.AreEqual("DEF", dummy.Scalar("'' + 'DEF'")); Assert.AreEqual("DEF", dummy.Scalar("'DEF' + ''")); Assert.IsNull(dummy.Scalar("{} + 'DEF'")); Assert.IsNull(dummy.Scalar("'ABC' + {} + 'DEF'")); Assert.IsNull(dummy.Scalar("'ABC' + {}")); Assert.AreEqual("ABCDEF", dummy.Scalar("'ABC' & '' & 'DEF'")); Assert.AreEqual("DEF", dummy.Scalar("'' & 'DEF'")); Assert.AreEqual("DEF", dummy.Scalar("'DEF' & ''")); Assert.AreEqual("DEF", dummy.Scalar("{} & 'DEF'")); Assert.AreEqual("ABCDEF", dummy.Scalar("'ABC' & {} & 'DEF'")); Assert.AreEqual("ABC", dummy.Scalar("'ABC' & {}")); Assert.IsNull(dummy.Scalar("'ABC' & {} & 'DEF' + {}")); }
public void Intersect() { var a = ElementNode.ForPrimitive("A"); var b1 = ElementNode.ForPrimitive("B"); var c = ElementNode.ForPrimitive("C"); var b2 = ElementNode.ForPrimitive("B"); var col1 = new ITypedElement[] { a, b1 }; var col2 = new ITypedElement[] { c, b2 }; var col3 = new ITypedElement[] { c }; var result = col1.Intersect(col2); Assert.IsNotNull(result); Assert.AreEqual("B", result.First().Value); result = col2.Intersect(col1); Assert.IsNotNull(result); Assert.AreEqual("B", result.First().Value); result = col1.Intersect(col3); Assert.IsNotNull(result); Assert.IsFalse(result.Any()); }
public static ITypedElement FpIndexOf(this string me, string fragment) { return(ElementNode.ForPrimitive(me.IndexOf(fragment))); }
public override Invokee VisitConstant(FP.ConstantExpression expression, SymbolTable scope) { return(InvokeeFactory.Return(ElementNode.ForPrimitive(expression.Value))); }
private static ITypedElement any2primitiveTypedElement(object source) => ElementNode.ForPrimitive(source);
private static object scalar(string expr) { ITypedElement dummy = ElementNode.ForPrimitive(true); return(dummy.Scalar(expr)); }
private static void isB(string expr, object value = null) { ITypedElement dummy = ElementNode.ForPrimitive(value ?? true); Assert.IsTrue(dummy.IsBoolean(expr, true)); }
public static void AddVar(this SymbolTable table, string name, object value) { table.AddVar(name, ElementNode.ForPrimitive(value)); }
public static IEnumerable <ITypedElement> FpSplit(this string me, string seperator) { var results = me.Split(new[] { seperator }, StringSplitOptions.RemoveEmptyEntries); return(results.Select(s => ElementNode.ForPrimitive(s))); }
private static ITypedElement any2ValueProvider(object source) => ElementNode.ForPrimitive(source);
public static IEnumerable <ITypedElement> ToChars(this string me) => me.ToCharArray().Select(c => ElementNode.ForPrimitive(c));