|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.ObjectModel; |
| 6 | +using System.IO; |
| 7 | +using System.Reflection.Emit; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Azure.WebJobs.Host.Bindings.Path; |
| 10 | +using Microsoft.Azure.WebJobs.Host.Bindings.Runtime; |
| 11 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 12 | +using Newtonsoft.Json.Linq; |
| 13 | + |
| 14 | +namespace Microsoft.Azure.WebJobs.Script.Binding |
| 15 | +{ |
| 16 | + public class ApiHubTableBinding : FunctionBinding |
| 17 | + { |
| 18 | + private readonly BindingTemplate _dataSetNameBindingTemplate; |
| 19 | + private readonly BindingTemplate _tableNameBindingTemplate; |
| 20 | + private readonly BindingTemplate _entityIdBindingTemplate; |
| 21 | + |
| 22 | + public ApiHubTableBinding( |
| 23 | + ScriptHostConfiguration config, |
| 24 | + ApiHubTableBindingMetadata metadata, |
| 25 | + FileAccess access) |
| 26 | + : base(config, metadata, access) |
| 27 | + { |
| 28 | + Connection = metadata.Connection; |
| 29 | + DataSetName = metadata.DataSetName; |
| 30 | + TableName = metadata.TableName; |
| 31 | + EntityId = metadata.EntityId; |
| 32 | + BindingDirection = metadata.Direction; |
| 33 | + |
| 34 | + _dataSetNameBindingTemplate = CreateBindingTemplate(DataSetName); |
| 35 | + _tableNameBindingTemplate = CreateBindingTemplate(TableName); |
| 36 | + _entityIdBindingTemplate = CreateBindingTemplate(EntityId); |
| 37 | + } |
| 38 | + |
| 39 | + public string Connection { get; } |
| 40 | + |
| 41 | + public string DataSetName { get; } |
| 42 | + |
| 43 | + public string TableName { get; } |
| 44 | + |
| 45 | + public string EntityId { get; } |
| 46 | + |
| 47 | + public BindingDirection BindingDirection { get; } |
| 48 | + |
| 49 | + public override Collection<CustomAttributeBuilder> GetCustomAttributes(Type parameterType) |
| 50 | + { |
| 51 | + var constructorTypes = new[] { typeof(string) }; |
| 52 | + var constructor = typeof(ApiHubTableAttribute).GetConstructor(constructorTypes); |
| 53 | + var constructorArguments = new[] { Connection }; |
| 54 | + var namedProperties = new[] |
| 55 | + { |
| 56 | + typeof(ApiHubTableAttribute).GetProperty("DataSetName"), |
| 57 | + typeof(ApiHubTableAttribute).GetProperty("TableName"), |
| 58 | + typeof(ApiHubTableAttribute).GetProperty("EntityId") |
| 59 | + }; |
| 60 | + var propertyValues = new[] |
| 61 | + { |
| 62 | + DataSetName, |
| 63 | + TableName, |
| 64 | + EntityId |
| 65 | + }; |
| 66 | + |
| 67 | + return new Collection<CustomAttributeBuilder>() |
| 68 | + { |
| 69 | + new CustomAttributeBuilder( |
| 70 | + constructor, |
| 71 | + constructorArguments, |
| 72 | + namedProperties, |
| 73 | + propertyValues) |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + public override async Task BindAsync(BindingContext context) |
| 78 | + { |
| 79 | + var attribute = new ApiHubTableAttribute(Connection) |
| 80 | + { |
| 81 | + DataSetName = BindAndResolve(DataSetName, _dataSetNameBindingTemplate, context), |
| 82 | + TableName = BindAndResolve(TableName, _tableNameBindingTemplate, context), |
| 83 | + EntityId = BindAndResolve(EntityId, _entityIdBindingTemplate, context) |
| 84 | + }; |
| 85 | + |
| 86 | + var runtimeContext = new RuntimeBindingContext(attribute); |
| 87 | + |
| 88 | + if (Access == FileAccess.Read && BindingDirection == BindingDirection.In) |
| 89 | + { |
| 90 | + context.Value = await context.Binder.BindAsync<JObject>(runtimeContext); |
| 91 | + } |
| 92 | + else if (Access == FileAccess.Write && BindingDirection == BindingDirection.Out) |
| 93 | + { |
| 94 | + await BindAsyncCollectorAsync<JObject>(context, runtimeContext); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static BindingTemplate CreateBindingTemplate(string value) |
| 99 | + { |
| 100 | + return |
| 101 | + value != null |
| 102 | + ? BindingTemplate.FromString(value) |
| 103 | + : null; |
| 104 | + } |
| 105 | + |
| 106 | + private string BindAndResolve( |
| 107 | + string value, BindingTemplate template, BindingContext context) |
| 108 | + { |
| 109 | + if (string.IsNullOrEmpty(value)) |
| 110 | + { |
| 111 | + return value; |
| 112 | + } |
| 113 | + |
| 114 | + if (context.BindingData == null || template == null) |
| 115 | + { |
| 116 | + return Resolve(value); |
| 117 | + } |
| 118 | + |
| 119 | + return Resolve(template.Bind(context.BindingData)); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments