예제 #1
0
        public void ToDateTimeOffset()
        {
            var plusOne = new TimeSpan(1, 0, 0);
            var plusTwo = new TimeSpan(2, 0, 0);

            var pt  = PartialTime.Parse("13:45:56");
            var dto = pt.ToDateTimeOffset(2019, 7, 23, plusOne);

            Assert.AreEqual(2019, dto.Year);
            Assert.AreEqual(7, dto.Month);
            Assert.AreEqual(23, dto.Day);
            Assert.AreEqual(13, dto.Hour);
            Assert.AreEqual(45, dto.Minute);
            Assert.AreEqual(56, dto.Second);
            Assert.AreEqual(plusOne, dto.Offset);

            pt  = PartialTime.Parse("13:45:56.456+02:00");
            dto = pt.ToDateTimeOffset(2019, 7, 23, plusOne);
            Assert.AreEqual(13, dto.Hour);
            Assert.AreEqual(45, dto.Minute);
            Assert.AreEqual(56, dto.Second);
            Assert.AreEqual(456, dto.Millisecond);
            Assert.AreEqual(plusTwo, dto.Offset);

            pt  = PartialTime.Parse("13+02:00");
            dto = pt.ToDateTimeOffset(2019, 7, 23, plusOne);
            Assert.AreEqual(13, dto.Hour);
            Assert.AreEqual(0, dto.Minute);
            Assert.AreEqual(0, dto.Second);
            Assert.AreEqual(plusTwo, dto.Offset);
        }
