Skip to content

Commit 3134209

Browse files
committed
addressing more PRs
1 parent 494a085 commit 3134209

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

src/DotNetWorker.Core/Context/Features/IDictionaryExtensions.cs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,59 @@
88
using System;
99
using System.Collections.Concurrent;
1010
using System.Collections.Generic;
11+
using System.ComponentModel;
1112

12-
namespace Microsoft.Azure.Functions.Worker
13+
internal static class IDictionaryExtensions
1314
{
14-
internal static class IDictionaryExtensions
15+
internal static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
1516
{
16-
internal static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
17+
if (key == null)
1718
{
18-
if (key == null)
19-
{
20-
throw new ArgumentNullException(nameof(key));
21-
}
22-
23-
if (dictionary.ContainsKey(key))
24-
{
25-
return false;
26-
}
27-
28-
dictionary.Add(key, value);
29-
return true;
19+
throw new ArgumentNullException(nameof(key));
3020
}
21+
22+
if (dictionary.ContainsKey(key))
23+
{
24+
return false;
25+
}
26+
27+
dictionary.Add(key, value);
28+
return true;
3129
}
30+
}
3231

33-
internal static class ConcurrentDictionaryExtensions
32+
internal static class ConcurrentDictionaryExtensions
33+
{
34+
public static TValue GetOrAdd<TKey, TValue, TArg>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
3435
{
35-
public static TValue GetOrAdd<TKey, TValue, TArg>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
36+
if (dictionary == null)
3637
{
37-
if (dictionary == null)
38-
{
39-
throw new ArgumentNullException(nameof(dictionary));
40-
}
41-
42-
if (key == null)
43-
{
44-
throw new ArgumentNullException(nameof(key));
45-
}
46-
47-
if (valueFactory == null)
48-
{
49-
throw new ArgumentNullException(nameof(valueFactory));
50-
}
51-
52-
return dictionary.GetOrAdd(key, k => valueFactory(k, factoryArgument));
38+
throw new ArgumentNullException(nameof(dictionary));
5339
}
40+
41+
if (key == null)
42+
{
43+
throw new ArgumentNullException(nameof(key));
44+
}
45+
46+
if (valueFactory == null)
47+
{
48+
throw new ArgumentNullException(nameof(valueFactory));
49+
}
50+
51+
return dictionary.GetOrAdd(key, k => valueFactory(k, factoryArgument));
5452
}
5553
}
54+
55+
internal static class KeyValuePairExtensions
56+
{
57+
// Based on https://source.dot.net/#System.Private.CoreLib/KeyValuePair.cs,aa57b8e336bf7f59
58+
[EditorBrowsable(EditorBrowsableState.Never)]
59+
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
60+
{
61+
key = pair.Key;
62+
value = pair.Value;
63+
}
64+
}
65+
5666
#endif

src/DotNetWorker.Core/Hosting/WorkerOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public bool IncludeEmptyEntriesInMessagePayload
5858

5959
private bool GetBoolCapability(string name)
6060
{
61-
return Capabilities.TryGetValue(name, out string value) && bool.TryParse(value, out bool b) && b;
61+
return Capabilities.TryGetValue(name, out string? value) && bool.TryParse(value, out bool b) && b;
6262
}
6363

6464
// For false values, the host does not expect the capability to exist; there are some cases where this
@@ -67,7 +67,7 @@ private void SetBoolCapability(string name, bool value)
6767
{
6868
if (value)
6969
{
70-
Capabilities[name] = value.ToString();
70+
Capabilities[name] = bool.TrueString;
7171
}
7272
else
7373
{

src/DotNetWorker.Grpc/GrpcWorker.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
76
using System.Runtime.InteropServices;
87
using System.Text.RegularExpressions;
@@ -213,20 +212,20 @@ internal static WorkerInitResponse WorkerInitRequestHandler(WorkerInitRequest re
213212

214213
response.WorkerMetadata.CustomProperties.Add("Worker.Grpc.Version", typeof(GrpcWorker).Assembly.GetName().Version?.ToString());
215214

216-
// Add required capabilities; these cannot be modified
217-
response.Capabilities.Add("RpcHttpBodyOnly", bool.TrueString);
218-
response.Capabilities.Add("RawHttpBodyBytes", bool.TrueString);
219-
response.Capabilities.Add("RpcHttpTriggerMetadataRemoved", bool.TrueString);
220-
response.Capabilities.Add("UseNullableValueDictionaryForHttp", bool.TrueString);
221-
response.Capabilities.Add("TypedDataCollection", bool.TrueString);
222-
response.Capabilities.Add("WorkerStatus", bool.TrueString);
223-
224-
// Add additional capabilites defined by WorkerOptions
225-
foreach (KeyValuePair<string, string> entry in workerOptions.Capabilities)
215+
// Add additional capabilities defined by WorkerOptions
216+
foreach ((string key, string value) in workerOptions.Capabilities)
226217
{
227-
response.Capabilities.Add(entry.Key, entry.Value);
218+
response.Capabilities[key] = value;
228219
}
229220

221+
// Add required capabilities; these cannot be modified and will override anything from WorkerOptions
222+
response.Capabilities["RpcHttpBodyOnly"] = bool.TrueString;
223+
response.Capabilities["RawHttpBodyBytes"] = bool.TrueString;
224+
response.Capabilities["RpcHttpTriggerMetadataRemoved"] = bool.TrueString;
225+
response.Capabilities["UseNullableValueDictionaryForHttp"] = bool.TrueString;
226+
response.Capabilities["TypedDataCollection"] = bool.TrueString;
227+
response.Capabilities["WorkerStatus"] = bool.TrueString;
228+
230229
return response;
231230
}
232231

0 commit comments

Comments
 (0)