Skip to content

Commit

Permalink
Add BinaryData default bindings. (#2670)
Browse files Browse the repository at this point in the history
* add BinaryData default bindings.

* poke CI?

* fix build.

* fix build.
  • Loading branch information
kasobol-msft authored Feb 4, 2021
1 parent 0771300 commit 67aa71a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/Microsoft.Azure.WebJobs.Host/Bindings/ConverterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ public ConverterManager()
// Default is UTF8, not write a BOM, close stream when done.
return new StreamWriter(stream);
});

this.AddExactConverter<Stream, BinaryData>(async (stream, cancellationToken) =>
{
return await BinaryData.FromStreamAsync(stream, cancellationToken);
});
this.AddExactConverter<ApplyConversion<BinaryData, Stream>, object>(async (pair, cancellationToken) =>
{
var data = pair.Value;
var stream = pair.Existing;

await data.ToStream().CopyToAsync(stream);
return null;
});

this.AddExactConverter<byte[], BinaryData>((bytes) =>
{
return BinaryData.FromBytes(bytes);
});
this.AddExactConverter<BinaryData, byte[]>((binaryData) =>
{
return binaryData.ToArray();
});
}

// If somebody registered a converter from Src-->Dest, then both those types can be used to
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Azure.WebJobs.Host/WebJobs.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.8.0" />
<PackageReference Include="System.Memory.Data" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public async Task Test(JobHost<ConfigStream> host)
{
foreach (var funcName in new string[]
{
"StreamRead", "StringRead", "ByteArrayRead", "TextReaderRead"
"StreamRead", "StringRead", "ByteArrayRead", "BinaryDataRead", "TextReaderRead"
})
{
_log = null;
Expand All @@ -321,7 +321,8 @@ public async Task Test(JobHost<ConfigStream> host)
"WriteTextWriter2",
"WriteTextWriter3",
"WriteString",
"WriteByteArray"
"WriteByteArray",
"WriteBinaryData"
})
{
_writeStream = null;
Expand Down Expand Up @@ -370,6 +371,14 @@ [TestStream] byte[] bytes
_log = Encoding.UTF8.GetString(bytes);
}

// Read as BinaryData
public void BinaryDataRead(
[TestStream] BinaryData binaryData
)
{
_log = binaryData.ToString();
}

public void TextReaderRead(
[TestStream("path", FileAccess.Read)] TextReader tr
)
Expand Down Expand Up @@ -439,6 +448,13 @@ [TestStream] out byte[] x
x = Encoding.UTF8.GetBytes(_writeMessage);
}

public void WriteBinaryData(
[TestStream] out BinaryData x
)
{
x = BinaryData.FromString(_writeMessage);
}


#endregion // #region Write overloads

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Reflection;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs.Host.TestCommon;
using System.IO;

namespace Microsoft.Azure.WebJobs.Host.UnitTests
{
Expand Down Expand Up @@ -46,15 +47,21 @@ public void ExactMatchOverride()
Assert.Equal("*x*", x1);
}

private T TestDefaultConverter<F, T>(F from, T to, ConverterManager cm = null)
private T TestDefaultConverter<F, T>(F from, T to, ConverterManager cm = null, Action<T> assertion = null)
{
if (cm == null) {
cm = new ConverterManager();
}
var converter = cm.GetSyncConverter<F, T, Attribute>();
Assert.NotNull(converter);
var result = converter(from, null, null);
Assert.Equal(to, result);
if (assertion == null)
{
Assert.Equal(to, result);
} else
{
assertion(result);
}
return result;
}

Expand Down Expand Up @@ -176,7 +183,18 @@ public void StringAndByteArray()
Wrapper obj3 = fromBytes(bytes, null, context);
Assert.Equal("ABC", obj3.Value);
}


// BinaryData converters.
[Fact]
public void BinaryData()
{
byte[] data = new byte[124];
new Random().NextBytes(data);
TestDefaultConverter(data, new BinaryData(data), assertion: result => Assert.Equal(data, result.ToArray()));

TestDefaultConverter(new BinaryData(data), data);
}

// Overload conversions on type if they're using different attributes.
[Fact]
public void AttributeOverloads()
Expand Down

0 comments on commit 67aa71a

Please sign in to comment.