예제 #2
0
 private static string convertToXmlString(object value)
 {
     return(value switch
     {
         bool bl => XmlConvert.ToString(bl),
         Byte by => XmlConvert.ToString(by),               // Not used in FHIR serialization
         Char cr => XmlConvert.ToString(cr),               // Not used in FHIR serialization
         DateTime dt => XmlConvert.ToString(dt, FMT_FULL), // Obsolete: use DateTimeOffset instead!!
         decimal dec => XmlConvert.ToString(dec),
         Double dbl => XmlConvert.ToString(dbl),
         Int16 i16 => XmlConvert.ToString(i16),
         Int32 i32 => XmlConvert.ToString(i32),
         Int64 i64 => XmlConvert.ToString(i64),        // Not used in FHIR serialization
         SByte sb => XmlConvert.ToString(sb),          // Not used in FHIR serialization
         Single sing => XmlConvert.ToString(sing),     // Not used in FHIR serialization
         UInt16 uint16 => XmlConvert.ToString(uint16), // Not used in FHIR serialization
         UInt32 uint32 => XmlConvert.ToString(uint32), // Not used in FHIR serialization
         UInt64 uint64 => XmlConvert.ToString(uint64), // Not used in FHIR serialization
         byte[] barr => System.Convert.ToBase64String(barr),
         DateTimeOffset dto => XmlConvert.ToString(dto, FMT_FULL),
         Uri uri => uri.ToString(),
         PartialDateTime pdt => pdt.ToString(),
         PartialTime pt => pt.ToString(),
         PartialDate pd => pd.ToString(),
         Enum en => en.GetLiteral(),
         BigInteger bi => bi.ToString(),
         Quantity q => q.ToString(),
         _ => throw Error.NotSupported($"Cannot convert '{value.GetType().Name}' value '{value}' to string"),
     });
        /// <summary>
        /// FhirPath toTime() function.
        /// </summary>
        /// <param name="focus"></param>
        /// <returns></returns>
        public static PartialTime?ToTime(this ITypedElement focus)
        {
            var val = focus?.Value;

            if (val == null)
            {
                return(null);
            }

            switch (val)
            {
            case PartialTime pt:
                return(pt);

            case string s:
                return(convertString(s));

            default:
                return(null);
            }

            PartialTime?convertString(string si)
            {
                // Inconsistenty, the format for a time requires the 'T' prefix, while
                // convertsToDateTime() does not expect a '@'.
                if (!si.StartsWith("T"))
                {
                    return(null);
                }

                return(PartialTime.TryParse(si.Substring(1), out var result) ?
                       result : (PartialTime?)null);
            }
        }
예제 #4
0
        public void FromDateTimeOffset()
        {
            var plusOne = new TimeSpan(1, 0, 0);

            var dto = new DateTimeOffset(2019, 7, 23, 13, 45, 56, 567, plusOne);
            var pt  = PartialTime.FromDateTimeOffset(dto);

            Assert.AreEqual(13, pt.Hours);
            Assert.AreEqual(45, pt.Minutes);
            Assert.AreEqual(56, pt.Seconds);
            Assert.AreEqual(567, pt.Millis);
            Assert.IsNull(pt.Offset);

            pt = PartialTime.FromDateTimeOffset(dto, includeOffset: true);
            Assert.AreEqual(13, pt.Hours);
            Assert.AreEqual(45, pt.Minutes);
            Assert.AreEqual(56, pt.Seconds);
            Assert.AreEqual(567, pt.Millis);
            Assert.AreEqual(plusOne, pt.Offset);

            pt = PartialTime.FromDateTimeOffset(dto, prec: PartialPrecision.Hour, includeOffset: true);
            Assert.AreEqual(13, pt.Hours);
            Assert.IsNull(pt.Minutes);
            Assert.AreEqual(PartialPrecision.Hour, pt.Precision);
        }
예제 #5
0
 public void TimeComparison()
 {
     Assert.IsTrue(PartialTime.Parse("13:00:00Z") > PartialTime.Parse("12:00:00Z"));
     Assert.IsTrue(PartialTime.Parse("13:00:00Z") < PartialTime.Parse("18:00:00+02:00"));
     Assert.IsTrue(PartialTime.Parse("12:34:00+00:00") > PartialTime.Parse("12:33:55+00:00"));
     Assert.IsTrue(PartialTime.Parse("13:00:00+00:00") < PartialTime.Parse("15:01:00+02:00"));
     Assert.IsTrue(PartialTime.Parse("13:00:00+00:00") > PartialTime.Parse("14:59:00+02:00"));
 }
예제 #6
0
 public void CheckOrdering()
 {
     Assert.AreEqual(1, PartialTime.Parse("13:00:00Z").CompareTo(PartialTime.Parse("12:00:00Z")));
     Assert.AreEqual(-1, PartialTime.Parse("13:00:00Z").CompareTo(PartialTime.Parse("18:00:00+02:00")));
     Assert.AreEqual(1, PartialTime.Parse("12:34:00+00:00").CompareTo(PartialTime.Parse("12:33:55+00:00")));
     Assert.AreEqual(-1, PartialTime.Parse("13:00:00+00:00").CompareTo(PartialTime.Parse("15:01:00+02:00")));
     Assert.AreEqual(0, PartialTime.Parse("13:45:02+01:00").CompareTo(PartialTime.Parse("13:45:02+01:00")));
 }
예제 #7
0
        public void TimeComparison()
        {
            Assert.True(PartialDateTime.Parse("2012-03-04T13:00:00Z") > PartialDateTime.Parse("2012-03-04T12:00:00Z"));
            Assert.True(PartialDateTime.Parse("2012-03-04T13:00:00Z") < PartialDateTime.Parse("2012-03-04T18:00:00+02:00"));

            Assert.True(PartialTime.Parse("12:34:00+00:00") > PartialTime.Parse("12:33:55+00:00"));
            Assert.True(PartialTime.Parse("13:00:00+00:00") < PartialTime.Parse("15:01:00+02:00"));
            Assert.True(PartialTime.Parse("13:00:00+00:00") > PartialTime.Parse("14:59:00+02:00"));
        }
예제 #8
0
        public void CheckOrdering()
        {
            Assert.Equal(1, PartialDateTime.Parse("2012-03-04T13:00:00Z").CompareTo(PartialDateTime.Parse("2012-03-04T12:00:00Z")));
            Assert.Equal(-1, PartialDateTime.Parse("2012-03-04T13:00:00Z").CompareTo(PartialDateTime.Parse("2012-03-04T18:00:00+02:00")));
            Assert.Equal(0, PartialDateTime.Parse("2015-01-01").CompareTo(PartialDateTime.Parse("2015-01-01")));

            Assert.Equal(1, PartialTime.Parse("12:34:00+00:00").CompareTo(PartialTime.Parse("12:33:55+00:00")));
            Assert.Equal(-1, PartialTime.Parse("13:00:00+00:00").CompareTo(PartialTime.Parse("15:01:00+02:00")));
            Assert.Equal(0, PartialTime.Parse("13:45:02+01:00").CompareTo(PartialTime.Parse("13:45:02+01:00")));
        }
예제 #9
0
        public void ConvertToString()
        {
            var inputs = ElementNode.CreateList("hoi", 4L, 3.4m, true, false, PartialTime.Parse("15:47:00+01:00"),
                                                PartialDateTime.Parse("2019-01-11T15:47:00+01:00"));
            var vals = new[] { "hoi", "4", "3.4", "true", "false", "15:47:00+01:00", "2019-01-11T15:47:00+01:00" };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.v, c.i.ToString()));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToString()));
        }
예제 #10
0
        public void TimeConstructor()
        {
            PartialTime.Parse("12:34:44+02:00");
            PartialTime.Parse("12:34:44");
            PartialTime.Parse("12:34:44Z");

            Assert.True(PartialTime.TryParse("12:34:44Z", out PartialTime pd));
            Assert.Equal(pd, PartialTime.Parse("12:34:44Z"));
            Assert.Equal("12:34:44Z", pd.ToString());

            Assert.False(PartialTime.TryParse("92:34:44Z", out pd));
        }
