Skip to content

Commit

Permalink
Simplify casts
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamMorrow committed Dec 29, 2023
1 parent 275ca92 commit 45e7d94
Showing 1 changed file with 14 additions and 38 deletions.
52 changes: 14 additions & 38 deletions LiftLog.Ui/Models/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,11 @@ public static implicit operator DecimalValue(decimal value)
return new DecimalValue(units, nanos);
}

public static implicit operator decimal?(DecimalValue? grpcDecimal)
{
if (grpcDecimal is null)
{
return null;
}
return grpcDecimal.Units + grpcDecimal.Nanos / NanoFactor;
}
public static implicit operator decimal?(DecimalValue? grpcDecimal) =>
grpcDecimal is null ? null : (decimal)grpcDecimal;

public static implicit operator DecimalValue?(decimal? value)
{
if (value is null)
{
return null;
}
var units = decimal.ToInt64(value.Value);
var nanos = decimal.ToInt32((value.Value - units) * NanoFactor);
return new DecimalValue(units, nanos);
}
public static implicit operator DecimalValue?(decimal? value) =>
value is null ? null : (DecimalValue)value.Value;
}

internal partial class DateOnlyDao
Expand All @@ -60,13 +46,11 @@ public DateOnlyDao(int year, int month, int day)
public static implicit operator DateOnlyDao(DateOnly model) =>
new(model.Year, model.Month, model.Day);

public static implicit operator DateOnlyDao?(DateOnly? model) =>
model is null
? null
: new DateOnlyDao(model.Value.Year, model.Value.Month, model.Value.Day);

public static implicit operator DateOnly?(DateOnlyDao? dao) =>
dao is null ? null : new DateOnly(dao.Year, dao.Month, dao.Day);
dao is null ? null : (DateOnly)dao;

public static implicit operator DateOnlyDao?(DateOnly? value) =>
value is null ? null : (DateOnlyDao)value.Value;
}

internal partial class TimeOnlyDao
Expand All @@ -85,18 +69,11 @@ public static implicit operator TimeOnly(TimeOnlyDao dao) =>
public static implicit operator TimeOnlyDao(TimeOnly model) =>
new(model.Hour, model.Minute, model.Second, model.Millisecond);

public static implicit operator TimeOnlyDao?(TimeOnly? model) =>
model is null
? null
: new TimeOnlyDao(
model.Value.Hour,
model.Value.Minute,
model.Value.Second,
model.Value.Millisecond
);

public static implicit operator TimeOnly?(TimeOnlyDao? dao) =>
dao is null ? null : new TimeOnly(dao.Hour, dao.Minute, dao.Second, dao.Millisecond);
dao is null ? null : (TimeOnly)dao;

public static implicit operator TimeOnlyDao?(TimeOnly? value) =>
value is null ? null : (TimeOnlyDao)value.Value;
}

internal partial class UUIDDao
Expand All @@ -111,8 +88,7 @@ public UUIDDao(Guid value)
public static implicit operator Guid(UUIDDao dao) => new(dao.Value.ToByteArray());

public static implicit operator UUIDDao?(Guid? value) =>
value is null ? null : new UUIDDao(value.Value);
value is null ? null : (UUIDDao)value.Value;

public static implicit operator Guid?(UUIDDao? dao) =>
dao is null ? null : new Guid(dao.Value.ToByteArray());
public static implicit operator Guid?(UUIDDao? dao) => dao is null ? null : (Guid)dao;
}

0 comments on commit 45e7d94

Please sign in to comment.