Skip to content

Commit

Permalink
Implemented IToSystemPrimitive on all applicable FHIR Primitives.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoutkramer committed Jan 17, 2025
1 parent 4fe1b2a commit c40e360
Show file tree
Hide file tree
Showing 33 changed files with 1,128 additions and 366 deletions.
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;
}
}
}
}
560 changes: 560 additions & 0 deletions src/Hl7.Fhir.Base/CompatibilitySuppressions.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/ElementModel/Types/Any.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Hl7.Fhir.ElementModel.Types;
/// <summary>
/// The base class for all CQL/FhirPath types.
/// </summary>
public abstract class Any : ICqlConvertible
public abstract class Any
{
/// <summary>
/// Returns the concrete subclass of Any that is used to represent the
Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/ElementModel/Types/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Hl7.Fhir.ElementModel.Types;

public class Boolean(bool value) : Any, ICqlEquatable, ICqlConvertible
public class Boolean(bool value) : Any, ICqlEquatable
{
public static Boolean True = new(true);
public static Boolean False = new(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@

namespace Hl7.Fhir.ElementModel.Types;

public interface ICqlConvertible
/// <summary>
/// An interface to convert a model POCO into a CQL/FhirPath type.
/// </summary>
public interface IToSystemPrimitive
{
/// <summary>
/// Tries to convert this object into a CQL/FhirPath type.
/// </summary>
/// <param name="to">The subclass of Any to convert to.</param>
/// <param name="result">If succesful, the converted object, otherwise null.</param>
bool TryConvertTo(Type to, [NotNullWhen(true)] out Any? result);
/// <returns>true if this model object has a CQL equivalent and the conversion succeeded, otherwise false.</returns>
bool TryConvertToSystemType([NotNullWhen(true)] out Any? result);
}


public static class CqlConvertible
{
public static bool TryConvertTo<T>(this ICqlConvertible source, [NotNullWhen(true)] out T? result) where T : Any
public static bool TryConvertTo<T>(this Any source, [NotNullWhen(true)] out T? result) where T : Any
{
var success = source.TryConvertTo(typeof(T), out var any) && any is T;

Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/ElementModel/Types/Quantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace Hl7.Fhir.ElementModel.Types;

public class Quantity(decimal value, string? unit, QuantityUnitSystem system)
: Any, IComparable, ICqlEquatable, ICqlOrderable, ICqlConvertible
: Any, IComparable, ICqlEquatable, ICqlOrderable
{
public const string UCUM = "http://unitsofmeasure.org";
public const string UCUM_UNIT = "1";
Expand Down
59 changes: 12 additions & 47 deletions src/Hl7.Fhir.Base/FhirPath/Functions/ConversionOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static bool? ToBoolean(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<Boolean>(out var result) ? result.Value : null;
}
public static bool? ToBoolean(this Any focus) =>
focus.TryConvertTo<Boolean>(out var result) ? result.Value : null;

/// <summary>
/// FhirPath convertsToBoolean() function
Expand All @@ -41,11 +38,7 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static int? ToInteger(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<Integer>(out var result) ? result.Value : null;
}
public static int? ToInteger(this Any focus) => focus.TryConvertTo<Integer>(out var result) ? result.Value : null;

/// <summary>
/// FhirPath convertsToInteger() function.
Expand All @@ -60,11 +53,8 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static decimal? ToDecimal(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<Decimal>(out var result) ? result.Value : null;
}
public static decimal? ToDecimal(this Any focus) =>
focus.TryConvertTo<Decimal>(out var result) ? result.Value : null;

/// <summary>
/// FhirPath convertsToDecimal() function.
Expand All @@ -79,11 +69,7 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static long? ToLong(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<Long>(out var result) ? result.Value : null;
}
public static long? ToLong(this Any focus) => focus.TryConvertTo<Long>(out var result) ? result.Value : null;


/// <summary>
Expand All @@ -99,11 +85,7 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static P.Quantity? ToQuantity(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<Quantity>(out var result) ? result : null;
}
public static P.Quantity? ToQuantity(this Any focus) => focus.TryConvertTo<Quantity>(out var result) ? result : null;

/// <summary>
/// FhirPath convertsToQuantity() function.
Expand All @@ -118,11 +100,8 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static string? ToStringRepresentation(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<String>(out var result) ? result.Value : null;
}
public static string? ToStringRepresentation(this Any focus) =>
focus.TryConvertTo<String>(out var result) ? result.Value : null;

/// <summary>
/// FhirPath convertsToString() function.
Expand All @@ -136,11 +115,7 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static P.Date? ToDate(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<Date>(out var result) ? result : null;
}
public static P.Date? ToDate(this Any focus) => focus.TryConvertTo<Date>(out var result) ? result : null;


/// <summary>
Expand All @@ -156,11 +131,7 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static P.DateTime? ToDateTime(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<DateTime>(out var result) ? result : null;
}
public static P.DateTime? ToDateTime(this Any focus) => focus.TryConvertTo<DateTime>(out var result) ? result : null;


/// <summary>
Expand All @@ -175,13 +146,7 @@ internal static class ConversionOperators
/// </summary>
/// <param name="focus"></param>
/// <returns></returns>
public static P.Time? ToTime(this Any focus)
{
if (focus is not ICqlConvertible c) return null;
return c.TryConvertTo<Time>(out var result) ? result : null;

}

public static P.Time? ToTime(this Any focus) => focus.TryConvertTo<Time>(out var result) ? result : null;

/// <summary>
/// FhirPath convertsToTime() function.
Expand Down
Loading

0 comments on commit c40e360

Please sign in to comment.