예제 #11
0
        public override void Draw(IDrawingToolkit tk, Area area)
        {
            if (!UpdateDrawArea(tk, area, Area))
            {
                return;
            }

            base.Draw(tk, area);

            tk.Begin();

            cancelRect = new Rectangle(
                new Point((Position.X + Width) - StyleConf.ButtonRecWidth, Position.Y),
                StyleConf.ButtonRecWidth, HeaderHeight);

            if (Active && Mode != DashboardMode.Edit)
            {
                tk.LineWidth     = StyleConf.ButtonLineWidth;
                tk.StrokeColor   = Button.BackgroundColor;
                tk.FillColor     = Button.BackgroundColor;
                tk.FontWeight    = FontWeight.Normal;
                tk.FontSize      = StyleConf.ButtonHeaderFontSize;
                tk.FontAlignment = FontAlignment.Left;
                tk.DrawText(new Point(Position.X + TextHeaderX, Position.Y),
                            Button.Width - TextHeaderX, iconImage.Height, Button.Timer.Name);
                tk.FontWeight    = FontWeight.Bold;
                tk.FontSize      = StyleConf.ButtonTimerFontSize;
                tk.FontAlignment = FontAlignment.Center;
                tk.DrawText(new Point(Position.X, Position.Y + iconImage.Height),
                            Button.Width, Button.Height - iconImage.Height,
                            PartialTime.ToSecondsString(), false, true);

                tk.FillColor = tk.StrokeColor = BackgroundColor;
                tk.DrawRectangle(cancelRect.TopLeft, cancelRect.Width, cancelRect.Height);
                tk.StrokeColor = TextColor;
                tk.FillColor   = TextColor;
                tk.DrawImage(new Point(cancelRect.TopLeft.X, cancelRect.TopLeft.Y + 5),
                             cancelRect.Width, cancelRect.Height - 10, cancelImage, true, true);
            }
            else
            {
                Text = Button.Name;
                DrawText(tk);
                Text = null;
            }

            if (TeamImage != null)
            {
                tk.DrawImage(new Point(Position.X + Width - 40, Position.Y + 5), 40,
                             iconImage.Height, TeamImage, true);
            }
            tk.End();
        }
예제 #12
0
        public void TimeEquality()
        {
            Assert.True(PartialDateTime.Parse("2015-01-01") == PartialDateTime.Parse("2015-01-01"));
            Assert.True(PartialDateTime.Parse("2015-01-01") != PartialDateTime.Parse("2015-01"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+02:00") == PartialDateTime.Parse("2015-01-01T13:40:50+02:00"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+00:00") == PartialDateTime.Parse("2015-01-01T13:40:50Z"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+00:10") != PartialDateTime.Parse("2015-01-01T13:40:50Z"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+00:10") != PartialDateTime.Parse("2015-01-01"));

            Assert.True(PartialTime.Parse("13:45:02Z") == PartialTime.Parse("13:45:02+00:00"));
            Assert.True(PartialTime.Parse("13:45:02+01:00") == PartialTime.Parse("13:45:02+01:00"));
            Assert.True(PartialTime.Parse("13:45:02+00:00") != PartialTime.Parse("13:45:02+01:00"));
        }
예제 #13
0
        public void GetNow()
        {
            var now  = PartialTime.Now();
            var now2 = DateTimeOffset.Now;

            Assert.AreEqual(now2.Hour, now.Hours);
            Assert.AreEqual(now2.Minute, now.Minutes);
            // Assert.AreEqual(now2.Second, now.Seconds);  // well, maybe not to avoid random CI build/test failures
            Assert.AreEqual(PartialPrecision.Fraction, now.Precision);
            Assert.IsFalse(now.HasOffset);

            now = PartialTime.Now(includeOffset: true);
            Assert.IsTrue(now.HasOffset);
        }
예제 #14
0
        public void FhirPath_Gramm_Literal()
        {
            var parser = Grammar.Literal.End();

            AssertParser.SucceedsMatch(parser, "'hi there'", new ConstantExpression("hi there"));
            AssertParser.SucceedsMatch(parser, "3", new ConstantExpression(3L));
            AssertParser.SucceedsMatch(parser, "3.14", new ConstantExpression(3.14m));
            AssertParser.SucceedsMatch(parser, "@2013-12", new ConstantExpression(PartialDateTime.Parse("2013-12")));
            AssertParser.SucceedsMatch(parser, "@T12:23:34Z", new ConstantExpression(PartialTime.Parse("12:23:34Z")));
            AssertParser.SucceedsMatch(parser, "true", new ConstantExpression(true));
            AssertParser.SucceedsMatch(parser, "@2014-12-13T12:00:00+02:00", new ConstantExpression(PartialDateTime.Parse("2014-12-13T12:00:00+02:00")));

            AssertParser.FailsMatch(parser, "%constant");
            AssertParser.FailsMatch(parser, "\"quotedstring\"");
            AssertParser.FailsMatch(parser, "A23identifier");
        }
