Skip to content

Commit 593f621

Browse files
committed
Minor cleanup in binding pipeline
1 parent 20f6b20 commit 593f621

File tree

2 files changed

+21
-38
lines changed

2 files changed

+21
-38
lines changed

src/WebJobs.Script/Binding/ExtensionBinding.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ public override async Task BindAsync(BindingContext context)
4646
{
4747
context.Attributes = _attributes.ToArray();
4848

49-
object inputValue = null;
50-
5149
if (_binding.DefaultType == typeof(IAsyncCollector<byte[]>))
5250
{
5351
await BindAsyncCollectorAsync<byte[]>(context);
@@ -66,35 +64,16 @@ public override async Task BindAsync(BindingContext context)
6664
}
6765
else if (_binding.DefaultType == typeof(JObject))
6866
{
69-
var result = await context.Binder.BindAsync<JObject>(_attributes.ToArray());
70-
if (Access == FileAccess.Read)
71-
{
72-
inputValue = result;
73-
}
67+
await BindJTokenAsync<JObject>(context, Access);
7468
}
7569
else if (_binding.DefaultType == typeof(JArray))
7670
{
77-
JArray entityArray = await context.Binder.BindAsync<JArray>(_attributes.ToArray());
78-
inputValue = entityArray;
71+
await BindJTokenAsync<JArray>(context, Access);
7972
}
8073
else
8174
{
8275
throw new NotSupportedException($"ScriptBinding type {_binding.DefaultType} is not supported");
8376
}
84-
85-
if (inputValue != null)
86-
{
87-
if (context.DataType == DataType.Stream)
88-
{
89-
// In file-based scripting (like Python), arguments may need to be copied to the file system.
90-
var inputStream = (Stream)context.Value;
91-
ConvertValueToStream(inputValue, inputStream);
92-
}
93-
else
94-
{
95-
context.Value = inputValue;
96-
}
97-
}
9877
}
9978

10079
internal static CustomAttributeBuilder GetAttributeBuilder(Attribute attribute)

src/WebJobs.Script/Binding/FunctionBinding.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ internal static async Task BindAsyncCollectorAsync<T>(BindingContext context)
187187
}
188188
}
189189

190+
internal static async Task BindJTokenAsync<T>(BindingContext context, FileAccess access)
191+
{
192+
var result = await context.Binder.BindAsync<T>(context.Attributes);
193+
194+
if (access == FileAccess.Read)
195+
{
196+
if (context.DataType == DataType.Stream)
197+
{
198+
ConvertValueToStream(result, (Stream)context.Value);
199+
}
200+
201+
context.Value = result;
202+
}
203+
}
204+
190205
internal static async Task BindStreamAsync(BindingContext context, FileAccess access)
191206
{
192207
Stream stream = await context.Binder.BindAsync<Stream>(context.Attributes);
@@ -233,22 +248,11 @@ public static void ConvertValueToStream(object value, Stream stream)
233248
{
234249
bytes = BitConverter.GetBytes((double)value);
235250
}
236-
else
251+
else if (value is JToken)
237252
{
238-
var jValue = value as JToken;
239-
if (jValue != null)
240-
{
241-
string json = jValue.ToString(Formatting.None);
242-
243-
// Use StreamWriter to handle encoding.
244-
// We're explicitly NOT disposing the StreamWriter because
245-
// we don't want to close the underlying Stream
246-
StreamWriter sw = new StreamWriter(stream);
247-
sw.Write(json);
248-
sw.Flush();
249-
250-
return;
251-
}
253+
JToken jToken = (JToken)value;
254+
string json = jToken.ToString(Formatting.None);
255+
bytes = Encoding.UTF8.GetBytes(json);
252256
}
253257

254258
using (valueStream = new MemoryStream(bytes))

0 commit comments

Comments
 (0)