Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Benchmarks/DateTimeBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class DateTimeBenchmarks
public void BenchmarkSetup()
{
_dateTimeInstance = new FhirDateTime(DATETIME);
_ = _dateTimeInstance.TryToDateTime(out var _); // trigger initial compile of regex
_ = _dateTimeInstance.TryToSystemDateTime(out var _); // trigger initial compile of regex

_dateInstance = new Date(DATE);
_ = _dateInstance.TryToDate(out var _); // trigger initial compile of regex
_ = _dateInstance.TryToSystemDate(out var _); // trigger initial compile of regex
}

private const string DATETIME = "2023-07-11T13:00:00";
Expand Down Expand Up @@ -59,4 +59,4 @@ public DateTimeOffset DateToDTO_Cached()
return result;
}
}
}
}
1,508 changes: 1,321 additions & 187 deletions src/Hl7.Fhir.Base/CompatibilitySuppressions.xml

Large diffs are not rendered by default.

39 changes: 35 additions & 4 deletions src/Hl7.Fhir.Base/ElementModel/TypedElementOnSourceNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Hl7.Fhir.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using P = Hl7.Fhir.ElementModel.Types;
Expand Down Expand Up @@ -198,7 +199,7 @@ private void raiseTypeError(string message, object source, bool warning = false,

// Finally, we have a (potentially) unparsed string + type info
// parse this primitive into the desired type
if (P.Any.TryParse(sourceText, ts, out var val))
if (tryParse(sourceText, ts, out var val))
return val;
else
{
Expand All @@ -210,7 +211,7 @@ private void raiseTypeError(string message, object source, bool warning = false,
if (_settings.TruncateDateTimeToDate && ts == typeof(P.Date))
#pragma warning restore CS0618 // Type or member is obsolete
{
if (P.Any.TryParse(sourceText, typeof(P.DateTime), out var dateTimeVal))
if (tryParse(sourceText, typeof(P.DateTime), out var dateTimeVal))
{
// TruncateToDate converts 1991-02-03T11:22:33Z to 1991-02-03+00:00 which is not a valid date!
var date = (dateTimeVal as P.DateTime)!.TruncateToDate();
Expand All @@ -224,6 +225,35 @@ private void raiseTypeError(string message, object source, bool warning = false,
}
}

private static bool tryParse(string value, Type primitiveType, [NotNullWhen(true)] out object? parsed)
{
if (!P.Any.TryParseToAny(value, primitiveType, out P.Any? any))
{
parsed = null;
return false;
}

parsed = any switch
{
P.Boolean b => b.Value,
P.Code c => c,
P.Concept c => c,
P.Decimal d => d.Value,
P.Integer i => i.Value,
P.Long l => l.Value,
P.Date dt => dt,
P.DateTime dt => dt,
P.Time t => t,
P.Ratio r => r,
P.Quantity q => q,
P.String s => s.Value,
_ => null
};

return parsed is not null;
}


private object? _value;
private bool _valueInitialized = false;
private static object _initializationLock = new();
Expand Down Expand Up @@ -500,7 +530,8 @@ public IEnumerable<object> Annotations(Type type)
}
}



[Obsolete("This class is used for internal purposes and is subject to change without notice. Don't use.")]
public delegate object? AdditionalStructuralRule(ITypedElement node, IExceptionSource ies, object? state);
}

}
Loading