Skip to content

Commit

Permalink
Support output binding when using language worker extensions (Azure#3…
Browse files Browse the repository at this point in the history
…4783)

* Support output binding when using language worker extensions

* add comment
  • Loading branch information
JoshLove-msft authored Mar 8, 2023
1 parent 12f3845 commit 3980728
Show file tree
Hide file tree
Showing 11 changed files with 5,000 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public static void ValidateAzureTableKeyValue(string value)
if (!IsValidAzureTableKeyValue(value))
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
"'{0}' is not a valid value for a partition key or row key.", value));
"'{0}' is not a valid value for a partition key or row key. " +
"Ensure that the table entity contains valid 'PartitionKey' and 'RowKey' values.", value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Azure;
Expand All @@ -14,6 +15,7 @@
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Host.Protocols;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.WebJobs.Extensions.Tables
Expand Down Expand Up @@ -46,6 +48,8 @@ public void Initialize(ExtensionConfigContext context)

binding
.AddConverter<JObject, TableEntity>(CreateTableEntityFromJObject)
// when using isolated .NET worker (and other language workers), the output attribute data will come in as a byte array
.AddConverter<byte[], TableEntity>(CreateTableEntityFromJsonBytes)
.AddConverter<TableEntity, JObject>(ConvertEntityToJObject)
.AddConverter<ITableEntity, TableEntity>(entity =>
{
Expand Down Expand Up @@ -178,6 +182,18 @@ private static JObject ConvertEntityToJObject(TableEntity tableEntity)
return jsonObject;
}

private static TableEntity CreateTableEntityFromJsonBytes(byte[] bytes)
{
JObject jObject = JsonConvert.DeserializeObject<JObject>(
Encoding.UTF8.GetString(bytes),
new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
});

return CreateTableEntityFromJObject(jObject);
}

private static TableEntity CreateTableEntityFromJObject(JObject entity)
{
TableEntity tableEntity = new TableEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ public static JObject JObject(object original)
["RowKey"] = RowKey
};
}

[return: Table(TableNameExpression)]
public static byte[] ByteArray(object original)
{
// This test case is meant to simulate the isolated worker scenario
// where the table output attribute data comes in as a byte array
var dict = new Dictionary<string, object>
{
["Value"] = original,
["PartitionKey"] = PartitionKey,
["RowKey"] = RowKey
};
// We enrich with OData annotations as this is what the isolated workers would ideally do
// and our test cases expect it.
return new BinaryData(dict.ToOdataAnnotatedDictionary()).ToArray();
}
}

[RecordedTest]
Expand Down Expand Up @@ -396,6 +412,19 @@ public static JObject JObject(object original)
["ETag"] = "*"
};
}

[return: Table(TableNameExpression)]
public static byte[] ByteArray(object original)
{
var dict = new Dictionary<string, object>
{
["Value"] = original,
["PartitionKey"] = PartitionKey,
["RowKey"] = RowKey,
["ETag"] = "*"
};
return new BinaryData(dict.ToOdataAnnotatedDictionary()).ToArray();
}
}

[RecordedTest]
Expand Down Expand Up @@ -465,6 +494,16 @@ public static JObject JObject(object original)
["Value"] = JToken.FromObject(original)
};
}

[return: Table(TableNameExpression, PartitionKey, RowKey)]
public static byte[] ByteArray(object original)
{
var dict = new Dictionary<string, object>
{
["Value"] = original
};
return new BinaryData(dict.ToOdataAnnotatedDictionary()).ToArray();
}
}

[RecordedTest]
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3980728

Please sign in to comment.