Skip to content

Commit

Permalink
support for dateonly type convertors
Browse files Browse the repository at this point in the history
  • Loading branch information
mivano committed Jun 13, 2023
1 parent c27c154 commit 51e891b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Infrastructure/TypeConvertors/DateOnlyTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AzureCostCli.Infrastructure.TypeConvertors;

public class DateOnlyTypeConverter : StringTypeConverterBase<DateOnly>
{
protected override DateOnly Parse(string s, IFormatProvider? provider) => DateOnly.Parse(s, provider);

protected override string ToIsoString(DateOnly source, IFormatProvider? provider) => source.ToString("O", provider);
}
57 changes: 57 additions & 0 deletions src/Infrastructure/TypeConvertors/StringTypeConverterBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.ComponentModel;
using System.Globalization;

namespace AzureCostCli.Infrastructure.TypeConvertors;

public abstract class StringTypeConverterBase<T> : TypeConverter
{
protected abstract T Parse(string s, IFormatProvider? provider);

protected abstract string ToIsoString(T source, IFormatProvider? provider);

public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is string str)
{
return Parse(str, GetFormat(culture));
}
return base.ConvertFrom(context, culture, value);
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (destinationType == typeof(string) && value is T typedValue)
{
return ToIsoString(typedValue, GetFormat(culture));
}
return base.ConvertTo(context, culture, value, destinationType);
}

private static IFormatProvider? GetFormat(CultureInfo? culture)
{
DateTimeFormatInfo? formatInfo = null;
if (culture != null)
{
formatInfo = (DateTimeFormatInfo?)culture.GetFormat(typeof(DateTimeFormatInfo));
}

return (IFormatProvider?)formatInfo ?? culture;
}
}
13 changes: 12 additions & 1 deletion src/OutputFormatters/JsonOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,23 @@ public sealed class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateOnly.FromDateTime(reader.GetDateTime());
return DateOnly.Parse(reader.GetString()!);
}

public override DateOnly ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateOnly.Parse(reader.GetString()!);
}

public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
var isoDate = value.ToString("O");
writer.WriteStringValue(isoDate);
}

public override void WriteAsPropertyName(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
var isoDate = value.ToString("O");
writer.WritePropertyName(isoDate);
}
}
8 changes: 7 additions & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using AzureCostCli.Commands;
using System.ComponentModel;
using AzureCostCli.Commands;
using AzureCostCli.Commands.CostByResource;
using AzureCostCli.Commands.ShowCommand;
using AzureCostCli.CostApi;
using AzureCostCli.Infrastructure;
using AzureCostCli.Infrastructure.TypeConvertors;
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli;

Expand All @@ -20,6 +22,10 @@

var registrar = new TypeRegistrar(registrations);

#if NET6_0
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
#endif

// Setup the application itself
var app = new CommandApp(registrar);

Expand Down
2 changes: 1 addition & 1 deletion src/azure-cost-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>AzureCostCli</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 51e891b

Please sign in to comment.