Skip to content

Commit

Permalink
Support OpenType[] in converters
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeStall committed Feb 9, 2017
1 parent fff0364 commit f776c7a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/Microsoft.Azure.WebJobs.Host/Bindings/ConverterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ private static string GetKey<TSrc, TDest, TAttribute>()

internal static OpenType GetTypeValidator<T>()
{
var openType = GetOpenType<T>();
return GetTypeValidator(typeof(T));
}

internal static OpenType GetTypeValidator(Type type)
{
var openType = GetOpenType(type);
if (openType != null)
{
return openType;
}
return new ExactMatch(typeof(T));

return new ExactMatch(type);
}

// Gets an OpenType from the given argument.
Expand All @@ -99,6 +104,13 @@ private static OpenType GetOpenType(Type t)
return (OpenType)Activator.CreateInstance(t);
}

if (t.IsArray)
{
var elementType = t.GetElementType();
var innerType = GetTypeValidator(elementType);
return new ArrayOpenType(innerType);
}

// Rewriter rule for generics so customers can say: IEnumerable<OpenType>
if (t.IsGenericType)
{
Expand Down Expand Up @@ -385,5 +397,28 @@ public override bool IsMatch(Type type)
return type == _type;
}
}

// Matches any T[]
private class ArrayOpenType : OpenType
{
private readonly OpenType _inner;
public ArrayOpenType(OpenType inner)
{
_inner = inner;
}
public override bool IsMatch(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (type.IsArray)
{
var elementType = type.GetElementType();
return _inner.IsMatch(elementType);
}
return false;
}
}
} // end class ConverterManager
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,42 @@ public void OpenTypeConverterWithOneGenericArg()
}
}


class OpenArrayConverter<T>
: IConverter<T[], string>
{
public string Convert(T[] input)
{
return string.Join(",", input);
}
}

// Test OpenType[] --> converter
[Fact]
public void OpenTypeArray()
{
var cm = new ConverterManager();

cm.AddConverter<OpenType[], string, Attribute>(typeof(OpenArrayConverter<>));
var attr = new TestAttribute(null);

var converter = cm.GetConverter<int[], string, Attribute>();
Assert.Equal("1,2,3", converter(new int[] { 1, 2, 3 }, attr, null));
}

// Test concrete array converters.
[Fact]
public void ClosedTypeArray()
{
var cm = new ConverterManager();

cm.AddConverter<int[], string, Attribute>(new OpenArrayConverter<int>());
var attr = new TestAttribute(null);

var converter = cm.GetConverter<int[], string, Attribute>();
Assert.Equal("1,2,3", converter(new int[] { 1, 2, 3 }, attr, null));
}

// Counter used by tests to verify that converter ctors are only run once and then shared across
// multiple invocations.
private int _counter;
Expand Down

0 comments on commit f776c7a

Please sign in to comment.