|
| 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.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Azure.WebJobs.Host.Bindings.Path; |
| 9 | +using Microsoft.WindowsAzure.Storage; |
| 10 | +using Microsoft.WindowsAzure.Storage.Table; |
| 11 | +using Newtonsoft.Json.Linq; |
| 12 | + |
| 13 | +namespace Microsoft.Azure.WebJobs.Script |
| 14 | +{ |
| 15 | + internal class TableBinding : Binding |
| 16 | + { |
| 17 | + private readonly BindingTemplate _partitionKeyBindingTemplate; |
| 18 | + private readonly BindingTemplate _rowKeyBindingTemplate; |
| 19 | + |
| 20 | + public TableBinding(JobHostConfiguration config, string name, string tableName, string partitionKey, string rowKey, FileAccess fileAccess) : base(config, name, "queue", fileAccess, false) |
| 21 | + { |
| 22 | + TableName = tableName; |
| 23 | + PartitionKey = partitionKey; |
| 24 | + RowKey = rowKey; |
| 25 | + _partitionKeyBindingTemplate = BindingTemplate.FromString(PartitionKey); |
| 26 | + _rowKeyBindingTemplate = BindingTemplate.FromString(RowKey); |
| 27 | + } |
| 28 | + |
| 29 | + public string TableName { get; private set; } |
| 30 | + public string PartitionKey { get; private set; } |
| 31 | + public string RowKey { get; private set; } |
| 32 | + |
| 33 | + public override bool HasBindingParameters |
| 34 | + { |
| 35 | + get |
| 36 | + { |
| 37 | + return _partitionKeyBindingTemplate.ParameterNames.Any() || |
| 38 | + _rowKeyBindingTemplate.ParameterNames.Any(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public override async Task BindAsync(IBinder binder, Stream stream, IReadOnlyDictionary<string, string> bindingData) |
| 43 | + { |
| 44 | + string boundPartitionKey = PartitionKey; |
| 45 | + string boundRowKey = RowKey; |
| 46 | + if (bindingData != null) |
| 47 | + { |
| 48 | + boundPartitionKey = _partitionKeyBindingTemplate.Bind(bindingData); |
| 49 | + boundRowKey = _rowKeyBindingTemplate.Bind(bindingData); |
| 50 | + } |
| 51 | + |
| 52 | + boundPartitionKey = Resolve(boundPartitionKey); |
| 53 | + boundRowKey = Resolve(boundRowKey); |
| 54 | + |
| 55 | + if (FileAccess == FileAccess.Write) |
| 56 | + { |
| 57 | + // read the content as a JObject |
| 58 | + JObject jsonObject = null; |
| 59 | + using (StreamReader streamReader = new StreamReader(stream)) |
| 60 | + { |
| 61 | + string content = await streamReader.ReadToEndAsync(); |
| 62 | + jsonObject = JObject.Parse(content); |
| 63 | + } |
| 64 | + |
| 65 | + // TODO: If RowKey has not been specified in the binding, try to |
| 66 | + // derive from the object properties (e.g. "rowKey" or "id" properties); |
| 67 | + |
| 68 | + IAsyncCollector<DynamicTableEntity> collector = binder.Bind<IAsyncCollector<DynamicTableEntity>>(new TableAttribute(TableName)); |
| 69 | + DynamicTableEntity tableEntity = new DynamicTableEntity(boundPartitionKey, boundRowKey); |
| 70 | + foreach (JProperty property in jsonObject.Properties()) |
| 71 | + { |
| 72 | + EntityProperty entityProperty = EntityProperty.CreateEntityPropertyFromObject((object)property.Value); |
| 73 | + tableEntity.Properties.Add(property.Name, entityProperty); |
| 74 | + } |
| 75 | + |
| 76 | + await collector.AddAsync(tableEntity); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + DynamicTableEntity tableEntity = binder.Bind<DynamicTableEntity>(new TableAttribute(TableName, boundPartitionKey, boundRowKey)); |
| 81 | + if (tableEntity != null) |
| 82 | + { |
| 83 | + OperationContext context = new OperationContext(); |
| 84 | + var entityProperties = tableEntity.WriteEntity(context); |
| 85 | + |
| 86 | + JObject jsonObject = new JObject(); |
| 87 | + foreach (var entityProperty in entityProperties) |
| 88 | + { |
| 89 | + jsonObject.Add(entityProperty.Key, entityProperty.Value.StringValue); |
| 90 | + } |
| 91 | + string json = jsonObject.ToString(); |
| 92 | + |
| 93 | + using (StreamWriter sw = new StreamWriter(stream)) |
| 94 | + { |
| 95 | + await sw.WriteAsync(json); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments