Skip to content

Commit 437e4b1

Browse files
committed
Add JArray case for binding;
support file-based scripts (like MS-Batch; Python)
1 parent c24b916 commit 437e4b1

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/WebJobs.Script/Binding/ExtensionBinding.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Threading.Tasks;
1212
using Microsoft.Azure.WebJobs.Script.Description;
1313
using Microsoft.Azure.WebJobs.Script.Extensibility;
14+
using Newtonsoft.Json;
1415
using Newtonsoft.Json.Linq;
1516

1617
namespace Microsoft.Azure.WebJobs.Script.Binding
@@ -46,6 +47,8 @@ public override async Task BindAsync(BindingContext context)
4647
{
4748
context.Attributes = _attributes.ToArray();
4849

50+
object inputValue = null;
51+
4952
if (_binding.DefaultType == typeof(IAsyncCollector<byte[]>))
5053
{
5154
await BindAsyncCollectorAsync<byte[]>(context);
@@ -67,13 +70,32 @@ public override async Task BindAsync(BindingContext context)
6770
var result = await context.Binder.BindAsync<JObject>(_attributes.ToArray());
6871
if (Access == FileAccess.Read)
6972
{
70-
context.Value = result;
73+
inputValue = result;
7174
}
7275
}
76+
else if (_binding.DefaultType == typeof(JArray))
77+
{
78+
JArray entityArray = await context.Binder.BindAsync<JArray>(_attributes.ToArray());
79+
inputValue = entityArray;
80+
}
7381
else
7482
{
7583
throw new NotSupportedException($"ScriptBinding type {_binding.DefaultType} is not supported");
7684
}
85+
86+
if (inputValue != null)
87+
{
88+
if (context.DataType == DataType.Stream)
89+
{
90+
// In file-based scripting (like Python), arguments may need to be copied to the file system.
91+
var inputStream = (Stream)context.Value;
92+
ConvertValueToStream(inputValue, inputStream);
93+
}
94+
else
95+
{
96+
context.Value = inputValue;
97+
}
98+
}
7799
}
78100

79101
internal static CustomAttributeBuilder GetAttributeBuilder(Attribute attribute)

src/WebJobs.Script/Binding/FunctionBinding.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.Azure.WebJobs.Host;
1313
using Microsoft.Azure.WebJobs.Script.Description;
1414
using Microsoft.Azure.WebJobs.Script.Extensibility;
15+
using Newtonsoft.Json;
1516
using Newtonsoft.Json.Linq;
1617

1718
namespace Microsoft.Azure.WebJobs.Script.Binding
@@ -236,6 +237,23 @@ public static void ConvertValueToStream(object value, Stream stream)
236237
{
237238
bytes = BitConverter.GetBytes((double)value);
238239
}
240+
else
241+
{
242+
var jValue = value as JToken;
243+
if (jValue != null)
244+
{
245+
string json = jValue.ToString(Formatting.None);
246+
247+
// Use StreamWriter to handle encoding.
248+
// We're explicitly NOT disposing the StreamWriter because
249+
// we don't want to close the underlying Stream
250+
StreamWriter sw = new StreamWriter(stream);
251+
sw.Write(json);
252+
sw.Flush();
253+
254+
return;
255+
}
256+
}
239257

240258
using (valueStream = new MemoryStream(bytes))
241259
{

0 commit comments

Comments
 (0)