-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDefaultCommentParser.cs
More file actions
616 lines (581 loc) · 25.6 KB
/
Copy pathDefaultCommentParser.cs
File metadata and controls
616 lines (581 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
using Microsoft.Extensions.Primitives;
namespace NpgsqlRest.Defaults;
internal static class DefaultCommentParser
{
private static readonly char[] newlineSeparator = ['\r', '\n'];
private static readonly char[] wordSeparators = [' ', ','];
private static readonly char[] headerSeparator = [':'];
private const string HttpKey = "http";
private static readonly string[] paramTypeKey = [
"requestparamtype",
"paramtype",
"request_param_type",
"param_type",
"request-param-type",
"param-type"
];
private static readonly string[] queryKey = [
"querystring",
"query_string",
"query-string",
"query"
];
private static readonly string[] jsonKey = [
"bodyjson",
"body_json",
"body-json",
"json",
"body"
];
private static readonly string[] authorizeKey = [
"requiresauthorization",
"authorize",
"requires_authorization",
"requires-authorization"
];
private static readonly string[] allowAnonymousKey = [
"allowanonymous",
"allow_anonymous",
"allow-anonymous",
"anonymous",
"anon"
];
private static readonly string[] timeoutKey = [
"commandtimeout",
"command_timeout",
"command-timeout",
"timeout"
];
private const string ContentTypeKey = "content-type";
private static readonly string[] requestHeadersModeKey = [
"requestheadersmode",
"request_headers_mode",
"request-headers-mode",
"requestheaders",
"request_headers",
"request-headers"
];
private const string IgnoreKey = "ignore";
private const string ContextKey = "context";
private const string ParameterKey = "parameter";
private static readonly string[] requestHeadersParameterNameKey = [
"requestheadersparametername",
"requestheadersparamname",
"request_headers_parameter_name",
"request_headers_param_name",
"request-headers-parameter-name",
"request-headers-param-name",
];
private static readonly string[] bodyParameterNameKey = [
"bodyparametername",
"body-parameter-name",
"body_parameter_name",
"bodyparamname",
"body-param-name",
"body_param_name"
];
private static readonly string[] tagsKey = ["for", "tags", "tag"];
private const string DisabledKey = "disabled";
private const string EnabledKey = "enabled";
private static readonly string[] textResponseNullHandlingKey = [
"responsenullhandling",
"response_null_handling",
"response-null-handling",
];
private const string EmptyStringKey = "emptystring";
private const string NullLiteral = "nullliteral";
private const string NoContentKey = "nocontent";
private static readonly string[] queryStringNullHandlingKey = [
"querystringnullhandling",
"query-string-null-handling",
"query_string_null_handling",
];
private static readonly string[] LoginKey = [
"login",
"signin",
];
private static readonly string[] LogoutKey = [
"logout",
"signout",
];
private static readonly string[] bufferRowsKey = [
"bufferrows",
"buffer_rows",
"buffer-rows",
"buffer"
];
public static RoutineEndpoint? Parse(ref Routine routine, ref NpgsqlRestOptions options, ref ILogger? logger, ref RoutineEndpoint routineEndpoint)
{
if (options.CommentsMode == CommentsMode.Ignore)
{
return routineEndpoint;
}
var originalUrl = routineEndpoint.Url;
var originalMethod = routineEndpoint.Method;
var originalParamType = routineEndpoint.RequestParamType;
var comment = routine.Comment;
var disabled = false;
bool haveTag = true;
if (string.IsNullOrEmpty(comment))
{
if (options.CommentsMode == CommentsMode.OnlyWithHttpTag)
{
return null;
}
}
else
{
string[] lines = comment.Split(newlineSeparator, StringSplitOptions.RemoveEmptyEntries);
bool hasHttpTag = false;
for (var i = 0; i < lines.Length; i++)
{
string line = lines[i].Trim();
string[] words = Split(ref line);
var len = words.Length;
if (len == 0)
{
continue;
}
// for tag1, tag2, tag3 [, ...]
// tag tag1, tag2, tag3 [, ...]
// tags tag1, tag2, tag3 [, ...]
if (routine.Tags is not null && routine.Tags.Length > 0 && StrEqualsToArray(ref words[0], tagsKey))
{
string[] arr = words[1..];
bool found = false;
for (var j = 0; j < routine.Tags.Length; j++)
{
var tag = routine.Tags[j];
if (StrEqualsToArray(ref tag, arr))
{
found = true;
break;
}
}
haveTag = found;
}
// disabled
// disabled tag1, tag2, tag3 [, ...]
else if (haveTag is true && StrEquals(ref words[0], DisabledKey))
{
if (len == 1)
{
disabled = true;
}
else if (routine.Tags is not null && routine.Tags.Length > 0)
{
string[] arr = words[1..];
for (var j = 0; j < routine.Tags.Length; j++)
{
var tag = routine.Tags[j];
if (StrEqualsToArray(ref tag, arr))
{
disabled = true;
break;
}
}
}
}
// enabled
// enabled [ tag1, tag2, tag3 [, ...] ]
else if (haveTag is true && StrEquals(ref words[0], EnabledKey))
{
if (len == 1)
{
disabled = false;
}
else if (routine.Tags is not null && routine.Tags.Length > 0)
{
string[] arr = words[1..];
for (var j = 0; j < routine.Tags.Length; j++)
{
var tag = routine.Tags[j];
if (StrEqualsToArray(ref tag, arr))
{
disabled = false;
break;
}
}
}
}
// HTTP
// HTTP [ GET | POST | PUT | DELETE ]
// HTTP [ GET | POST | PUT | DELETE ] path
else if (haveTag is true && StrEquals(ref words[0], HttpKey))
{
hasHttpTag = true;
if (len == 2 || len == 3)
{
if (Enum.TryParse<Method>(words[1], true, out var parsedMethod))
{
routineEndpoint.Method = parsedMethod;
routineEndpoint.RequestParamType = routineEndpoint.Method == Method.GET ? RequestParamType.QueryString : RequestParamType.BodyJson;
}
else
{
logger?.InvalidHttpMethodComment(words[1], routine.Schema, routine.Name, routineEndpoint.Method);
}
}
if (len == 3)
{
string urlPathSegment = words[2];
if (!Uri.TryCreate(urlPathSegment, UriKind.Relative, out Uri? uri))
{
logger?.InvalidUrlPathSegmentComment(urlPathSegment, routine.Schema, routine.Name, routineEndpoint.Url);
}
else
{
routineEndpoint.Url = uri.ToString();
if (!routineEndpoint.Url.StartsWith('/'))
{
routineEndpoint.Url = string.Concat("/", routineEndpoint.Url);
}
}
}
if (routineEndpoint.Method != originalMethod || !string.Equals(routineEndpoint.Url, originalUrl))
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetHttp(routine.Type, routine.Schema, routine.Name, routineEndpoint.Method, routineEndpoint.Url);
}
}
}
// requestparamtype [ [ querystring | query_string | query-string | query ] | [ bodyjson | body_json | body-json | json | body ] ]
// paramtype [ [ querystring | query_string | query-string | query ] | [ bodyjson | body_json | body-json | json | body ] ]
// request_param_type [ [ querystring | query_string | query-string | query ] | [ bodyjson | body_json | body-json | json | body ] ]
// param_type [ [ querystring | query_string | query-string | query ] | [ bodyjson | body_json | body-json | json | body ] ]
// request-param-type [ [ querystring | query_string | query-string | query ] | [ bodyjson | body_json | body-json | json | body ] ]
// param-type [ [ querystring | query_string | query-string | query ] | [ bodyjson | body_json | body-json | json | body ] ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(ref words[0], paramTypeKey))
{
if (StrEqualsToArray(ref words[1], queryKey))
{
routineEndpoint.RequestParamType = RequestParamType.QueryString;
}
else if (StrEqualsToArray(ref words[1], jsonKey))
{
routineEndpoint.RequestParamType = RequestParamType.BodyJson;
}
else
{
logger?.InvalidParameterTypeComment(words[1], routine.Schema, routine.Name, routineEndpoint.RequestParamType);
}
if (originalParamType != routineEndpoint.RequestParamType)
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetParameterType(routine.Type, routine.Schema, routine.Name, routineEndpoint.RequestParamType);
}
}
}
// requiresauthorization
// authorize
// requires_authorization
// requires-authorization
else if (haveTag is true && StrEqualsToArray(ref words[0], authorizeKey))
{
routineEndpoint.RequiresAuthorization = true;
if (words.Length > 1)
{
routineEndpoint.AuthorizeRoles = new(words[1..]);
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetAuthRoles(routine.Type, routine.Schema, routine.Name, routineEndpoint.AuthorizeRoles);
}
} else
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetAuth(routine.Type, routine.Schema, routine.Name);
}
}
}
// allowanonymous
// allow_anonymous
// allow-anonymous
// anonymous
// anon
else if (haveTag is true && StrEqualsToArray(ref words[0], allowAnonymousKey))
{
routineEndpoint.RequiresAuthorization = false;
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetAnon(routine.Type, routine.Schema, routine.Name);
}
}
// commandtimeout seconds
// command_timeout seconds
// command-timeout seconds
// timeout seconds
else if (haveTag is true && len >= 2 && StrEqualsToArray(ref words[0], timeoutKey))
{
if (int.TryParse(words[1], out var parsedTimeout))
{
if (routineEndpoint.CommandTimeout != parsedTimeout)
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetTimeout(routine.Type, routine.Schema, routine.Name, words[1]);
}
}
routineEndpoint.CommandTimeout = parsedTimeout;
}
else
{
logger?.InvalidTimeoutComment(words[1], routine.Schema, routine.Name, routineEndpoint.CommandTimeout);
}
}
// requestheadersmode [ ignore | context | parameter ]
// request_headers_mode [ ignore | context | parameter ]
// request-headers-mode [ ignore | context | parameter ]
// requestheaders [ ignore | context | parameter ]
// request_headers [ ignore | context | parameter ]
// request-headers [ ignore | context | parameter ]
else if (haveTag is true && StrEqualsToArray(ref words[0], requestHeadersModeKey))
{
if (StrEquals(ref words[1], IgnoreKey))
{
routineEndpoint.RequestHeadersMode = RequestHeadersMode.Ignore;
}
else if (StrEquals(ref words[1], ContextKey))
{
routineEndpoint.RequestHeadersMode = RequestHeadersMode.Context;
}
else if (StrEquals(ref words[1], ParameterKey))
{
routineEndpoint.RequestHeadersMode = RequestHeadersMode.Parameter;
}
else
{
logger?.InvalidRequestHeadersModeComment(words[1], routine.Schema, routine.Name, routineEndpoint.RequestHeadersMode);
}
if (routineEndpoint.RequestHeadersMode != options.RequestHeadersMode)
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetRequestHeadersMode(routine.Type, routine.Schema, routine.Name, words[1]);
}
}
}
// requestheadersparametername name
// requestheadersparamname name
// request_headers_parameter_name name
// request_headers_param_name name
// request-headers-parameter-name name
// request-headers-param-name name
else if (haveTag is true && StrEqualsToArray(ref words[0], requestHeadersParameterNameKey))
{
if (len == 2)
{
if (!string.Equals(routineEndpoint.RequestHeadersParameterName, words[1]))
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetRequestHeadersParamName(routine.Type, routine.Schema, routine.Name, words[1]);
}
}
routineEndpoint.RequestHeadersParameterName = words[1];
}
}
// bodyparametername name
// body-parameter-name name
// body_parameter_name name
// bodyparamname name
// body-param-name name
// body_param_name name
else if (haveTag is true && StrEqualsToArray(ref words[0], bodyParameterNameKey))
{
if (len == 2)
{
if (!string.Equals(routineEndpoint.BodyParameterName, words[1]))
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetBodyParamName(routine.Type, routine.Schema, routine.Name, words[1]);
}
}
routineEndpoint.BodyParameterName = words[1];
}
}
// responsenullhandling [ emptystring | nullliteral | nocontent ]
// response_null_handling [ emptystring | nullliteral | nocontent ]
// response-null-handling [ emptystring | nullliteral | nocontent ]
else if (haveTag is true && StrEqualsToArray(ref words[0], textResponseNullHandlingKey))
{
if (StrEquals(ref words[1], EmptyStringKey))
{
routineEndpoint.TextResponseNullHandling = TextResponseNullHandling.EmptyString;
}
else if (StrEquals(ref words[1], NullLiteral))
{
routineEndpoint.TextResponseNullHandling = TextResponseNullHandling.NullLiteral;
}
else if (StrEquals(ref words[1], NoContentKey))
{
routineEndpoint.TextResponseNullHandling = TextResponseNullHandling.NoContent;
}
else
{
logger?.InvalidResponseNullHandlingModeComment(words[1], routine.Schema, routine.Name, routineEndpoint.TextResponseNullHandling);
}
if (routineEndpoint.TextResponseNullHandling != options.TextResponseNullHandling)
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetTextResponseNullHandling(routine.Type, routine.Schema, routine.Name, words[1]);
}
}
}
// querystringnullhandling [ emptystring | nullliteral | ignore ]
// query_string_null_handling [ emptystring | nullliteral | ignore ]
// query-string-null-handling [ emptystring | nullliteral | ignore ]
else if (haveTag is true && StrEqualsToArray(ref words[0], queryStringNullHandlingKey))
{
if (StrEquals(ref words[1], EmptyStringKey))
{
routineEndpoint.QueryStringNullHandling = QueryStringNullHandling.EmptyString;
}
else if (StrEquals(ref words[1], NullLiteral))
{
routineEndpoint.QueryStringNullHandling = QueryStringNullHandling.NullLiteral;
}
else if (StrEquals(ref words[1], IgnoreKey))
{
routineEndpoint.QueryStringNullHandling = QueryStringNullHandling.Ignore;
}
else
{
logger?.InvalidQueryStringNullHandlingComment(words[1], routine.Schema, routine.Name, routineEndpoint.QueryStringNullHandling);
}
if (routineEndpoint.TextResponseNullHandling != options.TextResponseNullHandling)
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetQueryStringNullHandling(routine.Type, routine.Schema, routine.Name, routineEndpoint.QueryStringNullHandling);
}
}
}
// login
// signin
else if (haveTag is true && StrEqualsToArray(ref words[0], LoginKey))
{
routineEndpoint.Login = true;
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetLogin(routine.Type, routine.Schema, routine.Name);
}
}
// logout
// signout
else if (haveTag is true && StrEqualsToArray(ref words[0], LogoutKey))
{
routineEndpoint.Logout = true;
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetLogout(routine.Type, routine.Schema, routine.Name);
}
}
// bufferrows number
// buffer_rows number
// buffer-rows number
// buffer number
else if (haveTag is true && len >= 2 && StrEqualsToArray(ref words[0], bufferRowsKey))
{
if (ulong.TryParse(words[1], out var parsedBuffer))
{
if (routineEndpoint.BufferRows != parsedBuffer)
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentBufferRows(routine.Type, routine.Schema, routine.Name, words[1]);
}
}
routineEndpoint.BufferRows = parsedBuffer;
}
else
{
logger?.InvalidBufferRows(words[1], routine.Schema, routine.Name, options.BufferRows);
}
}
// key: value
// Content-Type: application/json
else if (haveTag is true && line.Contains(':'))
{
var parts = line.Split(headerSeparator, 2);
if (parts.Length == 2)
{
var headerName = parts[0].Trim();
var headerValue = parts[1].Trim();
if (StrEquals(ref headerName, ContentTypeKey))
{
if (!string.Equals(routineEndpoint.ResponseContentType, headerValue))
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetContentType(routine.Type, routine.Schema, routine.Name, headerValue);
}
}
routineEndpoint.ResponseContentType = headerValue;
}
else
{
if (routineEndpoint.ResponseHeaders is null)
{
routineEndpoint.ResponseHeaders = new()
{
[headerName] = new StringValues(headerValue)
};
}
else
{
if (routineEndpoint.ResponseHeaders.TryGetValue(headerName, out StringValues values))
{
routineEndpoint.ResponseHeaders[headerName] = StringValues.Concat(values, headerValue);
}
else
{
routineEndpoint.ResponseHeaders.Add(headerName, new StringValues(headerValue));
}
}
if (!string.Equals(routineEndpoint.ResponseContentType, headerValue))
{
if (options.LogAnnotationSetInfo)
{
logger?.CommentSetHeader(routine.Type, routine.Schema, routine.Name, headerName, headerValue);
}
}
}
}
}
}
if (disabled)
{
return null;
}
if (options.CommentsMode == CommentsMode.OnlyWithHttpTag && !hasHttpTag)
{
return null;
}
}
return routineEndpoint;
}
private static bool StrEquals(ref string str1, string str2) => str1.Equals(str2, StringComparison.OrdinalIgnoreCase);
private static bool StrEqualsToArray(ref string str, string[] arr)
{
for (var i = 0; i < arr.Length; i++)
{
if (str.Equals(arr[i], StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private static string[] Split(ref string str)
{
return str
.Split(wordSeparators, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.ToArray();
}
}