Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inputParser: support TypeConverters #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions src/Oakton.Testing/InputParserTester.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq.Expressions;
using Baseline.Reflection;
using Oakton.Help;
Expand Down Expand Up @@ -377,9 +379,15 @@ public void use_dictionary_flag_for_dict()
{
handlerFor(x => x.PropsFlag).ShouldBeOfType<DictionaryFlag>();
}

}

[Fact]
public void type_converters_can_be_used()
{
handle(x => x.Converted, "1234").ShouldBeTrue();

theInput.Converted.Value.ShouldBe(1234);
}
}

public enum Color
{
Expand Down Expand Up @@ -410,9 +418,10 @@ public class InputModel

[FlagAlias("aliased", 'a')]
public string AliasedFlag { get; set; }

public Dictionary<string, string> PropsFlag { get; set; } = new Dictionary<string, string>();


public CustomID Converted { get; set; }
}

public class InputCommand : OaktonCommand<InputModel>
Expand All @@ -428,4 +437,42 @@ public override bool Execute(InputModel input)
throw new NotImplementedException();
}
}
}

[TypeConverter(typeof(CustomIDConverter))]
public struct CustomID : IEquatable<CustomID>
{
public int Value { get; }

public CustomID(int value)
{
Value = value;
}

public bool Equals(CustomID other) => Value == other.Value;

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is CustomID && Equals((CustomID)obj);
}

public override int GetHashCode() => Value;
public override string ToString() => Value.ToString();
}

public class CustomIDConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string == false)
return base.ConvertFrom(context, culture, value);

return new CustomID(int.Parse((string)value));
}
}
}
1 change: 1 addition & 0 deletions src/Oakton/Oakton.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="System.AppContext" Version="4.3.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0" />
<PackageReference Include="System.Reflection.Extensions" Version="4.3.0" />
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<Reference Include="System" />
Expand Down
21 changes: 20 additions & 1 deletion src/Oakton/Parsing/InputParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
Expand All @@ -18,7 +19,13 @@ public static class InputParser
private static readonly Regex SHORT_FLAG_REGEX = new Regex("^{0}[^-]+".ToFormat(SHORT_FLAG_PREFIX));

private static readonly string FLAG_SUFFIX = "Flag";
private static readonly Conversions _converter = new Conversions();
private static readonly Conversions _converter;

static InputParser()
{
_converter = new Conversions();
_converter.RegisterConversionProvider<TypeConverterProvider>();
}


public static List<ITokenHandler> GetHandlers(Type inputType)
Expand Down Expand Up @@ -126,4 +133,16 @@ private static string splitOnPascalCaseAndAddHyphens(string name)
return name.SplitPascalCase().Split(' ').Join("-");
}
}

public class TypeConverterProvider : IConversionProvider
{
public Func<string, object> ConverterFor(Type type)
{
return value =>
{
var converter = TypeDescriptor.GetConverter(type);
return converter.ConvertFromString(value);
};
}
}
}