예제 #15
0
        public void ConvertToDateTime()
        {
            var now    = PartialDateTime.Parse("2019-01-11T15:47:00+01:00");
            var inputs = ElementNode.CreateList(new DateTimeOffset(2019, 1, 11, 15, 47, 00, new TimeSpan(1, 0, 0)),
                                                "2019-01", "2019-01-11T15:47:00+01:00");
            var vals = new[] { now, PartialDateTime.Parse("2019-01"), now };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.i.ToDateTime(), c.v));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToDateTime()));

            var wrong = ElementNode.CreateList("hi", 2.6m, false, PartialTime.Parse("16:05:49")).ToList();

            wrong.ForEach(c => Assert.IsNull(c.ToDateTime()));
            wrong.ForEach(c => Assert.IsFalse(c.ConvertsToDateTime()));
        }
예제 #16
0
        public void TimeConstructor()
        {
            accept("12:34:44.123456+02:00", 12, 34, 44, 123, new TimeSpan(2, 0, 0));
            accept("12:34:44.1+02:00", 12, 34, 44, 100, new TimeSpan(2, 0, 0));
            accept("12:34:44+02:00", 12, 34, 44, null, new TimeSpan(2, 0, 0));
            accept("12:34:44Z", 12, 34, 44, null, TimeSpan.Zero);
            accept("12:34:44+00:00", 12, 34, 44, null, TimeSpan.Zero);
            accept("12:34:44", 12, 34, 44, null, null);

            accept("12:34Z", 12, 34, null, null, TimeSpan.Zero);
            accept("12:34", 12, 34, null, null, null);
            accept("12", 12, null, null, null, null);
            accept("12Z", 12, null, null, null, TimeSpan.Zero);
            accept("12-04:30", 12, null, null, null, new TimeSpan(-4, -30, 0));

            reject("");
            reject("+05:00");
            reject("Z");
            reject("12:34.1234");
            reject("Hi12:34:44");
            reject("12:34:44there");
            reject("12:34:44+A");
            reject("12:34:44+345:432");
            reject("92:34:44");
            reject("12:34:AM");

            void accept(string testValue, int?h, int?m, int?s, int?ms, TimeSpan?o)
            {
                Assert.IsTrue(PartialTime.TryParse(testValue, out PartialTime parsed), "TryParse");
                Assert.AreEqual(h, parsed.Hours, "hours");
                Assert.AreEqual(m, parsed.Minutes, "minutes");
                Assert.AreEqual(s, parsed.Seconds, "seconds");
                Assert.AreEqual(ms, parsed.Millis, "millis");
                Assert.AreEqual(o, parsed.Offset, "offset");
                Assert.AreEqual(testValue, parsed.ToString(), "ToString");
            }

            void reject(string testValue)
            {
                Assert.IsFalse(PartialTime.TryParse(testValue, out _));
            }
        }
예제 #17
0
        public void FhirPath_Lex_Time()
        {
            var parser = Lexer.Time.End();

            accept("@T12:34:34.345674");
            accept("@T12:34:34");
            accept("@T12:35");
            accept("@T12");

            reject("@T12:34:34+02:30");
            reject("@T12:34:00Z");
            reject("2001-01-01T12:34:34+02:30");
            reject("@2001-01-01T12:34:34+02:30");
            reject("T12:34:34+02:30");
            reject("12:34:34+02:30");
            reject("@12:34:34+02:30");
            reject("@T12:34:34+48:30");

            void accept(string s) => AssertParser.SucceedsMatch(parser, s, PartialTime.Parse(s.Substring(2)));
            void reject(string s) => AssertParser.FailsMatch(parser, s);
        }
        private static object convertXmlStringToPrimitive(Type to, string value)
        {
            if (typeof(Boolean) == to)
            {
                return(XmlConvert.ToBoolean(value));
            }
            if (typeof(Byte) == to)
            {
                return(XmlConvert.ToByte(value));        // Not used in FHIR serialization
            }
            if (typeof(Char) == to)
            {
                return(XmlConvert.ToChar(value));        // Not used in FHIR serialization
            }
            if (typeof(DateTime) == to)
            {
                return(ConvertToDatetimeOffset(value).UtcDateTime);  // Obsolete: use DateTimeOffset instead!!
            }
            if (typeof(Decimal) == to)
            {
                return(XmlConvert.ToDecimal(value));
            }
            if (typeof(Double) == to)
            {
                return(XmlConvert.ToDouble(value));      // Could lead to loss in precision
            }
            if (typeof(Int16) == to)
            {
                return(XmlConvert.ToInt16(value));       // Could lead to loss in precision
            }
            if (typeof(Int32) == to)
            {
                return(XmlConvert.ToInt32(value));
            }
            if (typeof(Int64) == to)
            {
                return(XmlConvert.ToInt64(value));       // Not used in FHIR serialization
            }
            if (typeof(SByte) == to)
            {
                return(XmlConvert.ToSByte(value));       // Not used in FHIR serialization
            }
            if (typeof(Single) == to)
            {
                return(XmlConvert.ToSingle(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt16) == to)
            {
                return(XmlConvert.ToUInt16(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt32) == to)
            {
                return(XmlConvert.ToUInt32(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt64) == to)
            {
                return(XmlConvert.ToUInt64(value));      // Not used in FHIR serialization
            }
            if (typeof(byte[]) == to)
            {
                return(System.Convert.FromBase64String(value));
            }
            if (typeof(DateTimeOffset) == to)
            {
                return(ConvertToDatetimeOffset(value));
            }
            if (typeof(System.Uri) == to)
            {
                return(new Uri(value, UriKind.RelativeOrAbsolute));
            }
            if (typeof(PartialDateTime) == to)
            {
                return(PartialDateTime.Parse(value));
            }
            if (typeof(PartialTime) == to)
            {
                return(PartialTime.Parse(value));
            }
            if (typeof(BigInteger) == to)
            {
                return(BigInteger.Parse(value));
            }
            if (to.IsEnum())
            {
                var result = EnumUtility.ParseLiteral(value, to);
                if (result == null)
                {
                    throw Error.NotSupported($"String value '{value}' is not a known literal in enum '{to.Name}'");
                }

                return(result);
            }

            throw Error.NotSupported($"Cannot convert string value '{value}' to a {to.Name}");
        }
        public static SymbolTable AddStandardFP(this SymbolTable t)
        {
            // Functions that operate on the focus, without null propagation
            t.Add("empty", (IEnumerable <object> f) => !f.Any());
            t.Add("exists", (IEnumerable <object> f) => f.Any());
            t.Add("count", (IEnumerable <object> f) => f.Count());
            t.Add("trace", (IEnumerable <ITypedElement> f, string name, EvaluationContext ctx)
                  => f.Trace(name, ctx));

            t.Add("allTrue", (IEnumerable <ITypedElement> f) => f.All(e => e.Value as bool? == true));
            t.Add("combine", (IEnumerable <ITypedElement> l, IEnumerable <ITypedElement> r) => l.Concat(r));
            t.Add("binary.|", (object f, IEnumerable <ITypedElement> l, IEnumerable <ITypedElement> r) => l.DistinctUnion(r));
            t.Add("union", (IEnumerable <ITypedElement> l, IEnumerable <ITypedElement> r) => l.DistinctUnion(r));
            t.Add("binary.contains", (object f, IEnumerable <ITypedElement> a, ITypedElement b) => a.Contains(b));
            t.Add("binary.in", (object f, ITypedElement a, IEnumerable <ITypedElement> b) => b.Contains(a));
            t.Add("distinct", (IEnumerable <ITypedElement> f) => f.Distinct());
            t.Add("isDistinct", (IEnumerable <ITypedElement> f) => f.IsDistinct());
            t.Add("subsetOf", (IEnumerable <ITypedElement> f, IEnumerable <ITypedElement> a) => f.SubsetOf(a));
            t.Add("supersetOf", (IEnumerable <ITypedElement> f, IEnumerable <ITypedElement> a) => a.SubsetOf(f));
            t.Add("intersect", (IEnumerable <ITypedElement> f, IEnumerable <ITypedElement> a) => f.Intersect(a));
            t.Add("exclude", (IEnumerable <ITypedElement> f, IEnumerable <ITypedElement> a) => f.Exclude(a));

            t.Add("today", (object f) => PartialDate.Today());
            t.Add("now", (object f) => PartialDateTime.Now());
            t.Add("timeOfDay", (object f) => PartialTime.Now());

            t.Add("binary.&", (object f, string a, string b) => (a ?? "") + (b ?? ""));

            t.Add("iif", (IEnumerable <ITypedElement> f, bool?condition, IEnumerable <ITypedElement> result) => f.IIf(condition, result));
            t.Add("iif", (IEnumerable <ITypedElement> f, bool?condition, IEnumerable <ITypedElement> result, IEnumerable <ITypedElement> otherwise) => f.IIf(condition, result, otherwise));

            // Functions that use normal null propagation and work with the focus (buy may ignore it)
            t.Add("not", (IEnumerable <ITypedElement> f) => f.Not(), doNullProp: true);
            t.Add("builtin.children", (IEnumerable <ITypedElement> f, string a) => f.Navigate(a), doNullProp: true);

            t.Add("children", (IEnumerable <ITypedElement> f) => f.Children(), doNullProp: true);
            t.Add("descendants", (IEnumerable <ITypedElement> f) => f.Descendants(), doNullProp: true);

            t.Add("binary.=", (object f, IEnumerable <ITypedElement> a, IEnumerable <ITypedElement> b) => a.IsEqualTo(b), doNullProp: true);
            t.Add("binary.!=", (object f, IEnumerable <ITypedElement> a, IEnumerable <ITypedElement> b) => !a.IsEqualTo(b), doNullProp: true);
            t.Add("binary.~", (object f, IEnumerable <ITypedElement> a, IEnumerable <ITypedElement> b) => a.IsEquivalentTo(b), doNullProp: true);
            t.Add("binary.!~", (object f, IEnumerable <ITypedElement> a, IEnumerable <ITypedElement> b) => !a.IsEquivalentTo(b), doNullProp: true);

            t.Add("unary.-", (object f, long a) => - a, doNullProp: true);
            t.Add("unary.-", (object f, decimal a) => - a, doNullProp: true);
            t.Add("unary.+", (object f, long a) => a, doNullProp: true);
            t.Add("unary.+", (object f, decimal a) => a, doNullProp: true);

            t.Add("binary.*", (object f, long a, long b) => a * b, doNullProp: true);
            t.Add("binary.*", (object f, decimal a, decimal b) => a * b, doNullProp: true);
            t.Add("binary.*", (object f, Quantity a, Quantity b) => a * b, doNullProp: true);

            t.Add("binary./", (object f, decimal a, decimal b) => a / b, doNullProp: true);
            t.Add("binary./", (object f, Quantity a, Quantity b) => a / b, doNullProp: true);

            t.Add("binary.+", (object f, long a, long b) => a + b, doNullProp: true);
            t.Add("binary.+", (object f, decimal a, decimal b) => a + b, doNullProp: true);
            t.Add("binary.+", (object f, string a, string b) => a + b, doNullProp: true);
            t.Add("binary.+", (object f, Quantity a, Quantity b) => a + b, doNullProp: true);

            t.Add("binary.-", (object f, long a, long b) => a - b, doNullProp: true);
            t.Add("binary.-", (object f, decimal a, decimal b) => a - b, doNullProp: true);
            t.Add("binary.-", (object f, Quantity a, Quantity b) => a - b, doNullProp: true);

            t.Add("binary.div", (object f, long a, long b) => a / b, doNullProp: true);
            t.Add("binary.div", (object f, decimal a, decimal b) => (long)Math.Truncate(a / b), doNullProp: true);

            t.Add("binary.mod", (object f, long a, long b) => a % b, doNullProp: true);
            t.Add("binary.mod", (object f, decimal a, decimal b) => a % b, doNullProp: true);

            t.Add("binary.>", (object f, long a, long b) => a > b, doNullProp: true);
            t.Add("binary.>", (object f, decimal a, decimal b) => a > b, doNullProp: true);
            t.Add("binary.>", (object f, string a, string b) => string.CompareOrdinal(a, b) > 0, doNullProp: true);
            t.Add("binary.>", (object f, PartialDateTime a, PartialDateTime b) => a > b, doNullProp: true);
            t.Add("binary.>", (object f, PartialTime a, PartialTime b) => a > b, doNullProp: true);
            t.Add("binary.>", (object f, PartialDate a, PartialDate b) => a > b, doNullProp: true);
            t.Add("binary.>", (object f, Quantity a, Quantity b) => a > b, doNullProp: true);

            t.Add("binary.<", (object f, long a, long b) => a < b, doNullProp: true);
            t.Add("binary.<", (object f, decimal a, decimal b) => a < b, doNullProp: true);
            t.Add("binary.<", (object f, string a, string b) => string.CompareOrdinal(a, b) < 0, doNullProp: true);
            t.Add("binary.<", (object f, PartialDateTime a, PartialDateTime b) => a < b, doNullProp: true);
            t.Add("binary.<", (object f, PartialTime a, PartialTime b) => a < b, doNullProp: true);
            t.Add("binary.<", (object f, PartialDate a, PartialDate b) => a < b, doNullProp: true);
            t.Add("binary.<", (object f, Quantity a, Quantity b) => a < b, doNullProp: true);

            t.Add("binary.<=", (object f, long a, long b) => a <= b, doNullProp: true);
            t.Add("binary.<=", (object f, decimal a, decimal b) => a <= b, doNullProp: true);
            t.Add("binary.<=", (object f, string a, string b) => string.CompareOrdinal(a, b) <= 0, doNullProp: true);
            t.Add("binary.<=", (object f, PartialDateTime a, PartialDateTime b) => a <= b, doNullProp: true);
            t.Add("binary.<=", (object f, PartialTime a, PartialTime b) => a <= b, doNullProp: true);
            t.Add("binary.<=", (object f, PartialDate a, PartialDate b) => a <= b, doNullProp: true);
            t.Add("binary.<=", (object f, Quantity a, Quantity b) => a <= b, doNullProp: true);

            t.Add("binary.>=", (object f, long a, long b) => a >= b, doNullProp: true);
            t.Add("binary.>=", (object f, decimal a, decimal b) => a >= b, doNullProp: true);
            t.Add("binary.>=", (object f, string a, string b) => string.CompareOrdinal(a, b) >= 0, doNullProp: true);
            t.Add("binary.>=", (object f, PartialDateTime a, PartialDateTime b) => a >= b, doNullProp: true);
            t.Add("binary.>=", (object f, PartialTime a, PartialTime b) => a >= b, doNullProp: true);
            t.Add("binary.>=", (object f, PartialDate a, PartialDate b) => a >= b, doNullProp: true);
            t.Add("binary.>=", (object f, Quantity a, Quantity b) => a >= b, doNullProp: true);

            t.Add("single", (IEnumerable <ITypedElement> f) => f.Single(), doNullProp: true);
            t.Add("skip", (IEnumerable <ITypedElement> f, long a) => f.Skip((int)a), doNullProp: true);
            t.Add("first", (IEnumerable <ITypedElement> f) => f.First(), doNullProp: true);
            t.Add("last", (IEnumerable <ITypedElement> f) => f.Last(), doNullProp: true);
            t.Add("tail", (IEnumerable <ITypedElement> f) => f.Tail(), doNullProp: true);
            t.Add("take", (IEnumerable <ITypedElement> f, long a) => f.Take((int)a), doNullProp: true);
            t.Add("builtin.item", (IEnumerable <ITypedElement> f, long a) => f.Item((int)a), doNullProp: true);

            t.Add("toBoolean", (ITypedElement f) => f.ToBoolean(), doNullProp: true);
            t.Add("convertsToBoolean", (ITypedElement f) => f.ConvertsToBoolean(), doNullProp: true);
            t.Add("toInteger", (ITypedElement f) => f.ToInteger(), doNullProp: true);
            t.Add("convertsToInteger", (ITypedElement f) => f.ConvertsToInteger(), doNullProp: true);
            t.Add("toDecimal", (ITypedElement f) => f.ToDecimal(), doNullProp: true);
            t.Add("convertsToDecimal", (ITypedElement f) => f.ConvertsToDecimal(), doNullProp: true);
            t.Add("toDateTime", (ITypedElement f) => f.ToDateTime(), doNullProp: true);
            t.Add("convertsToDateTime", (ITypedElement f) => f.ConvertsToDateTime(), doNullProp: true);
            t.Add("toTime", (ITypedElement f) => f.ToTime(), doNullProp: true);
            t.Add("convertsToTime", (ITypedElement f) => f.ConvertsToTime(), doNullProp: true);
            t.Add("toDate", (ITypedElement f) => f.ToDate(), doNullProp: true);
            t.Add("convertsToDate", (ITypedElement f) => f.ConvertsToDate(), doNullProp: true);
            t.Add("toString", (ITypedElement f) => f.ToStringRepresentation(), doNullProp: true);
            t.Add("convertsToString", (ITypedElement f) => f.ConvertsToString(), doNullProp: true);
            t.Add("toQuantity", (ITypedElement f) => f.ToQuantity(), doNullProp: true);
            t.Add("convertsToQuantity", (ITypedElement f) => f.ConvertsToQuantity(), doNullProp: true);

            t.Add("upper", (string f) => f.ToUpper(), doNullProp: true);
            t.Add("lower", (string f) => f.ToLower(), doNullProp: true);
            t.Add("toChars", (string f) => f.ToChars(), doNullProp: true);
            t.Add("substring", (string f, long a) => f.FpSubstring((int)a), doNullProp: true);
            t.Add("substring", (string f, long a, long b) => f.FpSubstring((int)a, (int)b), doNullProp: true);
            t.Add("startsWith", (string f, string fragment) => f.StartsWith(fragment), doNullProp: true);
            t.Add("endsWith", (string f, string fragment) => f.EndsWith(fragment), doNullProp: true);
            t.Add("matches", (string f, string regex) => Regex.IsMatch(f, regex), doNullProp: true);
            t.Add("indexOf", (string f, string fragment) => f.FpIndexOf(fragment), doNullProp: true);
            t.Add("contains", (string f, string fragment) => f.Contains(fragment), doNullProp: true);
            t.Add("replaceMatches", (string f, string regex, string subst) => Regex.Replace(f, regex, subst), doNullProp: true);
            t.Add("replace", (string f, string regex, string subst) => f.FpReplace(regex, subst), doNullProp: true);
            t.Add("length", (string f) => f.Length, doNullProp: true);

            // The next two functions existed pre-normative, so we have kept them.
            t.Add("is", (ITypedElement f, string name) => f.Is(name), doNullProp: true);
            t.Add("as", (IEnumerable <ITypedElement> f, string name) => f.FilterType(name), doNullProp: true);

            t.Add("ofType", (IEnumerable <ITypedElement> f, string name) => f.FilterType(name), doNullProp: true);
            t.Add("binary.is", (object f, ITypedElement left, string name) => left.Is(name), doNullProp: true);
            t.Add("binary.as", (object f, ITypedElement left, string name) => left.CastAs(name), doNullProp: true);

            // Kept for backwards compatibility, but no longer part of the spec
            t.Add("binary.as", (object f, IEnumerable <ITypedElement> left, string name) => left.FilterType(name), doNullProp: true);

            t.Add("extension", (IEnumerable <ITypedElement> f, string url) => f.Extension(url), doNullProp: true);

            // Logic operators do not use null propagation and may do short-cut eval
            t.AddLogic("binary.and", (a, b) => a.And(b));
            t.AddLogic("binary.or", (a, b) => a.Or(b));
            t.AddLogic("binary.xor", (a, b) => a.XOr(b));
            t.AddLogic("binary.implies", (a, b) => a.Implies(b));

            // Special late-bound functions
            t.Add(new CallSignature("where", typeof(IEnumerable <ITypedElement>), typeof(object), typeof(Invokee)), runWhere);
            t.Add(new CallSignature("select", typeof(IEnumerable <ITypedElement>), typeof(object), typeof(Invokee)), runSelect);
            t.Add(new CallSignature("all", typeof(bool), typeof(object), typeof(Invokee)), runAll);
            t.Add(new CallSignature("any", typeof(bool), typeof(object), typeof(Invokee)), runAny);
            t.Add(new CallSignature("repeat", typeof(IEnumerable <ITypedElement>), typeof(object), typeof(Invokee)), runRepeat);
            t.Add(new CallSignature("trace", typeof(IEnumerable <ITypedElement>), typeof(string), typeof(object), typeof(Invokee)), Trace);

            t.AddVar("sct", "http://snomed.info/sct");
            t.AddVar("loinc", "http://loinc.org");
            t.AddVar("ucum", "http://unitsofmeasure.org");

            t.Add("builtin.coreexturl", (object f, string id) => getCoreExtensionUrl(id));
            t.Add("builtin.corevsurl", (object f, string id) => getCoreValueSetUrl(id));

            return(t);
        }
예제 #20
0
 private void SucceedsTime(Parser <PartialTime> parser, string s)
 {
     AssertParser.SucceedsMatch(parser, s, PartialTime.Parse(s.Substring(2)));
 }
        private static object convertXmlStringToPrimitive(Type to, string value)
        {
            if (typeof(Boolean) == to)
            {
                return(XmlConvert.ToBoolean(value));
            }
            if (typeof(Byte) == to)
            {
                return(XmlConvert.ToByte(value));        // Not used in FHIR serialization
            }
            if (typeof(Char) == to)
            {
                return(XmlConvert.ToChar(value));        // Not used in FHIR serialization
            }
            if (typeof(DateTime) == to)
            {
                return(ConvertToDatetimeOffset(value).UtcDateTime);  // Obsolete: use DateTimeOffset instead!!
            }
            if (typeof(Decimal) == to)
            {
                if (FORBIDDEN_DECIMAL_PREFIXES.Any(prefix => value.StartsWith(prefix)) || value.EndsWith("."))
                {
                    // decimal cannot start with '+', '-' or '00' and cannot end with '.'
                    throw new FormatException("Input string was not in a correct format.");
                }
                return(decimal.Parse(value, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture));
            }
            if (typeof(Double) == to)
            {
                return(XmlConvert.ToDouble(value));      // Could lead to loss in precision
            }
            if (typeof(Int16) == to)
            {
                return(XmlConvert.ToInt16(value));       // Could lead to loss in precision
            }
            if (typeof(Int32) == to)
            {
                return(XmlConvert.ToInt32(value));
            }
            if (typeof(Int64) == to)
            {
                return(XmlConvert.ToInt64(value));       // Not used in FHIR serialization
            }
            if (typeof(SByte) == to)
            {
                return(XmlConvert.ToSByte(value));       // Not used in FHIR serialization
            }
            if (typeof(Single) == to)
            {
                return(XmlConvert.ToSingle(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt16) == to)
            {
                return(XmlConvert.ToUInt16(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt32) == to)
            {
                return(XmlConvert.ToUInt32(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt64) == to)
            {
                return(XmlConvert.ToUInt64(value));      // Not used in FHIR serialization
            }
            if (typeof(byte[]) == to)
            {
                return(System.Convert.FromBase64String(value));
            }
            if (typeof(DateTimeOffset) == to)
            {
                return(ConvertToDatetimeOffset(value));
            }
            if (typeof(System.Uri) == to)
            {
                return(new Uri(value, UriKind.RelativeOrAbsolute));
            }
            if (typeof(PartialDateTime) == to)
            {
                return(PartialDateTime.Parse(value));
            }
            if (typeof(PartialTime) == to)
            {
                return(PartialTime.Parse(value));
            }
            if (typeof(BigInteger) == to)
            {
                return(BigInteger.Parse(value));
            }
            if (to.IsEnum())
            {
                var result = EnumUtility.ParseLiteral(value, to);
                if (result == null)
                {
                    throw Error.NotSupported($"String value '{value}' is not a known literal in enum '{to.Name}'");
                }

                return(result);
            }

            throw Error.NotSupported($"Cannot convert string value '{value}' to a {to.Name}");
        }