-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathTriggerAdapterBindingProvider.cs
216 lines (182 loc) · 7.92 KB
/
TriggerAdapterBindingProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Host.Protocols;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs.Host.Bindings
{
// Regular BindToInput has to do a TAttribute --> Value creation.
// But triggers already have a Listener that provided the initial object; and we're just
// converting it to the user's target parameter.
internal class TriggerAdapterBindingProvider<TAttribute, TTriggerValue> :
FluentBindingProvider<TAttribute>, IBindingProvider, IBindingRuleProvider
where TAttribute : Attribute
where TTriggerValue : class
{
private readonly INameResolver _nameResolver;
private readonly IConverterManager _converterManager;
public TriggerAdapterBindingProvider(
INameResolver nameResolver,
IConverterManager converterManager
)
{
_nameResolver = nameResolver;
_converterManager = converterManager;
}
// Listed in precedence for providing via DefaultType.
// Precedence is more important than how we produce the default type (a direct conversion vs. a converter)
private static readonly Type[] _defaultTypes = new Type[] {
typeof(byte[]),
typeof(JObject),
typeof(JArray),
typeof(string)
};
public Type GetDefaultType(Attribute attribute, FileAccess access, Type requestedType)
{
foreach (var target in _defaultTypes)
{
if (_converterManager.HasConverter<TAttribute>(typeof(TTriggerValue), target))
{
return target;
}
}
return typeof(object);
}
public IEnumerable<BindingRule> GetRules()
{
if (typeof(TTriggerValue).IsPublic)
{
yield return new BindingRule
{
SourceAttribute = typeof(TAttribute),
UserType = OpenType.FromType<TTriggerValue>()
};
}
var cm = (ConverterManager)_converterManager;
var types = cm.GetPossibleDestinationTypesFromSource(typeof(TAttribute), typeof(TTriggerValue));
var converters = new Type[] { typeof(TTriggerValue) };
foreach (OpenType type in types)
{
yield return new BindingRule
{
SourceAttribute = typeof(TAttribute),
Converters = converters,
UserType = type
};
}
}
public Task<IBinding> TryCreateAsync(BindingProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var parameter = context.Parameter;
var parameterType = parameter.ParameterType;
if (parameterType.IsByRef)
{
return Task.FromResult<IBinding>(null);
}
var type = typeof(ExactBinding<>).MakeGenericType(typeof(TAttribute), typeof(TTriggerValue), parameterType);
var method = type.GetMethod("TryBuild", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var binding = BindingFactoryHelpers.MethodInvoke<IBinding>(method, this, context);
return Task.FromResult<IBinding>(binding);
}
private class ExactBinding<TUserType> : IBinding
{
private FuncAsyncConverter<TTriggerValue, TUserType> _converter;
private FuncAsyncConverter<DirectInvokeString, TTriggerValue> _directInvoker;
private FuncAsyncConverter<TTriggerValue, DirectInvokeString> _getInvokeString;
private TAttribute _attribute;
public bool FromAttribute => true;
public static ExactBinding<TUserType> TryBuild(
TriggerAdapterBindingProvider<TAttribute, TTriggerValue> parent,
BindingProviderContext context)
{
IConverterManager cm = parent._converterManager;
var converter = cm.GetConverter<TTriggerValue, TUserType, TAttribute>();
if (converter == null)
{
return null;
}
var parameter = context.Parameter;
var attributeSource = TypeUtility.GetResolvedAttribute<TAttribute>(parameter);
return new ExactBinding<TUserType>
{
_converter = converter,
_directInvoker = GetDirectInvoker(cm),
_getInvokeString = GetDirectInvokeString(cm),
_attribute = attributeSource
};
}
private static FuncAsyncConverter<TTriggerValue, DirectInvokeString> GetDirectInvokeString(IConverterManager cm)
{
var converter = cm.GetConverter<TTriggerValue, DirectInvokeString, TAttribute>();
if (converter != null)
{
return converter;
}
var strConverter = cm.GetConverter<TTriggerValue, string, TAttribute>();
if (strConverter != null)
{
return async (input, attr, ctx) =>
{
var str = await strConverter(input, attr, ctx);
return new DirectInvokeString(str);
};
}
// No converter. Provide something to display in UI
return (input, attr, ctx) => Task.FromResult(DirectInvokeString.None);
}
private static FuncAsyncConverter<DirectInvokeString, TTriggerValue> GetDirectInvoker(IConverterManager cm)
{
var direct = cm.GetConverter<DirectInvokeString, TTriggerValue, TAttribute>();
if (direct != null)
{
return direct;
}
var str = cm.GetConverter<string, TTriggerValue, TAttribute>();
if (str != null)
{
return (input, attr, ctx) => str(input.Value, attr, ctx);
}
return null;
}
public async Task<IValueProvider> BindAsync(object value, ValueBindingContext context)
{
TTriggerValue val = value as TTriggerValue;
if (val == null)
{
if (_directInvoker != null && (value is string str))
{
// Direct invoke case. Need to converrt String-->TTriggerValue.
val = await _directInvoker(new DirectInvokeString(str), _attribute, context);
}
else
{
// How is this possible?
throw new NotImplementedException();
}
}
TUserType result = await _converter(val, _attribute, context);
DirectInvokeString invokeString = await _getInvokeString(val, _attribute, context);
IValueProvider vp = new ConstantValueProvider(result, typeof(TUserType), invokeString.Value);
return vp;
}
public Task<IValueProvider> BindAsync(BindingContext context)
{
// Never called, since a trigger alreayd has an object.
throw new NotImplementedException();
}
public ParameterDescriptor ToParameterDescriptor()
{
return new ParameterDescriptor();
}
}
}
}