Skip to content

Commit

Permalink
Add func based converter
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Sep 29, 2023
1 parent fb7ab1d commit c008286
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Meziantou.Framework.HumanReadable;
public static class ConverterListExtensions
{
public static void Add<T>(this IList<HumanReadableConverter> converters, Func<T, string> convert)
{
converters.Add(new FuncConverter<T>((value, options) => convert(value)));
}

public static void Add<T>(this IList<HumanReadableConverter> converters, Func<T, HumanReadableSerializerOptions, string> convert)
{
converters.Add(new FuncConverter<T>(convert));
}

private sealed class FuncConverter<T> : HumanReadableConverter<T>
{
private readonly Func<T, HumanReadableSerializerOptions, string> _converter;

public FuncConverter(Func<T, HumanReadableSerializerOptions, string> converter) => _converter = converter;

protected override void WriteValue(HumanReadableTextWriter writer, T? value, HumanReadableSerializerOptions options)
{
var str = _converter(value, options);
writer.WriteValue(str);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Description>One-way serializer to a human readable format</Description>
<Version>1.0.16</Version>
<Version>1.0.17</Version>
<CompatibilityRange>true</CompatibilityRange>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Description>Enables verification of objects using inline snapshots</Description>
<DefineConstants Condition="'$(IsOfficialBuild)' != 'true'">$(DefineConstants);DEBUG_TaskDialogPrompt</DefineConstants>

<Version>1.0.25</Version>
<Version>1.0.26</Version>
<NoWarn>$(NoWarn);NU5100</NoWarn>

<!-- Buggy because of netstandard2.0 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ public void SerializeObjectGraph()

[Fact]
public void Double() => AssertSerialization(-5.30d, "-5.3");

[Fact]
public void Double_NaN() => AssertSerialization(double.NaN, "NaN");

Expand Down Expand Up @@ -1037,7 +1037,7 @@ public void StringWriter()

[Fact]
public void MethodInfo_dynamic_ValueTuple_Parameter() => AssertSerialization(typeof(Methods).GetMethod(nameof(Methods.ValueTupleDynamic)), "Meziantou.Framework.HumanReadable.Tests.SerializerTests+Methods.ValueTupleDynamic((dynamic, System.Int32) value)");

[Fact]
public void MethodInfo_dynamic_Nested_ValueTuple_Parameter() => AssertSerialization(typeof(Methods).GetMethod(nameof(Methods.ValueTupleNestedDynamic)), "Meziantou.Framework.HumanReadable.Tests.SerializerTests+Methods.ValueTupleNestedDynamic((dynamic A, (System.Int32 B, dynamic C) D) value)");

Expand Down Expand Up @@ -2161,6 +2161,14 @@ public void IgnoreMemberWithType()
});
}

[Fact]
public void FuncConverter()
{
var options = new HumanReadableSerializerOptions();
options.Converters.Add<double>(value => value.ToString("G17", CultureInfo.InvariantCulture));
AssertSerialization(-5.30d, options, "-5.2999999999999998");
}

private sealed class PropThrowAnException
{
public int A => throw new NotSupportedException();
Expand Down

0 comments on commit c008286

Please sign in to comment.