-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDefaultCommentParser.cs
More file actions
842 lines (756 loc) · 34.7 KB
/
Copy pathDefaultCommentParser.cs
File metadata and controls
842 lines (756 loc) · 34.7 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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
using System.Text.RegularExpressions;
namespace NpgsqlRest.Defaults;
internal static partial class DefaultCommentParser
{
private static readonly char[] NewlineSeparator = ['\r', '\n'];
// All annotation keys moved to their respective handler files in CommentParsers directory
public static RoutineEndpoint? Parse(
Routine routine,
RoutineEndpoint routineEndpoint)
{
if (Options.CommentsMode == CommentsMode.Ignore)
{
return routineEndpoint;
}
var originalParamType = routineEndpoint.RequestParamType;
var comment = routine.Comment;
var disabled = false;
bool haveTag = true;
if (string.IsNullOrEmpty(comment))
{
if (Options.CommentsMode is CommentsMode.OnlyWithHttpTag or CommentsMode.OnlyAnnotated)
{
return null;
}
}
else
{
var routineDescription = routine.Type == RoutineType.SqlFile
? routine.SimpleDefinition
: string.Concat(routine.Type, " ", routine.Schema, ".", routine.Name);
var urlDescription = string.Concat(routineEndpoint.Method.ToString(), " ", routineEndpoint.Path);
var description = string.Concat(routineDescription, " mapped to ", urlDescription);
_annotationLabels = new List<string>();
string[] lines = comment.Split(NewlineSeparator, StringSplitOptions.RemoveEmptyEntries);
routineEndpoint.CommentWordLines = new string[lines.Length][];
bool hasHttpTag = false;
// True when a plugin handler claims a line with RequestsEndpoint=true (exposure intent) —
// under OnlyWithHttpTag/OnlyAnnotated this creates the endpoint even with no HTTP tag.
bool anyHandlerRequestedEndpoint = false;
// Accumulates comment lines that are NOT recognized as built-in directives. Exposed via
// RoutineEndpoint.UnhandledCommentLines for plugins (e.g. MCP) to parse their own
// annotations and/or derive a human description. Core attaches no meaning to them.
List<string>? unhandledCommentLines = null;
for (var i = 0; i < lines.Length; i++)
{
string line = lines[i].Trim();
string[] wordsLower = line.SplitWordsLower();
string[] words = line.SplitWords();
routineEndpoint.CommentWordLines[i] = wordsLower;
var len = wordsLower.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(wordsLower[0], TagsKey))
{
HandleTags(routine, wordsLower, ref haveTag);
}
// key = value
// If key matches a real parameter name → resolved parameter (SQL expression)
// Otherwise → custom parameter
else if (haveTag is true && SplitBySeparatorChar(line, Consts.Equal, out var customParamName, out var customParamValue))
{
if (routine.OriginalParamsHash.Contains(customParamName))
{
HandleResolvedParameter(routineEndpoint, customParamName, customParamValue, description);
}
else
{
HandleCustomParameter(routineEndpoint, customParamName, customParamValue, description);
WarnUnknownPlaceholders(routine, customParamValue, description);
}
TrackAnnotation(line);
}
// key: value
// Content-Type: application/json
else if (haveTag is true && SplitBySeparatorChar(line, Consts.Colon, out var headerName, out var headerValue))
{
HandleHeader(routineEndpoint, headerName, headerValue, description);
WarnUnknownPlaceholders(routine, headerValue, description);
TrackAnnotation(line);
}
// disabled
// disabled tag1, tag2, tag3 [, ...]
else if (haveTag is true && StrEquals(wordsLower[0], DisabledKey))
{
HandleDisabled(routine, wordsLower, len, ref disabled);
TrackAnnotation(line);
}
// enabled
// enabled [ tag1, tag2, tag3 [, ...] ]
else if (haveTag is true && StrEquals(wordsLower[0], EnabledKey))
{
HandleEnabled(routine, wordsLower, len, ref disabled);
TrackAnnotation(line);
}
// internal
// internal_only
else if (haveTag is true && StrEqualsToArray(wordsLower[0], InternalKey))
{
routineEndpoint.InternalOnly = true;
CommentLogger?.LogTrace("Endpoint {Description} marked as internal-only", description);
TrackAnnotation(line);
}
// HTTP
// HTTP [ GET | POST | PUT | DELETE ]
// HTTP [ GET | POST | PUT | DELETE ] path
// HTTP path
else if (haveTag is true && StrEquals(wordsLower[0], HttpKey))
{
HandleHttp(routineEndpoint, wordsLower, len, description, ref urlDescription, ref description, routineDescription, ref hasHttpTag);
TrackAnnotation(line);
}
// PATH path
else if (haveTag is true && StrEquals(wordsLower[0], PathKey))
{
HandlePath(routineEndpoint, wordsLower, description, ref urlDescription, ref description, routineDescription);
TrackAnnotation(line);
}
// request_param_type [ [ query_string | query ] | [ body_json | body ] ]
// param_type [ [ query_string | query ] | [ body_json | body ] ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], ParamTypeKey))
{
HandleParamType(routineEndpoint, wordsLower, description, originalParamType);
TrackAnnotation(line);
}
// authorize
// requires_authorization
// authorize [ role1, role2, role3 [, ...] ]
// requires_authorization [ role1, role2, role3 [, ...] ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], AuthorizeKey))
{
HandleAuthorize(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// allow_anonymous
// anonymous
// allow_anon
// anon
else if (haveTag is true && StrEqualsToArray(wordsLower[0], AllowAnonymousKey))
{
HandleAllowAnonymous(routineEndpoint, description);
TrackAnnotation(line);
}
// command_timeout interval
// timeout interval
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], TimeoutKey))
{
HandleTimeout(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// request_headers_mode [ ignore | context | parameter ]
// request_headers [ ignore | context | parameter ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], RequestHeadersModeKey))
{
HandleRequestHeadersMode(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// request_headers_parameter_name name
// request_headers_param_name name
else if (haveTag is true && StrEqualsToArray(wordsLower[0], RequestHeadersParameterNameKey))
{
HandleRequestHeadersParameterName(routineEndpoint, wordsLower, len, description);
TrackAnnotation(line);
}
// body_parameter_name name
// body_param_name name
else if (haveTag is true && StrEqualsToArray(wordsLower[0], BodyParameterNameKey))
{
HandleBodyParameterName(routineEndpoint, wordsLower, len, description);
TrackAnnotation(line);
}
// response_null_handling [ empty_string | empty | null_literal | null | no_content | 204 | 204_no_content ]
// response_null [ empty_string | empty | null_literal | null | no_content | 204 | 204_no_content ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], TextResponseNullHandlingKey))
{
HandleResponseNullHandling(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// query_string_null_handling [ empty_string | empty | null_literal | null | ignore ]
// query_null_handling [ empty_string | empty |null_literal | null | ignore ]
// query_string_null [ empty_string | empty |null_literal | null | ignore ]
// query_null [ empty_string | empty | null_literal | null | ignore ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], QueryStringNullHandlingKey))
{
HandleQueryStringNullHandling(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// login
// signin
else if (haveTag is true && StrEqualsToArray(wordsLower[0], LoginKey))
{
HandleLogin(routineEndpoint, description);
TrackAnnotation(line);
}
// logout
// signout
else if (haveTag is true && StrEqualsToArray(wordsLower[0], LogoutKey))
{
HandleLogout(routineEndpoint, description);
TrackAnnotation(line);
}
// buffer_rows number
// buffer number
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], BufferRowsKey))
{
HandleBufferRows(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// raw
// raw_mode
// raw_results
else if (haveTag is true && StrEqualsToArray(wordsLower[0], RawKey))
{
HandleRaw(routineEndpoint, description);
TrackAnnotation(line);
}
// single
// single_record
else if (haveTag is true && StrEqualsToArray(wordsLower[0], SingleKey))
{
HandleSingle(routineEndpoint, description);
TrackAnnotation(line);
}
// void
// void_result
else if (haveTag is true && StrEqualsToArray(wordsLower[0], VoidKey))
{
HandleVoid(routineEndpoint, description);
TrackAnnotation(line);
}
// separator [ value ]
// raw_separator [ value ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], SeparatorKey))
{
HandleSeparator(routineEndpoint, line, wordsLower, description);
TrackAnnotation(line);
}
// new_line [ value ]
// raw_new_line [ value ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], NewLineKey))
{
HandleNewLine(routineEndpoint, line, wordsLower, description);
TrackAnnotation(line);
}
// columns
// names
// column_names
else if (haveTag is true && StrEqualsToArray(wordsLower[0], ColumnNamesKey))
{
HandleColumnNames(routineEndpoint, description);
TrackAnnotation(line);
}
// sensitive
// security_sensitive
else if (haveTag is true && StrEqualsToArray(wordsLower[0], SecuritySensitiveKey))
{
HandleSecuritySensitive(routineEndpoint, description);
TrackAnnotation(line);
}
// user_context
else if (haveTag is true && StrEqualsToArray(wordsLower[0], UserContextKey))
{
HandleUserContext(routineEndpoint, description);
TrackAnnotation(line);
}
// user_parameters
// user_params
else if (haveTag is true && StrEqualsToArray(wordsLower[0], UserParemetersKey))
{
HandleUserParameters(routineEndpoint, description);
TrackAnnotation(line);
}
// cached
// cached [ param1, param2, param3 [, ...] ]
else if (haveTag is true && StrEquals(wordsLower[0], CacheKey))
{
HandleCached(routine, routineEndpoint, words, len, description);
TrackAnnotation(line);
}
// cache_expires [ value ]
// cache_expires_in [ value ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], CacheExpiresInKey))
{
HandleCacheExpiresIn(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// cache_profile <name>
else if (haveTag is true && StrEquals(wordsLower[0], CacheProfileKey))
{
HandleCacheProfile(routineEndpoint, words, len, description);
TrackAnnotation(line);
}
// connection
// connection_name
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], ConnectionNameKey))
{
HandleConnectionName(routineEndpoint, wordsLower, description);
TrackAnnotation(line);
}
// upload
// upload for handler_name1, handler_name2 [, ...]
// upload param_name as metadata
else if (haveTag is true && StrEquals(wordsLower[0], UploadKey))
{
HandleUpload(routine, routineEndpoint, wordsLower, len, description);
TrackAnnotation(line);
}
// param param_name1 is hash of param_name2
// param param_name is upload metadata
// param [original] [new_name] [type]
// param [original] is [new_name] [type is [type]]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], ParameterKey))
{
HandleParameter(routine, routineEndpoint, wordsLower, words, len, description);
TrackAnnotation(line);
}
// sse path [ path ] [ on info | notice | warning ]
// sse_path [ path ] [ on info | notice | warning ]
// sse_events_path [ path ] [ on info | notice | warning ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], SseEventsStreamingPathKey))
{
HandleSseEventsPath(routineEndpoint, wordsLower, words, len, description);
TrackAnnotation(line);
}
// sse_publish [ on info | notice | warning ]
// sse_events_publish [ on info | notice | warning ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], SseEventsPublishKey))
{
HandleSseEventsPublish(routineEndpoint, wordsLower, words, len, description);
TrackAnnotation(line);
}
// sse_subscribe [ path ] [ on info | notice | warning ]
// sse_events_subscribe [ path ] [ on info | notice | warning ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], SseEventsSubscribeKey))
{
HandleSseEventsSubscribe(routineEndpoint, wordsLower, words, len, description);
TrackAnnotation(line);
}
// sse_level [ info | notice | warning ]
// sse_events_level [ info | notice | warning ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], SseEventsLevelKey))
{
HandleSseEventsLevel(routineEndpoint, wordsLower, words, line, description);
TrackAnnotation(line);
}
// sse_scope [ [ matching | authorize | all ] | [ authorize [ role_or_user1, role_or_user1, role_or_user1 [, ...] ] ] ]
// sse_events_scope [ [ matching | authorize | all ] | [ authorize [ role_or_user1, role_or_user1, role_or_user1 [, ...] ] ] ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], SseEventsStreamingScopeKey))
{
HandleSseEventsScope(routineEndpoint, wordsLower, line, description);
TrackAnnotation(line);
}
// basic_authentication [ username ] [ password ]
// basic_auth [ username ] [ password ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], BasicAuthKey))
{
HandleBasicAuth(routineEndpoint, words, len, description);
TrackAnnotation(line);
}
// basic_authentication_realm [ realm ]
// basic_auth_realm [ realm ]
// realm [ realm ]
else if (haveTag is true && len > 1 && StrEqualsToArray(wordsLower[0], BasicAuthRealmKey))
{
HandleBasicAuthRealm(routineEndpoint, words, description);
TrackAnnotation(line);
}
// basic_authentication_command [ command ]
// basic_auth_command [ command ]
// challenge_command [ command ]
else if (haveTag is true && len > 1 && StrEqualsToArray(words[0], BasicAuthCommandKey))
{
HandleBasicAuthCommand(routineEndpoint, words, line, description);
TrackAnnotation(line);
}
// retry_strategy_name [ name ]
// retry_strategy [ name ]
// retry [ name ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], RetryStrategyKey))
{
HandleRetryStrategy(routineEndpoint, words, description);
TrackAnnotation(line);
}
// rate_limiter_policy_name [ name ]
// rate_limiter_policy [ name ]
// rate_limiter [ name ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], RateLimiterPolicyKey))
{
HandleRateLimiterPolicy(routineEndpoint, words, description);
TrackAnnotation(line);
}
// error_code_policy_name [ name ]
// error_code_policy [ name ]
// error_code [ name ]
else if (haveTag is true && len >= 2 && StrEqualsToArray(wordsLower[0], ErrorCodePolicyKey))
{
HandleErrorCodePolicy(routineEndpoint, words, description);
TrackAnnotation(line);
}
// validate _param_name using rule_name
// validation _param_name using rule_name
else if (haveTag is true && len >= 4 && StrEqualsToArray(wordsLower[0], ValidateKey))
{
HandleValidate(routine, routineEndpoint, words, len, description);
TrackAnnotation(line);
}
// proxy
// proxy [ GET | POST | PUT | DELETE | PATCH ]
// proxy host_url
// proxy [ GET | POST | PUT | DELETE | PATCH ] host_url
// reverse_proxy [ ... ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], ProxyKey))
{
HandleProxy(routine, routineEndpoint, wordsLower, words, len, description);
TrackAnnotation(line);
}
// proxy_out [ GET | POST | PUT | DELETE | PATCH ] [ host_url ] [ ?query={param} ]
// forward_proxy [ ... ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], ProxyOutKey))
{
HandleProxyOut(routine, routineEndpoint, wordsLower, words, len, description);
TrackAnnotation(line);
}
// encrypt
// encrypt [ param1, param2, ... ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], EncryptKey))
{
HandleEncrypt(routine, routineEndpoint, wordsLower, len, description);
TrackAnnotation(line);
}
// decrypt
// decrypt [ col1, col2, ... ]
else if (haveTag is true && StrEqualsToArray(wordsLower[0], DecryptKey))
{
HandleDecrypt(routineEndpoint, wordsLower, len, description);
TrackAnnotation(line);
}
// nested
// nested_json
// nested_composite
else if (haveTag is true && StrEqualsToArray(wordsLower[0], NestedJsonKey))
{
HandleNestedJson(routineEndpoint, description);
TrackAnnotation(line);
}
else
{
// Not a built-in directive — offer it to plugin comment handlers in a single
// pass (no second iteration / re-tokenization per plugin). First handler to
// claim it (non-null label) wins; core logs it centrally, consistent with
// built-in annotation logging. If no handler claims it, it's prose → surfaced
// via RoutineEndpoint.UnhandledCommentLines.
CommentLineResult? handled = null;
foreach (var handler in Options.EndpointCreateHandlers)
{
handled = handler.HandleCommentLine(routineEndpoint, line, words, wordsLower);
if (handled is not null)
{
break;
}
}
if (handled is { } result)
{
CommentLogger?.LogTrace("Plugin comment annotation '{Label}' for {Description}", result.Label, description);
// Include plugin-claimed annotations (e.g. `mcp`, `openapi …`) in the per-endpoint
// annotations summary, so they're as visible as built-in annotations.
TrackAnnotation(line);
if (result.RequestsEndpoint)
{
anyHandlerRequestedEndpoint = true;
}
}
else
{
(unhandledCommentLines ??= []).Add(line);
}
}
}
routineEndpoint.UnhandledCommentLines = unhandledCommentLines?.ToArray();
if (disabled)
{
Logger?.CommentDisabled(description);
return null;
}
if (Options.CommentsMode is CommentsMode.OnlyWithHttpTag or CommentsMode.OnlyAnnotated
&& !hasHttpTag && !anyHandlerRequestedEndpoint)
{
return null;
}
// An endpoint that exists ONLY because a plugin requested it (no HTTP tag under a gating
// mode) gets no public HTTP route: the plugin asked for a projection (e.g. an MCP tool),
// not a route — `mcp` alone must not silently widen the HTTP surface. An explicit HTTP tag
// opts into dual exposure; `internal` remains the explicit override when an HTTP tag exists.
if (Options.CommentsMode is CommentsMode.OnlyWithHttpTag or CommentsMode.OnlyAnnotated
&& !hasHttpTag && anyHandlerRequestedEndpoint && routineEndpoint.InternalOnly is false)
{
routineEndpoint.InternalOnly = true;
Logger?.PluginRequestedEndpointInternalOnly(description);
}
// Detect proxy response parameters after all annotations are processed.
// This must run after param renames so that renamed parameters (e.g., $1 → _proxy_body)
// are detected correctly regardless of annotation order.
if (routineEndpoint.IsProxy)
{
DetectProxyResponseParameters(routine, routineEndpoint);
Logger?.LogDebug("Proxy endpoint {Description} HasProxyResponseParameters: {HasParams}",
description, routineEndpoint.HasProxyResponseParameters);
}
// For SQL file routines: update column type descriptors when @param annotations
// retyped parameters. Describe resolves column types before annotations run, so columns
// from "select $1 as col" are typed as text when the parameter type was unknown.
// After "param $1 _name integer" retypes to integer, the column descriptor should match.
if (routine.Type == RoutineType.SqlFile)
{
UpdateColumnDescriptorsFromRetypedParams(routine);
}
if (_annotationLabels is { Count: > 0 })
{
Logger?.CommentAnnotationsSummary(description, string.Join(", ", _annotationLabels));
}
_annotationLabels = null;
}
return routineEndpoint;
}
public static void SetCustomParameter(RoutineEndpoint endpoint, string name, string value)
{
value = Regex.Unescape(value);
if (StrEqualsToArray(name, BufferRowsKey))
{
if (ulong.TryParse(value, out var parsedBuffer))
{
endpoint.BufferRows = parsedBuffer;
}
}
else if (StrEqualsToArray(name, RawKey))
{
if (bool.TryParse(value, out var parsedRaw))
{
endpoint.Raw = parsedRaw;
}
}
else if (StrEqualsToArray(name, SingleKey))
{
if (bool.TryParse(value, out var parsedSingle))
{
endpoint.ReturnSingleRecord = parsedSingle;
}
}
else if (StrEqualsToArray(name, VoidKey))
{
if (bool.TryParse(value, out var parsedVoid))
{
endpoint.Void = parsedVoid;
}
}
else if (StrEqualsToArray(name, SeparatorKey))
{
endpoint.RawValueSeparator = value;
}
else if (StrEqualsToArray(name, NewLineKey))
{
endpoint.RawNewLineSeparator = value;
}
else if (StrEqualsToArray(name, ColumnNamesKey))
{
if (bool.TryParse(value, out var parsedRawColumnNames))
{
endpoint.RawColumnNames = parsedRawColumnNames;
}
}
else if (StrEqualsToArray(name, ConnectionNameKey))
{
//if (Options.ConnectionStrings is not null && options.ConnectionStrings.ContainsKey(value) is true)
//{
// endpoint.ConnectionName = value;
//}
endpoint.ConnectionName = value;
}
else if (StrEqualsToArray(name, UserContextKey))
{
if (bool.TryParse(value, out var parserUserContext))
{
endpoint.UserContext = parserUserContext;
}
}
else if (StrEqualsToArray(name, UserParemetersKey))
{
if (bool.TryParse(value, out var parserUserParameters))
{
endpoint.UseUserParameters = parserUserParameters;
}
}
else if (StrEqualsToArray(name, SseEventsStreamingPathKey))
{
// Tag-value form of @sse: same shorthand semantics — publish + subscribe on the same path.
if (bool.TryParse(value, out var parseredStreamingPath))
{
endpoint.SseEventsPath = parseredStreamingPath is true ?
(endpoint.SseEventNoticeLevel ?? Options.DefaultSseEventNoticeLevel).ToString().ToLowerInvariant() :
null;
endpoint.SsePublishEnabled = parseredStreamingPath;
}
else
{
endpoint.SseEventsPath = value;
endpoint.SsePublishEnabled = true;
}
}
else if (StrEqualsToArray(name, SseEventsPublishKey))
{
if (bool.TryParse(value, out var parsedPublish))
{
endpoint.SsePublishEnabled = parsedPublish;
}
else
{
endpoint.SsePublishEnabled = true;
}
}
else if (StrEqualsToArray(name, SseEventsSubscribeKey))
{
if (bool.TryParse(value, out var parsedSubscribe))
{
endpoint.SseEventsPath = parsedSubscribe is true ?
(endpoint.SseEventNoticeLevel ?? Options.DefaultSseEventNoticeLevel).ToString().ToLowerInvariant() :
null;
}
else
{
endpoint.SseEventsPath = value;
}
}
else if (StrEqualsToArray(name, SseEventsLevelKey))
{
if (Enum.TryParse<PostgresNoticeLevels>(value, true, out var parsedLevel))
{
endpoint.SseEventNoticeLevel = parsedLevel;
}
}
else if (StrEqualsToArray(name, SseEventsStreamingScopeKey))
{
var words = value.SplitWords();
if (words.Length > 0 && Enum.TryParse<SseEventsScope>(words[0], true, out var parsedScope))
{
endpoint.SseEventsScope = parsedScope;
if (parsedScope == SseEventsScope.Authorize && words.Length > 1)
{
endpoint.SseEventsRoles ??= new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var word in words[1..])
{
if (string.IsNullOrWhiteSpace(word) is false)
{
endpoint.SseEventsRoles.Add(word);
}
}
}
}
else
{
Logger?.LogError("Could not recognize valid value for parameter key {key}. Valid values are: {values}. Provided value is {provided}.",
name, string.Join(", ", Enum.GetNames<SseEventsScope>()), value);
}
}
else if (StrEqualsToArray(name, BasicAuthKey))
{
if (endpoint.BasicAuth is null)
{
endpoint.BasicAuth = new() { Enabled = true };
}
var words = value.SplitWords();
if (words.Length >= 3)
{
var username = words[1];
var password = words[2];
if (string.IsNullOrEmpty(username) is false && string.IsNullOrEmpty(password) is false)
{
endpoint.BasicAuth.Users[username] = password;
}
}
}
else if (StrEqualsToArray(name, BasicAuthRealmKey))
{
if (endpoint.BasicAuth is null)
{
endpoint.BasicAuth = new() { Enabled = true };
}
endpoint.BasicAuth.Realm = value;
}
else if (StrEqualsToArray(name, BasicAuthCommandKey))
{
if (endpoint.BasicAuth is null)
{
endpoint.BasicAuth = new() { Enabled = true };
}
endpoint.BasicAuth.ChallengeCommand = value;
}
else if (StrEqualsToArray(name, RetryStrategyKey))
{
if (Options.CommandRetryOptions.Strategies.TryGetValue(value, out var strategy))
{
endpoint.RetryStrategy = strategy;
}
}
else if (StrEqualsToArray(name, RateLimiterPolicyKey))
{
endpoint.RateLimiterPolicy = value;
}
else if (StrEqualsToArray(name, ErrorCodePolicyKey))
{
endpoint.ErrorCodePolicy = value;
}
else if (StrEqualsToArray(name, EncryptKey))
{
if (bool.TryParse(value, out var parsedEncrypt))
{
endpoint.EncryptAllParameters = parsedEncrypt;
}
}
else if (StrEqualsToArray(name, DecryptKey))
{
if (bool.TryParse(value, out var parsedDecrypt))
{
endpoint.DecryptAllColumns = parsedDecrypt;
}
}
else if (StrEqualsToArray(name, TimeoutKey))
{
var parsedInterval = Parser.ParsePostgresInterval(value);
if (parsedInterval is not null)
{
endpoint.CommandTimeout = parsedInterval;
}
}
else
{
// Strip @ prefix from custom parameter names for consistency
var paramName = name.Length > 0 && name[0] == '@' ? name[1..] : name;
if (endpoint.CustomParameters is null)
{
endpoint.CustomParameters = new()
{
[paramName] = value
};
}
else
{
endpoint.CustomParameters[paramName] = value;
}
}
}
// Utility methods moved to DefaultCommentParser.Utilities.cs
}