From 9be811a89ab42ea658f62bda52a1cffebae6df9d Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Wed, 18 Nov 2020 18:37:20 +0200 Subject: [PATCH] Refactor ArgumentValueConverter into a class --- .../CommandSchemaAnalyzerTests.cs | 8 ++--- .../Commands/SupportedArgumentTypesCommand.cs | 4 +-- CliFx/ArgumentValueConverter.cs | 30 +++++++++++++++++++ CliFx/Attributes/CommandArgumentAttribute.cs | 2 +- CliFx/Exceptions/CliFxException.cs | 6 ++-- CliFx/IArgumentValueConverter.cs | 13 -------- 6 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 CliFx/ArgumentValueConverter.cs delete mode 100644 CliFx/IArgumentValueConverter.cs diff --git a/CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs b/CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs index c612eb0..30771d8 100644 --- a/CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs +++ b/CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs @@ -153,9 +153,9 @@ public class MyCommand : ICommand // language=cs @" -public class MyConverter : IArgumentValueConverter +public class MyConverter : ArgumentValueConverter { - public object ConvertFrom(string value) => value; + public string ConvertFrom(string value) => value; } [Command] @@ -305,9 +305,9 @@ public class MyCommand : ICommand // language=cs @" -public class MyConverter : IArgumentValueConverter +public class MyConverter : ArgumentValueConverter { - public object ConvertFrom(string value) => value; + public string ConvertFrom(string value) => value; } [Command] diff --git a/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs b/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs index 8969904..71233b3 100644 --- a/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs +++ b/CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs @@ -165,9 +165,9 @@ namespace CliFx.Tests.Commands public CustomConvertible(int value) => Value = value; } - public class CustomConvertibleConverter : IArgumentValueConverter + public class CustomConvertibleConverter : ArgumentValueConverter { - public object ConvertFrom(string value) => + public override CustomConvertible ConvertFrom(string value) => new CustomConvertible(int.Parse(value, CultureInfo.InvariantCulture)); } } diff --git a/CliFx/ArgumentValueConverter.cs b/CliFx/ArgumentValueConverter.cs new file mode 100644 index 0000000..7f47f47 --- /dev/null +++ b/CliFx/ArgumentValueConverter.cs @@ -0,0 +1,30 @@ +namespace CliFx +{ + /// + /// Implements custom conversion logic that maps an argument value to a domain type. + /// + /// + /// This type is public for legacy reasons. + /// Please derive from instead. + /// + public interface IArgumentValueConverter + { + /// + /// Converts an input value to object of required type. + /// + public object ConvertFrom(string value); + } + + /// + /// A base type for custom argument converters. + /// + public abstract class ArgumentValueConverter : IArgumentValueConverter + { + /// + /// Converts an input value to object of required type. + /// + public abstract T ConvertFrom(string value); + + object IArgumentValueConverter.ConvertFrom(string value) => ConvertFrom(value)!; + } +} \ No newline at end of file diff --git a/CliFx/Attributes/CommandArgumentAttribute.cs b/CliFx/Attributes/CommandArgumentAttribute.cs index ef275a9..b400b88 100644 --- a/CliFx/Attributes/CommandArgumentAttribute.cs +++ b/CliFx/Attributes/CommandArgumentAttribute.cs @@ -15,7 +15,7 @@ namespace CliFx.Attributes /// /// Type of converter to use when mapping the argument value. - /// Converter must implement . + /// Converter must derive from . /// public Type? Converter { get; set; } diff --git a/CliFx/Exceptions/CliFxException.cs b/CliFx/Exceptions/CliFxException.cs index 55535a0..fb8c69f 100644 --- a/CliFx/Exceptions/CliFxException.cs +++ b/CliFx/Exceptions/CliFxException.cs @@ -181,7 +181,7 @@ If it's not feasible to fit into these constraints, consider using options inste Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameter(s) with invalid converters: {invalidParameters.JoinToString(Environment.NewLine)} -Specified converter must implement {typeof(IArgumentValueConverter).FullName}."; +Specified converter must implement {typeof(ArgumentValueConverter<>).FullName}."; return new CliFxException(message.Trim()); } @@ -279,7 +279,7 @@ Environment variable names are not case-sensitive."; Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid converters: {invalidOptions.JoinToString(Environment.NewLine)} -Specified converter must implement {typeof(IArgumentValueConverter).FullName}."; +Specified converter must implement {typeof(ArgumentValueConverter<>).FullName}."; return new CliFxException(message.Trim()); } @@ -292,7 +292,7 @@ Specified converter must implement {typeof(IArgumentValueConverter).FullName}."; Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid validators: {invalidOptions.JoinToString(Environment.NewLine)} -Specified validators must inherit from {typeof(IArgumentValueValidator).FullName}."; +Specified validators must inherit from {typeof(ArgumentValueValidator<>).FullName}."; return new CliFxException(message.Trim()); } diff --git a/CliFx/IArgumentValueConverter.cs b/CliFx/IArgumentValueConverter.cs deleted file mode 100644 index 9929d42..0000000 --- a/CliFx/IArgumentValueConverter.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace CliFx -{ - /// - /// Implements custom conversion logic that maps an argument value to a domain type. - /// - public interface IArgumentValueConverter - { - /// - /// Converts an input value to object of required type. - /// - public object ConvertFrom(string value); - } -}