-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/Meziantou.Framework.HumanReadableSerializer/ConverterListExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters