4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Dynamic ;
7
- using System . Linq ;
8
7
using Microsoft . AspNetCore . Http ;
9
8
using Microsoft . Azure . WebJobs . Script . Grpc . Messages ;
10
9
using Microsoft . Azure . WebJobs . Script . Workers . Rpc ;
@@ -13,30 +12,58 @@ namespace Microsoft.Azure.WebJobs.Script.Grpc
13
12
{
14
13
internal static class GrpcMessageExtensionUtilities
15
14
{
16
- public static object ConvertFromHttpMessageToExpando ( RpcHttp inputMessage )
15
+ private static readonly object BoxedTrue = true ;
16
+ private static readonly object BoxedFalse = false ;
17
+ private static readonly IReadOnlyDictionary < string , object > EmptyHeaders = new Dictionary < string , object > ( ) ;
18
+
19
+ public static ExpandoObject ConvertFromHttpMessageToExpando ( RpcHttp inputMessage )
17
20
{
18
- if ( inputMessage == null )
21
+ if ( inputMessage is null )
19
22
{
20
23
return null ;
21
24
}
22
25
23
- dynamic expando = new ExpandoObject ( ) ;
24
- expando . method = inputMessage . Method ;
25
- expando . query = inputMessage . Query as IDictionary < string , string > ;
26
- expando . statusCode = inputMessage . StatusCode ;
27
- expando . headers = inputMessage . Headers . ToDictionary ( p => p . Key , p => ( object ) p . Value ) ;
28
- expando . enableContentNegotiation = inputMessage . EnableContentNegotiation ;
26
+ var expando = new ExpandoObject ( ) ;
27
+ IDictionary < string , object > dict = expando ;
28
+
29
+ dict [ "method" ] = inputMessage . Method ;
30
+ dict [ "query" ] = inputMessage . Query ;
31
+ dict [ "statusCode" ] = inputMessage . StatusCode ;
32
+ dict [ "enableContentNegotiation" ] = inputMessage . EnableContentNegotiation ? BoxedTrue : BoxedFalse ;
29
33
30
- expando . cookies = new List < Tuple < string , string , CookieOptions > > ( ) ;
31
- foreach ( RpcHttpCookie cookie in inputMessage . Cookies )
34
+ if ( inputMessage . Headers is { Count : > 0 } )
35
+ {
36
+ var headerDict = new Dictionary < string , object > ( inputMessage . Headers . Count ) ;
37
+ foreach ( var kvp in inputMessage . Headers )
38
+ {
39
+ headerDict [ kvp . Key ] = kvp . Value ;
40
+ }
41
+ dict [ "headers" ] = headerDict ;
42
+ }
43
+ else
32
44
{
33
- expando . cookies . Add ( RpcHttpCookieConverter ( cookie ) ) ;
45
+ dict [ "headers" ] = EmptyHeaders ;
34
46
}
35
47
36
- if ( inputMessage . Body != null )
48
+ if ( inputMessage . Cookies is { Count : > 0 } )
37
49
{
38
- expando . body = inputMessage . Body . ToObject ( ) ;
50
+ var cookiesList = new List < Tuple < string , string , CookieOptions > > ( inputMessage . Cookies . Count ) ;
51
+ foreach ( var cookie in inputMessage . Cookies )
52
+ {
53
+ cookiesList . Add ( RpcHttpCookieConverter ( cookie ) ) ;
54
+ }
55
+ dict [ "cookies" ] = cookiesList ;
39
56
}
57
+ else
58
+ {
59
+ dict [ "cookies" ] = Array . Empty < Tuple < string , string , CookieOptions > > ( ) ;
60
+ }
61
+
62
+ if ( inputMessage . Body is not null )
63
+ {
64
+ dict [ "body" ] = inputMessage . Body . ToObject ( ) ;
65
+ }
66
+
40
67
return expando ;
41
68
}
42
69
@@ -80,27 +107,17 @@ public static Tuple<string, string, CookieOptions> RpcHttpCookieConverter(RpcHtt
80
107
81
108
internal static void UpdateWorkerMetadata ( this WorkerMetadata workerMetadata , RpcWorkerConfig workerConfig )
82
109
{
83
- workerMetadata . RuntimeName = string . IsNullOrEmpty ( workerMetadata . RuntimeName )
84
- ? workerConfig . Description . Language : workerMetadata . RuntimeName ;
85
- workerMetadata . RuntimeVersion = string . IsNullOrEmpty ( workerMetadata . RuntimeVersion )
86
- ? workerConfig . Description . DefaultRuntimeVersion : workerMetadata . RuntimeVersion ;
110
+ workerMetadata . RuntimeName ??= workerConfig . Description . Language ;
111
+ workerMetadata . RuntimeVersion ??= workerConfig . Description . DefaultRuntimeVersion ;
87
112
}
88
113
89
- private static SameSiteMode RpcSameSiteEnumConverter ( RpcHttpCookie . Types . SameSite sameSite )
114
+ private static SameSiteMode RpcSameSiteEnumConverter ( RpcHttpCookie . Types . SameSite sameSite ) => sameSite switch
90
115
{
91
- switch ( sameSite )
92
- {
93
- case RpcHttpCookie . Types . SameSite . Strict :
94
- return SameSiteMode . Strict ;
95
- case RpcHttpCookie . Types . SameSite . Lax :
96
- return SameSiteMode . Lax ;
97
- case RpcHttpCookie . Types . SameSite . None :
98
- return SameSiteMode . Unspecified ;
99
- case RpcHttpCookie . Types . SameSite . ExplicitNone :
100
- return SameSiteMode . None ;
101
- default :
102
- return SameSiteMode . Unspecified ;
103
- }
104
- }
116
+ RpcHttpCookie . Types . SameSite . Strict => SameSiteMode . Strict ,
117
+ RpcHttpCookie . Types . SameSite . Lax => SameSiteMode . Lax ,
118
+ RpcHttpCookie . Types . SameSite . None => SameSiteMode . Unspecified ,
119
+ RpcHttpCookie . Types . SameSite . ExplicitNone => SameSiteMode . None ,
120
+ _ => SameSiteMode . Unspecified
121
+ } ;
105
122
}
106
- }
123
+ }
0 commit comments