|
| 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.IO; |
| 6 | +using System.Reflection; |
| 7 | +using System.Reflection.Emit; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 11 | +using Newtonsoft.Json.Linq; |
| 12 | + |
| 13 | +namespace Microsoft.Azure.WebJobs.Script.Binding |
| 14 | +{ |
| 15 | + internal class DocumentDBBinding : FunctionBinding |
| 16 | + { |
| 17 | + private BindingDirection _bindingDirection; |
| 18 | + |
| 19 | + public DocumentDBBinding(ScriptHostConfiguration config, string name, string databaseName, string collectionName, bool createIfNotExists, FileAccess access, BindingDirection direction) : |
| 20 | + base(config, name, BindingType.DocumentDB, access, false) |
| 21 | + { |
| 22 | + DatabaseName = databaseName; |
| 23 | + CollectionName = collectionName; |
| 24 | + CreateIfNotExists = createIfNotExists; |
| 25 | + _bindingDirection = direction; |
| 26 | + } |
| 27 | + |
| 28 | + public string DatabaseName { get; private set; } |
| 29 | + |
| 30 | + public string CollectionName { get; private set; } |
| 31 | + |
| 32 | + public bool CreateIfNotExists { get; private set; } |
| 33 | + |
| 34 | + public override bool HasBindingParameters |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + return false; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public override CustomAttributeBuilder GetCustomAttribute() |
| 43 | + { |
| 44 | + Type attributeType = typeof(DocumentDBAttribute); |
| 45 | + PropertyInfo[] props = new[] |
| 46 | + { |
| 47 | + attributeType.GetProperty("DatabaseName"), |
| 48 | + attributeType.GetProperty("CollectionName"), |
| 49 | + attributeType.GetProperty("CreateIfNotExists") |
| 50 | + }; |
| 51 | + |
| 52 | + object[] propValues = new object[] |
| 53 | + { |
| 54 | + DatabaseName, |
| 55 | + CollectionName, |
| 56 | + CreateIfNotExists |
| 57 | + }; |
| 58 | + |
| 59 | + ConstructorInfo constructor = attributeType.GetConstructor(System.Type.EmptyTypes); |
| 60 | + |
| 61 | + return new CustomAttributeBuilder(constructor, new object[] { }, props, propValues); |
| 62 | + } |
| 63 | + |
| 64 | + public override async Task BindAsync(BindingContext context) |
| 65 | + { |
| 66 | + DocumentDBAttribute attribute = new DocumentDBAttribute |
| 67 | + { |
| 68 | + DatabaseName = DatabaseName, |
| 69 | + CollectionName = CollectionName, |
| 70 | + CreateIfNotExists = CreateIfNotExists |
| 71 | + }; |
| 72 | + |
| 73 | + // Only output bindings are supported. |
| 74 | + if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out) |
| 75 | + { |
| 76 | + IAsyncCollector<JObject> collector = context.Binder.Bind<IAsyncCollector<JObject>>(attribute); |
| 77 | + byte[] bytes; |
| 78 | + using (MemoryStream ms = new MemoryStream()) |
| 79 | + { |
| 80 | + context.Value.CopyTo(ms); |
| 81 | + bytes = ms.ToArray(); |
| 82 | + } |
| 83 | + JObject entity = JObject.Parse(Encoding.UTF8.GetString(bytes)); |
| 84 | + await collector.AddAsync(entity); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments