Skip to content

Commit

Permalink
Replaced Result<T> with normal TryXXX pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoutkramer committed Jan 17, 2025
1 parent 4253de5 commit 4fe1b2a
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 174 deletions.
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 @@ -154,7 +154,7 @@ public static bool TryConvert(object? value, [NotNullWhen(true)] out Any? primit
}

// some utility methods shared by the subclasses
protected static ArgumentException NotSameTypeComparison(object me, object? them) =>
protected static InvalidOperationException NotSameTypeComparison(object me, object? them) =>
new($"Cannot compare {me} (of type {me.GetType()}) to {them} (of type {them?.GetType()}), because the types differ.");

protected static TOut RunCast<TOut>(Any value) =>
Expand Down
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Base/ElementModel/Types/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Boolean(bool value) : Any, ICqlEquatable, ICqlConvertible
public const string TRUE_LITERAL = "true";
public const string FALSE_LITERAL = "false";

public Boolean() : this(default) { }
public Boolean() : this(false) { }

public bool Value { get; } = value;

Expand All @@ -43,7 +43,7 @@ public static bool TryParse(string representation, [NotNullWhen(true)] out Boole
}
else
{
value = default;
value = null;
return false;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Hl7.Fhir.Base/ElementModel/Types/Code.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ public override bool TryConvertTo(Type to, [NotNullWhen(true)] out Any? result)
}

public override string ToString() => $"{Value}@{System} " + Display;

// Does not support equality, equivalence and ordering in the CQL sense, so no explicit implementations of these interfaces
}
2 changes: 2 additions & 0 deletions src/Hl7.Fhir.Base/ElementModel/Types/Concept.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public override bool TryConvertTo(Type to, [NotNullWhen(true)] out Any? result)
}

public override string ToString() => string.Join(", ", Codes) + (Display != null ? $" \"{Display}\"" : "");

// Does not support equality, equivalence and ordering in the CQL sense, so no explicit implementations of these interfaces
}
11 changes: 4 additions & 7 deletions src/Hl7.Fhir.Base/ElementModel/Types/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,11 @@ private static Date add(Date dateValue, decimal value, string unit)

public bool TryEquals(Any other, [NotNullWhen(true)] out bool? result)
{
if (other is Date && TryCompareTo(other, out var comparison))
{
result = comparison == 0;
return true;
}
result = other is Date
? TryCompareTo(other, out var comparison) ? comparison == 0 : null
: false;

result = null;
return false;
return result is not null;
}

public static bool operator ==(Date a, Date b) => Equals(a, b);
Expand Down
11 changes: 4 additions & 7 deletions src/Hl7.Fhir.Base/ElementModel/Types/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,11 @@ public override bool Equals(object? obj) => obj is Any other && TryEquals(other,

public bool TryEquals(Any other, [NotNullWhen(true)] out bool? result)
{
if (other is DateTime && TryCompareTo(other, out var comparison))
{
result = comparison == 0;
return true;
}
result = other is DateTime
? TryCompareTo(other, out var comparison) ? comparison == 0 : null
: false;

result = null;
return false;
return result is not null;
}

public static bool operator ==(DateTime a, DateTime b) => Equals(a, b);
Expand Down
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Base/ElementModel/Types/Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Hl7.Fhir.ElementModel.Types;

public class Decimal(decimal value = default) : Any, IComparable, ICqlEquatable, ICqlOrderable
public class Decimal(decimal value = 0) : Any, IComparable, ICqlEquatable, ICqlOrderable
{
public decimal Value { get; } = value;

Expand All @@ -31,7 +31,7 @@ public static bool TryParse(string representation, [NotNullWhen(true)] out Decim
{
if (representation == null) throw new ArgumentNullException(nameof(representation));

value = default;
value = null;

if (FORBIDDEN_DECIMAL_PREFIXES.Any(representation.StartsWith) || representation.EndsWith("."))
return false;
Expand Down
79 changes: 26 additions & 53 deletions src/Hl7.Fhir.Base/ElementModel/Types/Long.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,64 +68,37 @@ public int CompareTo(object? obj)
public static implicit operator long(Long i) => i.Value;
public static explicit operator Long(long i) => new (i);

public static implicit operator Decimal(Long i) => new (i.Value);
public static implicit operator Quantity(Long i) => new (i.Value);

public static explicit operator Boolean(Long l) =>
l.Value switch
{
1 => Boolean.True,
0 => Boolean.False,
_ => throw new InvalidCastException($"Cannot cast Long value {l} to Boolean.")
};

public static implicit operator String(Long l) => new(l.ToString());

public static explicit operator Integer(Long l) =>
l.Value is >= int.MinValue and <= int.MaxValue
? new Integer((int)l.Value)
: throw new InvalidCastException($"Cannot cast Long value {l} to Integer, it is too large.");
public static explicit operator Decimal(Long i) => RunCast<Decimal>(i);
public static explicit operator Quantity(Long i) => RunCast<Quantity>(i);
public static explicit operator Boolean(Long l) => RunCast<Boolean>(l);
public static explicit operator String(Long l) => RunCast<String>(l);
public static explicit operator Integer(Long l) => RunCast<Integer>(l);

public override bool TryConvertTo(Type to, [NotNullWhen(true)] out Any? result)
{
result = null;

if (to == typeof(Long))
{
result = this;
return true;
}

if (to == typeof(Integer))
{
result = (Integer)this;
return true;
}

if (to == typeof(Decimal))
{
result = (Decimal)this;
return true;
}

if (to == typeof(Quantity))
{
result = (Quantity)this;
return true;
}

if (to == typeof(Boolean))
{
result = (Boolean)this;
return true;
}

if (to == typeof(String))
{
result = (String)this;
return true;
}

result = default;
return false;
else if (to == typeof(Integer))
result = Value is >= int.MinValue and <= int.MaxValue
? new Integer((int)Value)
: null;
else if (to == typeof(Decimal))
result = new Decimal(Value);
else if (to == typeof(Quantity))
result = new Quantity(Value);
else if (to == typeof(Boolean))
result = Value switch
{
1 => Boolean.True,
0 => Boolean.False,
_ => null
};
else if (to == typeof(String))
result = new String(ToString());

return result is not null;
}

bool? ICqlEquatable.IsEqualTo(Any? other) => other is not null ? Equals(other) : null;
Expand Down
41 changes: 15 additions & 26 deletions src/Hl7.Fhir.Base/ElementModel/Types/Quantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,11 @@ public bool Equals(Any other, QuantityComparison comparisonType) =>
/// calendar durations and definite quantity durations above seconds is determined by the <paramref name="comparisonType"/></remarks>
public bool TryEquals(Any other, QuantityComparison comparisonType, [NotNullWhen(true)] out bool? result)
{
if (other is Quantity && TryCompareTo(other, comparisonType, out var comparison))
{
result = comparison == 0;
return true;
}
result = other is Quantity
? TryCompareTo(other, comparisonType, out var comparison) ? comparison == 0 : null
: false;

result = null;
return false;
return result is not null;
}

public static bool operator ==(Quantity a, Quantity b) => a.CompareTo(b) == 0;
Expand Down Expand Up @@ -224,11 +221,7 @@ public bool TryCompareTo(Any? other, QuantityComparison comparisonType, [NotNull
return true;
}

if (other is not Quantity otherQ)
{
result = null;
return false;
}
if (other is not Quantity otherQ) throw NotSameTypeComparison(this, other);

if (IsDuration && otherQ.IsDuration)
{
Expand Down Expand Up @@ -277,7 +270,7 @@ Quantity normalizeToUcum(Quantity orig)
_ => throw new InvalidOperationException($"Unit '{orig.Unit}' is not a known calendar duration.")
};

return new(orig.Value, ucumUnit, QuantityUnitSystem.UCUM);
return new Quantity(orig.Value, ucumUnit, QuantityUnitSystem.UCUM);
}
}

Expand Down Expand Up @@ -309,25 +302,21 @@ bool ICqlEquatable.IsEquivalentTo(Any? other) =>
other is not null && TryEquals(other, CQL_EQUIVALENCE_COMPARISON, out var result) && result.Value;

int? ICqlOrderable.CompareTo(Any? other) =>
other is not null && TryCompareTo(other, out var result) ? result : null;
other is not null && TryCompareTo(other, out var result)
? result
: null;

public static explicit operator String(Quantity q) => new(q.ToString());
public static explicit operator String(Quantity q) => RunCast<String>(q);

public override bool TryConvertTo(Type to, [NotNullWhen(true)] out Any? result)
{
result = null;

if(to == typeof(Quantity))
{
result = this;
return true;
}

if (to == typeof(String))
{
result = (String)this;
return true;
}
else if (to == typeof(String))
result = new String(ToString());

result = default;
return false;
return result is not null;
}
}
18 changes: 6 additions & 12 deletions src/Hl7.Fhir.Base/ElementModel/Types/Ratio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,18 @@ public static bool TryParse(string representation, [NotNullWhen(true)] out Ratio
public override int GetHashCode() => (Numerator, Denominator).GetHashCode();
public override string ToString() => $"{Numerator}:{Denominator}";

public static explicit operator String(Ratio r) => new(r.ToString());
public static explicit operator String(Ratio r) => RunCast<String>(r);

public override bool TryConvertTo(Type to, [NotNullWhen(true)] out Any? result)
{
result = null;

if(to == typeof(Ratio))
{
result = this;
return true;
}

if (to == typeof(String))
{
result = (String)this;
return true;
}
else if (to == typeof(String))
result = new String(ToString());

result = default;
return false;
return result is not null;
}

public static bool operator ==(Ratio left, Ratio right) => left.Equals(right);
Expand Down
28 changes: 9 additions & 19 deletions src/Hl7.Fhir.Base/ElementModel/Types/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#nullable enable

using Hl7.FhirPath.Sprache;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -93,35 +92,26 @@ public int CompareTo(object? obj)
public override string ToString() => Value;

public static implicit operator string(String s) => s.Value;
public static implicit operator String(string s) => new(s);

public static explicit operator Boolean(String s) =>
s.TryConvertTo<Boolean>(out var result)
? result
: throw new InvalidCastException($"Cannot cast String value {s} to Boolean.");
public static explicit operator String(string s) => new(s);
public static explicit operator Boolean(String s) => RunCast<Boolean>(s);

public override bool TryConvertTo(Type to, [NotNullWhen(true)] out Any? result)
{
result = null;

if(to == typeof(String))
{
result = this;
return true;
}

if (to == typeof(Boolean))
{
var boolValue = Value.ToLower() switch
else if (to == typeof(Boolean))
result = Value.ToLower() switch
{
"true" or "t" or "yes" or "y" or "1" or "1.0" => Boolean.True,
"false" or "f" or "no" or "n" or "0" or "0.0" => Boolean.False,
_ => null
};
else
_ = TryParseToAny(Value, to, out result);

result = boolValue;
return result is not null;
}

return TryParseToAny(Value, to, out result);
return result is not null;
}

private static T convertTo<T>(String s) where T:Any =>
Expand Down
Loading

0 comments on commit 4fe1b2a

Please sign in to comment.