| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.RegularExpressions; |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 6 | | using Microsoft.CodeAnalysis.Text; |
| | | 7 | | |
| | | 8 | | namespace TeleFlow.Generators; |
| | | 9 | | |
| | | 10 | | [Generator] |
| | | 11 | | public sealed partial class TelegramHandlerSourceGenerator : IIncrementalGenerator |
| | | 12 | | { |
| | 1 | 13 | | private static readonly SymbolDisplayFormat FullyQualifiedFormat = SymbolDisplayFormat.FullyQualifiedFormat; |
| | | 14 | | |
| | | 15 | | public void Initialize(IncrementalGeneratorInitializationContext context) |
| | | 16 | | { |
| | 28 | 17 | | IncrementalValueProvider<ImmutableArray<GeneratedHandlerMethod?>> methodHandlers = context.SyntaxProvider |
| | 28 | 18 | | .CreateSyntaxProvider( |
| | 2117 | 19 | | static (node, _) => node is MethodDeclarationSyntax, |
| | 52 | 20 | | static (syntaxContext, _) => GetHandlerMethod(syntaxContext)) |
| | 52 | 21 | | .Where(static method => method is not null) |
| | 28 | 22 | | .Collect(); |
| | | 23 | | |
| | 28 | 24 | | IncrementalValueProvider<ImmutableArray<GeneratedHandlerMethod?>> classBasedHandlers = context.SyntaxProvider |
| | 28 | 25 | | .CreateSyntaxProvider( |
| | 2117 | 26 | | static (node, _) => node is TypeDeclarationSyntax, |
| | 51 | 27 | | static (syntaxContext, _) => GetClassBasedHandlerMethod(syntaxContext)) |
| | 51 | 28 | | .Where(static method => method is not null) |
| | 28 | 29 | | .Collect(); |
| | | 30 | | |
| | 28 | 31 | | IncrementalValueProvider<ImmutableArray<GeneratedErrorHandlerMethod?>> errorHandlers = context.SyntaxProvider |
| | 28 | 32 | | .CreateSyntaxProvider( |
| | 2117 | 33 | | static (node, _) => node is MethodDeclarationSyntax, |
| | 52 | 34 | | static (syntaxContext, _) => GetErrorHandlerMethod(syntaxContext)) |
| | 52 | 35 | | .Where(static method => method is not null) |
| | 28 | 36 | | .Collect(); |
| | | 37 | | |
| | 28 | 38 | | IncrementalValueProvider<ImmutableArray<GeneratedStateGroup?>> stateGroups = context.SyntaxProvider |
| | 28 | 39 | | .CreateSyntaxProvider( |
| | 2117 | 40 | | static (node, _) => node is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, |
| | 19 | 41 | | static (syntaxContext, _) => GetStateGroup(syntaxContext)) |
| | 19 | 42 | | .Where(static group => group is not null) |
| | 28 | 43 | | .Collect(); |
| | | 44 | | |
| | 28 | 45 | | IncrementalValueProvider<ImmutableArray<GeneratedCallbackDataPayload?>> callbackDataPayloads = context.SyntaxPro |
| | 28 | 46 | | .CreateSyntaxProvider( |
| | 2117 | 47 | | static (node, _) => node is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, |
| | 19 | 48 | | static (syntaxContext, _) => GetCallbackDataPayload(syntaxContext)) |
| | 19 | 49 | | .Where(static payload => payload is not null) |
| | 28 | 50 | | .Collect(); |
| | | 51 | | |
| | 28 | 52 | | context.RegisterSourceOutput( |
| | 28 | 53 | | methodHandlers.Combine(classBasedHandlers).Combine(errorHandlers).Combine(callbackDataPayloads), |
| | 28 | 54 | | static (sourceContext, collectedHandlers) => |
| | 28 | 55 | | { |
| | 28 | 56 | | GeneratedHandlerMethod[] handlers = collectedHandlers.Left.Left.Left |
| | 28 | 57 | | .Concat(collectedHandlers.Left.Left.Right) |
| | 39 | 58 | | .Where(static handler => handler is not null) |
| | 28 | 59 | | .Cast<GeneratedHandlerMethod>() |
| | 39 | 60 | | .GroupBy(static handler => handler.SignatureKey, StringComparer.Ordinal) |
| | 39 | 61 | | .Select(static group => group.First()) |
| | 25 | 62 | | .OrderBy(static handler => handler.HandlerTypeMetadataName, StringComparer.Ordinal) |
| | 25 | 63 | | .ThenBy(static handler => handler.SourcePath, StringComparer.Ordinal) |
| | 25 | 64 | | .ThenBy(static handler => handler.SourceSpanStart) |
| | 28 | 65 | | .ToArray(); |
| | 28 | 66 | | GeneratedErrorHandlerMethod[] errors = collectedHandlers.Left.Right |
| | 3 | 67 | | .Where(static handler => handler is not null) |
| | 28 | 68 | | .Cast<GeneratedErrorHandlerMethod>() |
| | 3 | 69 | | .GroupBy(static handler => handler.SignatureKey, StringComparer.Ordinal) |
| | 3 | 70 | | .Select(static group => group.First()) |
| | 2 | 71 | | .OrderBy(static handler => handler.HandlerTypeMetadataName, StringComparer.Ordinal) |
| | 2 | 72 | | .ThenBy(static handler => handler.SourcePath, StringComparer.Ordinal) |
| | 2 | 73 | | .ThenBy(static handler => handler.SourceSpanStart) |
| | 28 | 74 | | .ToArray(); |
| | 28 | 75 | | GeneratedCallbackDataPayload[] callbackPayloads = collectedHandlers.Right |
| | 2 | 76 | | .Where(static payload => payload is not null) |
| | 28 | 77 | | .Cast<GeneratedCallbackDataPayload>() |
| | 2 | 78 | | .GroupBy(static payload => payload.TypeMetadataName, StringComparer.Ordinal) |
| | 2 | 79 | | .Select(static group => group.First()) |
| | 0 | 80 | | .OrderBy(static payload => payload.TypeMetadataName, StringComparer.Ordinal) |
| | 0 | 81 | | .ThenBy(static payload => payload.SourcePath, StringComparer.Ordinal) |
| | 0 | 82 | | .ThenBy(static payload => payload.SourceSpanStart) |
| | 28 | 83 | | .ToArray(); |
| | 28 | 84 | | |
| | 28 | 85 | | if (handlers.Length == 0 && errors.Length == 0 && callbackPayloads.Length == 0) |
| | 28 | 86 | | { |
| | 4 | 87 | | return; |
| | 28 | 88 | | } |
| | 28 | 89 | | |
| | 24 | 90 | | sourceContext.AddSource( |
| | 24 | 91 | | "TeleFlow.Telegram.GeneratedHandlers.g.cs", |
| | 24 | 92 | | SourceText.From(GenerateSource(handlers, errors, callbackPayloads), Encoding.UTF8)); |
| | 52 | 93 | | }); |
| | | 94 | | |
| | 28 | 95 | | context.RegisterSourceOutput(stateGroups, static (sourceContext, collectedGroups) => |
| | 28 | 96 | | { |
| | 28 | 97 | | GeneratedStateGroup[] groups = collectedGroups |
| | 4 | 98 | | .Where(static group => group is not null) |
| | 28 | 99 | | .Cast<GeneratedStateGroup>() |
| | 4 | 100 | | .GroupBy(static group => group.TypeMetadataName, StringComparer.Ordinal) |
| | 4 | 101 | | .Select(static group => group.First()) |
| | 0 | 102 | | .OrderBy(static group => group.TypeMetadataName, StringComparer.Ordinal) |
| | 28 | 103 | | .ToArray(); |
| | 28 | 104 | | |
| | 28 | 105 | | if (groups.Length == 0) |
| | 28 | 106 | | { |
| | 24 | 107 | | return; |
| | 28 | 108 | | } |
| | 28 | 109 | | |
| | 4 | 110 | | sourceContext.AddSource( |
| | 4 | 111 | | "TeleFlow.StateGroups.g.cs", |
| | 4 | 112 | | SourceText.From(GenerateStateGroupsSource(groups), Encoding.UTF8)); |
| | 32 | 113 | | }); |
| | 28 | 114 | | } |
| | | 115 | | |
| | | 116 | | private static GeneratedHandlerMethod? GetHandlerMethod(GeneratorSyntaxContext context) |
| | | 117 | | { |
| | 52 | 118 | | if (context.SemanticModel.GetDeclaredSymbol(context.Node) is not IMethodSymbol method || |
| | 52 | 119 | | IsClassBasedHandlerType(method.ContainingType) || |
| | 52 | 120 | | !TelegramHandlerSymbols.HasAnyRouteAttribute(method)) |
| | | 121 | | { |
| | 11 | 122 | | return null; |
| | | 123 | | } |
| | | 124 | | |
| | 41 | 125 | | if (!TryBuildHandler(method, includeClassRouteAttributes: false, out GeneratedHandlerMethod handler)) |
| | | 126 | | { |
| | 6 | 127 | | return null; |
| | | 128 | | } |
| | | 129 | | |
| | 35 | 130 | | return handler; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static GeneratedCallbackDataPayload? GetCallbackDataPayload(GeneratorSyntaxContext context) |
| | | 134 | | { |
| | 19 | 135 | | if (context.SemanticModel.GetDeclaredSymbol(context.Node) is not INamedTypeSymbol type) |
| | | 136 | | { |
| | 0 | 137 | | return null; |
| | | 138 | | } |
| | | 139 | | |
| | 19 | 140 | | AttributeData? attribute = TelegramHandlerSymbols |
| | 19 | 141 | | .GetAttributes(type, TelegramHandlerSymbols.CallbackDataAttribute, inherit: false) |
| | 19 | 142 | | .FirstOrDefault(); |
| | | 143 | | |
| | 19 | 144 | | if (attribute is null) |
| | | 145 | | { |
| | 17 | 146 | | return null; |
| | | 147 | | } |
| | | 148 | | |
| | 2 | 149 | | string? prefix = GetConstructorString(attribute); |
| | | 150 | | |
| | 2 | 151 | | if (!TelegramCallbackDataFacts.IsValidPayloadPrefix(prefix) || |
| | 2 | 152 | | !IsAccessibleFromGeneratedCode(type) || |
| | 2 | 153 | | !TryGetCallbackDataFields(type, out bool usesConstructor, out ImmutableArray<GeneratedCallbackDataField> fie |
| | | 154 | | { |
| | 0 | 155 | | return null; |
| | | 156 | | } |
| | | 157 | | |
| | 2 | 158 | | return new GeneratedCallbackDataPayload( |
| | 2 | 159 | | type.ToDisplayString(FullyQualifiedFormat), |
| | 2 | 160 | | type.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat), |
| | 2 | 161 | | prefix!, |
| | 2 | 162 | | usesConstructor, |
| | 2 | 163 | | fields, |
| | 2 | 164 | | context.Node.SyntaxTree.FilePath, |
| | 2 | 165 | | context.Node.SpanStart); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | private static GeneratedHandlerMethod? GetClassBasedHandlerMethod(GeneratorSyntaxContext context) |
| | | 169 | | { |
| | 51 | 170 | | if (context.SemanticModel.GetDeclaredSymbol(context.Node) is not INamedTypeSymbol type || |
| | 51 | 171 | | !IsClassBasedHandlerType(type)) |
| | | 172 | | { |
| | 47 | 173 | | return null; |
| | | 174 | | } |
| | | 175 | | |
| | 4 | 176 | | ImmutableArray<IMethodSymbol> handleMethods = GetDeclaredHandleAsyncMethods(type); |
| | | 177 | | |
| | 4 | 178 | | if (handleMethods.Length != 1 || |
| | 4 | 179 | | (!TelegramHandlerSymbols.HasAnyRouteAttribute(type) && |
| | 4 | 180 | | !TelegramHandlerSymbols.HasAnyRouteAttribute(handleMethods[0]))) |
| | | 181 | | { |
| | 0 | 182 | | return null; |
| | | 183 | | } |
| | | 184 | | |
| | 4 | 185 | | if (!TryBuildHandler(handleMethods[0], includeClassRouteAttributes: true, out GeneratedHandlerMethod handler)) |
| | | 186 | | { |
| | 0 | 187 | | return null; |
| | | 188 | | } |
| | | 189 | | |
| | 4 | 190 | | return handler; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | private static GeneratedErrorHandlerMethod? GetErrorHandlerMethod(GeneratorSyntaxContext context) |
| | | 194 | | { |
| | 52 | 195 | | if (context.SemanticModel.GetDeclaredSymbol(context.Node) is not IMethodSymbol method || |
| | 52 | 196 | | !TelegramHandlerSymbols.HasAnyErrorAttribute(method)) |
| | | 197 | | { |
| | 49 | 198 | | return null; |
| | | 199 | | } |
| | | 200 | | |
| | 3 | 201 | | if (!TryBuildErrorHandler(method, out GeneratedErrorHandlerMethod handler)) |
| | | 202 | | { |
| | 0 | 203 | | return null; |
| | | 204 | | } |
| | | 205 | | |
| | 3 | 206 | | return handler; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | private static bool TryBuildHandler( |
| | | 210 | | IMethodSymbol method, |
| | | 211 | | bool includeClassRouteAttributes, |
| | | 212 | | out GeneratedHandlerMethod handler) |
| | | 213 | | { |
| | 45 | 214 | | handler = null!; |
| | | 215 | | |
| | 45 | 216 | | ImmutableArray<GeneratedRoute> routes = BuildRoutes(method, includeClassRouteAttributes, out ITypeSymbol? callba |
| | | 217 | | |
| | 45 | 218 | | if (routes.Length == 0 || |
| | 45 | 219 | | method.DeclaredAccessibility != Accessibility.Public || |
| | 45 | 220 | | method.IsStatic || |
| | 45 | 221 | | method.IsGenericMethod || |
| | 45 | 222 | | method.ContainingType.TypeKind is TypeKind.Interface || |
| | 45 | 223 | | method.ContainingType.IsAbstract || |
| | 45 | 224 | | !IsClassBasedRouteCompatible(method.ContainingType, routes[0].Kind, callbackPayloadType) || |
| | 45 | 225 | | !IsSupportedReturnType(method.ReturnType) || |
| | 45 | 226 | | !TryGetExpectedContextType(routes[0].Kind, out string expectedContextType) || |
| | 45 | 227 | | !HasExactlyOneExpectedContext(method, expectedContextType) || |
| | 45 | 228 | | !HasValidCallbackPayloadParameter(method, callbackPayloadType) || |
| | 45 | 229 | | !TryResolveRouteValueNames(method, routes, out ImmutableHashSet<string> routeValueNames)) |
| | | 230 | | { |
| | 5 | 231 | | return false; |
| | | 232 | | } |
| | | 233 | | |
| | 40 | 234 | | if (!TryBuildStates(method, out ImmutableArray<string> states, out string? sceneName)) |
| | | 235 | | { |
| | 1 | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | |
| | 39 | 239 | | MethodDeclarationSyntax? syntax = method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() as MethodDeclar |
| | 78 | 240 | | Location? location = method.Locations.FirstOrDefault(location => location.IsInSource); |
| | 39 | 241 | | ImmutableArray<GeneratedParameter> parameters = BuildParameters(method, routes[0].Kind, expectedContextType, cal |
| | 80 | 242 | | bool hasCallbackRoute = routes.Any(static route => route.Kind == GeneratedHandlerKind.Callback); |
| | | 243 | | |
| | 39 | 244 | | if (parameters.Length == 0) |
| | | 245 | | { |
| | 0 | 246 | | return false; |
| | | 247 | | } |
| | | 248 | | |
| | 39 | 249 | | if (!TryBuildAutoAnswerCallback(method, hasCallbackRoute, out GeneratedAutoAnswerCallback? autoAnswerCallback)) |
| | | 250 | | { |
| | 0 | 251 | | return false; |
| | | 252 | | } |
| | | 253 | | |
| | 39 | 254 | | handler = new GeneratedHandlerMethod( |
| | 39 | 255 | | SignatureKey: method.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat), |
| | 39 | 256 | | HandlerTypeName: method.ContainingType.ToDisplayString(FullyQualifiedFormat), |
| | 39 | 257 | | HandlerTypeMetadataName: method.ContainingType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) |
| | 39 | 258 | | MethodName: method.Name, |
| | 39 | 259 | | ModuleName: GetModuleName(method.ContainingType), |
| | 39 | 260 | | SceneName: sceneName, |
| | 39 | 261 | | CallbackPayloadType: callbackPayloadType?.ToDisplayString(FullyQualifiedFormat), |
| | 39 | 262 | | AutoAnswerCallback: autoAnswerCallback, |
| | 39 | 263 | | Routes: routes, |
| | 39 | 264 | | States: states, |
| | 39 | 265 | | Parameters: parameters, |
| | 39 | 266 | | SourcePath: location?.SourceTree?.FilePath ?? string.Empty, |
| | 39 | 267 | | SourceSpanStart: location?.SourceSpan.Start ?? syntax?.SpanStart ?? 0); |
| | | 268 | | |
| | 39 | 269 | | return true; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | private static bool TryBuildErrorHandler( |
| | | 273 | | IMethodSymbol method, |
| | | 274 | | out GeneratedErrorHandlerMethod handler) |
| | | 275 | | { |
| | 3 | 276 | | handler = null!; |
| | | 277 | | |
| | 3 | 278 | | IReadOnlyList<AttributeData> errorAttributes = TelegramHandlerSymbols.GetErrorAttributes(method, inherit: true); |
| | | 279 | | |
| | 3 | 280 | | if (errorAttributes.Count == 0 || |
| | 3 | 281 | | method.DeclaredAccessibility != Accessibility.Public || |
| | 3 | 282 | | method.IsStatic || |
| | 3 | 283 | | method.IsGenericMethod || |
| | 3 | 284 | | method.ContainingType.TypeKind is TypeKind.Interface || |
| | 3 | 285 | | method.ContainingType.IsAbstract || |
| | 3 | 286 | | !TryGetErrorReturnKind(method.ReturnType, out GeneratedErrorReturnKind returnKind) || |
| | 3 | 287 | | !TryBuildErrorExceptionTypes(errorAttributes, out ImmutableArray<ITypeSymbol?> exceptionTypes, out Immutable |
| | 3 | 288 | | !TryBuildErrorParameters(method, exceptionTypes, out ImmutableArray<GeneratedErrorParameter> parameters, out |
| | | 289 | | { |
| | 0 | 290 | | return false; |
| | | 291 | | } |
| | | 292 | | |
| | 3 | 293 | | MethodDeclarationSyntax? syntax = method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() as MethodDeclar |
| | 6 | 294 | | Location? location = method.Locations.FirstOrDefault(static location => location.IsInSource); |
| | | 295 | | |
| | 3 | 296 | | handler = new GeneratedErrorHandlerMethod( |
| | 3 | 297 | | SignatureKey: method.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat), |
| | 3 | 298 | | HandlerTypeName: method.ContainingType.ToDisplayString(FullyQualifiedFormat), |
| | 3 | 299 | | HandlerTypeMetadataName: method.ContainingType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) |
| | 3 | 300 | | MethodName: method.Name, |
| | 3 | 301 | | ModuleName: GetModuleName(method.ContainingType), |
| | 3 | 302 | | ExceptionTypes: exceptionTypeNames, |
| | 3 | 303 | | TelegramContextType: telegramContextType, |
| | 3 | 304 | | ReturnKind: returnKind, |
| | 3 | 305 | | Parameters: parameters, |
| | 3 | 306 | | SourcePath: location?.SourceTree?.FilePath ?? string.Empty, |
| | 3 | 307 | | SourceSpanStart: location?.SourceSpan.Start ?? syntax?.SpanStart ?? 0); |
| | | 308 | | |
| | 3 | 309 | | return true; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | private static bool TryBuildErrorExceptionTypes( |
| | | 313 | | IReadOnlyList<AttributeData> attributes, |
| | | 314 | | out ImmutableArray<ITypeSymbol?> exceptionTypes, |
| | | 315 | | out ImmutableArray<string?> exceptionTypeNames) |
| | | 316 | | { |
| | 3 | 317 | | ImmutableArray<ITypeSymbol?>.Builder symbols = ImmutableArray.CreateBuilder<ITypeSymbol?>(); |
| | 3 | 318 | | ImmutableArray<string?>.Builder names = ImmutableArray.CreateBuilder<string?>(); |
| | | 319 | | |
| | 12 | 320 | | foreach (AttributeData attribute in attributes) |
| | | 321 | | { |
| | 3 | 322 | | if (TelegramHandlerSymbols.IsGenericAttribute(attribute, TelegramHandlerSymbols.GenericErrorAttribute)) |
| | | 323 | | { |
| | 2 | 324 | | if (attribute.AttributeClass is not { TypeArguments.Length: 1 } genericAttribute || |
| | 2 | 325 | | !IsExceptionType(genericAttribute.TypeArguments[0])) |
| | | 326 | | { |
| | 0 | 327 | | exceptionTypes = []; |
| | 0 | 328 | | exceptionTypeNames = []; |
| | 0 | 329 | | return false; |
| | | 330 | | } |
| | | 331 | | |
| | 2 | 332 | | symbols.Add(genericAttribute.TypeArguments[0]); |
| | 2 | 333 | | names.Add(genericAttribute.TypeArguments[0].ToDisplayString(FullyQualifiedFormat)); |
| | 2 | 334 | | continue; |
| | | 335 | | } |
| | | 336 | | |
| | 1 | 337 | | symbols.Add(null); |
| | 1 | 338 | | names.Add(null); |
| | | 339 | | } |
| | | 340 | | |
| | 3 | 341 | | exceptionTypes = symbols.ToImmutable(); |
| | 3 | 342 | | exceptionTypeNames = names.ToImmutable(); |
| | 3 | 343 | | return exceptionTypes.Length > 0; |
| | 0 | 344 | | } |
| | | 345 | | |
| | | 346 | | private static bool TryBuildErrorParameters( |
| | | 347 | | IMethodSymbol method, |
| | | 348 | | ImmutableArray<ITypeSymbol?> exceptionTypes, |
| | | 349 | | out ImmutableArray<GeneratedErrorParameter> parameters, |
| | | 350 | | out string? telegramContextType) |
| | | 351 | | { |
| | 3 | 352 | | parameters = []; |
| | 3 | 353 | | telegramContextType = null; |
| | | 354 | | |
| | 3 | 355 | | if (method.Parameters.Count(static parameter => |
| | 11 | 356 | | TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.TelegramErrorContext)) > 1 || |
| | 3 | 357 | | method.Parameters.Count(static parameter => |
| | 14 | 358 | | TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.CancellationToken)) > 1) |
| | | 359 | | { |
| | 0 | 360 | | return false; |
| | | 361 | | } |
| | | 362 | | |
| | 3 | 363 | | IParameterSymbol[] exceptionParameters = method.Parameters |
| | 11 | 364 | | .Where(static parameter => IsExceptionType(parameter.Type)) |
| | 3 | 365 | | .ToArray(); |
| | | 366 | | |
| | 3 | 367 | | if (exceptionParameters.Length > 1) |
| | | 368 | | { |
| | 0 | 369 | | return false; |
| | | 370 | | } |
| | | 371 | | |
| | 3 | 372 | | IParameterSymbol[] telegramContextParameters = method.Parameters |
| | 11 | 373 | | .Where(static parameter => IsTelegramContextType(parameter.Type)) |
| | 3 | 374 | | .ToArray(); |
| | | 375 | | |
| | 3 | 376 | | if (telegramContextParameters.Length > 1) |
| | | 377 | | { |
| | 0 | 378 | | return false; |
| | | 379 | | } |
| | | 380 | | |
| | 3 | 381 | | IParameterSymbol? exceptionParameter = exceptionParameters.FirstOrDefault(); |
| | | 382 | | |
| | 3 | 383 | | if (exceptionParameter is not null) |
| | | 384 | | { |
| | 12 | 385 | | foreach (ITypeSymbol? exceptionType in exceptionTypes) |
| | | 386 | | { |
| | 3 | 387 | | if (exceptionType is null) |
| | | 388 | | { |
| | 1 | 389 | | if (!TelegramHandlerSymbols.IsType(exceptionParameter.Type, TelegramHandlerSymbols.Exception)) |
| | | 390 | | { |
| | 0 | 391 | | return false; |
| | | 392 | | } |
| | | 393 | | |
| | | 394 | | continue; |
| | | 395 | | } |
| | | 396 | | |
| | 2 | 397 | | if (!IsAssignableFrom(exceptionParameter.Type, exceptionType)) |
| | | 398 | | { |
| | 0 | 399 | | return false; |
| | | 400 | | } |
| | | 401 | | } |
| | | 402 | | } |
| | | 403 | | |
| | 3 | 404 | | telegramContextType = telegramContextParameters.FirstOrDefault()?.Type.ToDisplayString(FullyQualifiedFormat); |
| | 3 | 405 | | parameters = BuildErrorParameters(method); |
| | 3 | 406 | | return true; |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | private static bool TryBuildAutoAnswerCallback( |
| | | 410 | | IMethodSymbol method, |
| | | 411 | | bool hasCallbackRoute, |
| | | 412 | | out GeneratedAutoAnswerCallback? autoAnswerCallback) |
| | | 413 | | { |
| | 39 | 414 | | autoAnswerCallback = null; |
| | | 415 | | |
| | 39 | 416 | | AttributeData? methodAttribute = TelegramHandlerSymbols.GetFirstAttribute( |
| | 39 | 417 | | method, |
| | 39 | 418 | | TelegramHandlerSymbols.AutoAnswerCallbackAttribute, |
| | 39 | 419 | | inherit: true); |
| | | 420 | | |
| | 39 | 421 | | if (methodAttribute is not null && !hasCallbackRoute) |
| | | 422 | | { |
| | 0 | 423 | | return false; |
| | | 424 | | } |
| | | 425 | | |
| | 39 | 426 | | if (!hasCallbackRoute) |
| | | 427 | | { |
| | 33 | 428 | | return true; |
| | | 429 | | } |
| | | 430 | | |
| | 6 | 431 | | AttributeData? attribute = methodAttribute ?? |
| | 6 | 432 | | TelegramHandlerSymbols.GetFirstAttribute( |
| | 6 | 433 | | method.ContainingType, |
| | 6 | 434 | | TelegramHandlerSymbols.AutoAnswerCallbackAttribute, |
| | 6 | 435 | | inherit: true); |
| | | 436 | | |
| | 6 | 437 | | if (attribute is null) |
| | | 438 | | { |
| | 5 | 439 | | return true; |
| | | 440 | | } |
| | | 441 | | |
| | 1 | 442 | | string? text = GetConstructorString(attribute); |
| | 1 | 443 | | if (text is not null && string.IsNullOrWhiteSpace(text)) |
| | | 444 | | { |
| | 0 | 445 | | return false; |
| | | 446 | | } |
| | | 447 | | |
| | 1 | 448 | | autoAnswerCallback = new GeneratedAutoAnswerCallback( |
| | 1 | 449 | | GetNamedBool(attribute, "Enabled", defaultValue: true), |
| | 1 | 450 | | text, |
| | 1 | 451 | | GetNamedBool(attribute, "ShowAlert", defaultValue: false)); |
| | 1 | 452 | | return true; |
| | | 453 | | } |
| | | 454 | | |
| | | 455 | | private static ImmutableArray<GeneratedRoute> BuildRoutes( |
| | | 456 | | IMethodSymbol method, |
| | | 457 | | bool includeClassRouteAttributes, |
| | | 458 | | out ITypeSymbol? callbackPayloadType) |
| | | 459 | | { |
| | 45 | 460 | | callbackPayloadType = null; |
| | 45 | 461 | | bool hasRawCallback = GetRouteAttributes( |
| | 45 | 462 | | method, |
| | 45 | 463 | | TelegramHandlerSymbols.CallbackAttribute, |
| | 45 | 464 | | includeClassRouteAttributes) |
| | 45 | 465 | | .Count > 0; |
| | 45 | 466 | | IReadOnlyList<AttributeData> genericCallbackAttributes = GetGenericRouteAttributes( |
| | 45 | 467 | | method, |
| | 45 | 468 | | TelegramHandlerSymbols.GenericCallbackAttribute, |
| | 45 | 469 | | includeClassRouteAttributes); |
| | 45 | 470 | | AttributeData? genericCallback = genericCallbackAttributes.Count > 0 |
| | 45 | 471 | | ? genericCallbackAttributes[0] |
| | 45 | 472 | | : null; |
| | | 473 | | |
| | 45 | 474 | | if (genericCallback?.AttributeClass is { TypeArguments.Length: 1 } genericCallbackType) |
| | | 475 | | { |
| | 3 | 476 | | callbackPayloadType = genericCallbackType.TypeArguments[0]; |
| | | 477 | | } |
| | | 478 | | |
| | 45 | 479 | | int messageRouteAttributeCount = |
| | 45 | 480 | | GetRouteAttributes(method, TelegramHandlerSymbols.CommandAttribute, includeClassRouteAttributes).Count + |
| | 45 | 481 | | GetRouteAttributes(method, TelegramHandlerSymbols.MessageAttribute, includeClassRouteAttributes).Count + |
| | 45 | 482 | | GetRouteAttributes(method, TelegramHandlerSymbols.TextAttribute, includeClassRouteAttributes).Count + |
| | 45 | 483 | | GetRouteAttributes(method, TelegramHandlerSymbols.TextTemplateAttribute, includeClassRouteAttributes).Count |
| | 45 | 484 | | GetRouteAttributes(method, TelegramHandlerSymbols.CommandTemplateAttribute, includeClassRouteAttributes).Cou |
| | 45 | 485 | | GetRouteAttributes(method, TelegramHandlerSymbols.TextRegexAttribute, includeClassRouteAttributes).Count + |
| | 45 | 486 | | GetRouteAttributes(method, TelegramHandlerSymbols.CommandRegexAttribute, includeClassRouteAttributes).Count; |
| | 45 | 487 | | bool hasChatMemberUpdated = GetRouteAttributes( |
| | 45 | 488 | | method, |
| | 45 | 489 | | TelegramHandlerSymbols.ChatMemberUpdatedAttribute, |
| | 45 | 490 | | includeClassRouteAttributes).Count > 0; |
| | 45 | 491 | | bool hasMyChatMemberUpdated = GetRouteAttributes( |
| | 45 | 492 | | method, |
| | 45 | 493 | | TelegramHandlerSymbols.MyChatMemberUpdatedAttribute, |
| | 45 | 494 | | includeClassRouteAttributes).Count > 0; |
| | 45 | 495 | | bool hasChatMemberRoute = hasChatMemberUpdated || hasMyChatMemberUpdated; |
| | | 496 | | |
| | 45 | 497 | | if ((hasRawCallback || callbackPayloadType is not null) && |
| | 45 | 498 | | (messageRouteAttributeCount > 0 || hasChatMemberRoute || (hasRawCallback && callbackPayloadType is not null) |
| | | 499 | | { |
| | 0 | 500 | | return []; |
| | | 501 | | } |
| | | 502 | | |
| | 45 | 503 | | if (hasChatMemberRoute && messageRouteAttributeCount > 0) |
| | | 504 | | { |
| | 0 | 505 | | return []; |
| | | 506 | | } |
| | | 507 | | |
| | 45 | 508 | | if (!hasChatMemberRoute && HasChatMemberTransitionAttributes(method)) |
| | | 509 | | { |
| | 0 | 510 | | return []; |
| | | 511 | | } |
| | | 512 | | |
| | 45 | 513 | | if (!TryBuildFilters(method, out ImmutableArray<GeneratedFilter> filters)) |
| | | 514 | | { |
| | 0 | 515 | | return []; |
| | | 516 | | } |
| | | 517 | | |
| | 45 | 518 | | if (!TryBuildRoleRequirements(method, out ImmutableArray<int> roleRequirements)) |
| | | 519 | | { |
| | 0 | 520 | | return []; |
| | | 521 | | } |
| | | 522 | | |
| | 45 | 523 | | if (hasChatMemberRoute) |
| | | 524 | | { |
| | 7 | 525 | | if (!TryPrepareCustomFilters(filters, GeneratedHandlerKind.ChatMember, out ImmutableArray<GeneratedFilter> c |
| | 0 | 526 | | chatMemberFilters.Any(static filter => filter.CustomTypeName is null && |
| | 0 | 527 | | !SupportsGeneratedFilter(filter, GeneratedHandlerKind.ChatMember)) || |
| | 7 | 528 | | !TryBuildChatMemberTransitions(method, out ImmutableArray<GeneratedChatMemberTransition> transitions)) |
| | | 529 | | { |
| | 0 | 530 | | return []; |
| | | 531 | | } |
| | | 532 | | |
| | 7 | 533 | | filters = chatMemberFilters; |
| | 7 | 534 | | ImmutableArray<GeneratedRoute>.Builder chatMemberRoutes = ImmutableArray.CreateBuilder<GeneratedRoute>(); |
| | | 535 | | |
| | 7 | 536 | | if (hasChatMemberUpdated) |
| | | 537 | | { |
| | 6 | 538 | | chatMemberRoutes.Add(new GeneratedRoute( |
| | 6 | 539 | | GeneratedHandlerKind.ChatMember, |
| | 6 | 540 | | GeneratedRouteKind.ChatMemberUpdated, |
| | 6 | 541 | | Command: null, |
| | 6 | 542 | | Pattern: null, |
| | 6 | 543 | | CommandPrefixes: ["/"], |
| | 6 | 544 | | AllowSpaceAfterPrefix: false, |
| | 6 | 545 | | PrefixMode: CommandPrefixModeRequired, |
| | 6 | 546 | | IgnoreCase: true, |
| | 6 | 547 | | TextFilters: [], |
| | 6 | 548 | | filters, |
| | 6 | 549 | | transitions, |
| | 6 | 550 | | roleRequirements, |
| | 6 | 551 | | RouteValues: ImmutableDictionary<string, GeneratedRouteValue>.Empty)); |
| | | 552 | | } |
| | | 553 | | |
| | 7 | 554 | | if (hasMyChatMemberUpdated) |
| | | 555 | | { |
| | 1 | 556 | | chatMemberRoutes.Add(new GeneratedRoute( |
| | 1 | 557 | | GeneratedHandlerKind.ChatMember, |
| | 1 | 558 | | GeneratedRouteKind.MyChatMemberUpdated, |
| | 1 | 559 | | Command: null, |
| | 1 | 560 | | Pattern: null, |
| | 1 | 561 | | CommandPrefixes: ["/"], |
| | 1 | 562 | | AllowSpaceAfterPrefix: false, |
| | 1 | 563 | | PrefixMode: CommandPrefixModeRequired, |
| | 1 | 564 | | IgnoreCase: true, |
| | 1 | 565 | | TextFilters: [], |
| | 1 | 566 | | filters, |
| | 1 | 567 | | transitions, |
| | 1 | 568 | | roleRequirements, |
| | 1 | 569 | | RouteValues: ImmutableDictionary<string, GeneratedRouteValue>.Empty)); |
| | | 570 | | } |
| | | 571 | | |
| | 7 | 572 | | return chatMemberRoutes.ToImmutable(); |
| | | 573 | | } |
| | | 574 | | |
| | 38 | 575 | | if (hasRawCallback || callbackPayloadType is not null) |
| | | 576 | | { |
| | 6 | 577 | | if (!TryPrepareCustomFilters(filters, GeneratedHandlerKind.Callback, out ImmutableArray<GeneratedFilter> cal |
| | 10 | 578 | | callbackFilters.Any(static filter => filter.CustomTypeName is null && |
| | 10 | 579 | | !SupportsGeneratedFilter(filter, GeneratedHandlerKind.Callback))) |
| | | 580 | | { |
| | 0 | 581 | | return []; |
| | | 582 | | } |
| | | 583 | | |
| | 6 | 584 | | filters = callbackFilters; |
| | 6 | 585 | | return |
| | 6 | 586 | | [ |
| | 6 | 587 | | new GeneratedRoute( |
| | 6 | 588 | | GeneratedHandlerKind.Callback, |
| | 6 | 589 | | GeneratedRouteKind.Callback, |
| | 6 | 590 | | Command: null, |
| | 6 | 591 | | Pattern: null, |
| | 6 | 592 | | CommandPrefixes: ["/"], |
| | 6 | 593 | | AllowSpaceAfterPrefix: false, |
| | 6 | 594 | | PrefixMode: CommandPrefixModeRequired, |
| | 6 | 595 | | IgnoreCase: true, |
| | 6 | 596 | | TextFilters: [], |
| | 6 | 597 | | filters, |
| | 6 | 598 | | ChatMemberTransitions: [], |
| | 6 | 599 | | RoleRequirements: roleRequirements, |
| | 6 | 600 | | RouteValues: ImmutableDictionary<string, GeneratedRouteValue>.Empty) |
| | 6 | 601 | | ]; |
| | | 602 | | } |
| | | 603 | | |
| | 32 | 604 | | if (!TryPrepareCustomFilters(filters, GeneratedHandlerKind.Message, out ImmutableArray<GeneratedFilter> messageF |
| | 54 | 605 | | messageFilters.Any(static filter => filter.CustomTypeName is null && |
| | 54 | 606 | | !SupportsGeneratedFilter(filter, GeneratedHandlerKind.Message))) |
| | | 607 | | { |
| | 0 | 608 | | return []; |
| | | 609 | | } |
| | | 610 | | |
| | 32 | 611 | | filters = messageFilters; |
| | 32 | 612 | | ImmutableArray<GeneratedRoute>.Builder routes = ImmutableArray.CreateBuilder<GeneratedRoute>(); |
| | | 613 | | |
| | 87 | 614 | | foreach (AttributeData attribute in GetRouteAttributes( |
| | 32 | 615 | | method, |
| | 32 | 616 | | TelegramHandlerSymbols.CommandAttribute, |
| | 32 | 617 | | includeClassRouteAttributes)) |
| | | 618 | | { |
| | 12 | 619 | | string? command = GetConstructorString(attribute); |
| | 12 | 620 | | int prefixMode = GetCommandPrefixMode(attribute); |
| | | 621 | | |
| | 12 | 622 | | if (!IsValidCommand(command) || |
| | 12 | 623 | | !TryGetPrefixes(attribute, out ImmutableArray<string> prefixes) || |
| | 12 | 624 | | !IsSupportedCommandPrefixMode(prefixMode) || |
| | 12 | 625 | | HasInvalidNoPrefixConfiguration(attribute, prefixMode)) |
| | | 626 | | { |
| | 1 | 627 | | return []; |
| | | 628 | | } |
| | | 629 | | |
| | 11 | 630 | | routes.Add(new GeneratedRoute( |
| | 11 | 631 | | GeneratedHandlerKind.Command, |
| | 11 | 632 | | GeneratedRouteKind.CommandExact, |
| | 11 | 633 | | command, |
| | 11 | 634 | | command, |
| | 11 | 635 | | prefixes, |
| | 11 | 636 | | GetNamedBool(attribute, "AllowSpaceAfterPrefix", defaultValue: false), |
| | 11 | 637 | | prefixMode, |
| | 11 | 638 | | GetNamedBool(attribute, "IgnoreCase", defaultValue: true), |
| | 11 | 639 | | TextFilters: [], |
| | 11 | 640 | | filters, |
| | 11 | 641 | | ChatMemberTransitions: [], |
| | 11 | 642 | | RoleRequirements: roleRequirements, |
| | 11 | 643 | | RouteValues: ImmutableDictionary<string, GeneratedRouteValue>.Empty)); |
| | | 644 | | } |
| | | 645 | | |
| | 66 | 646 | | foreach (AttributeData attribute in GetRouteAttributes( |
| | 31 | 647 | | method, |
| | 31 | 648 | | TelegramHandlerSymbols.CommandTemplateAttribute, |
| | 31 | 649 | | includeClassRouteAttributes)) |
| | | 650 | | { |
| | 3 | 651 | | string? template = GetConstructorString(attribute); |
| | 3 | 652 | | int prefixMode = GetCommandPrefixMode(attribute); |
| | | 653 | | |
| | 3 | 654 | | if (template is null || |
| | 3 | 655 | | string.IsNullOrWhiteSpace(template) || |
| | 3 | 656 | | !TryParseTemplateRouteValues(template, out ImmutableDictionary<string, GeneratedRouteValue> routeValues) |
| | 3 | 657 | | !TryGetPrefixes(attribute, out ImmutableArray<string> prefixes) || |
| | 3 | 658 | | !IsSupportedCommandPrefixMode(prefixMode) || |
| | 3 | 659 | | HasInvalidNoPrefixConfiguration(attribute, prefixMode) || |
| | 3 | 660 | | CommandPatternStartsWithPrefix(template, prefixes)) |
| | | 661 | | { |
| | 2 | 662 | | return []; |
| | | 663 | | } |
| | | 664 | | |
| | 1 | 665 | | routes.Add(new GeneratedRoute( |
| | 1 | 666 | | GeneratedHandlerKind.Command, |
| | 1 | 667 | | GeneratedRouteKind.CommandTemplate, |
| | 1 | 668 | | Command: null, |
| | 1 | 669 | | Pattern: template.Trim(), |
| | 1 | 670 | | prefixes, |
| | 1 | 671 | | GetNamedBool(attribute, "AllowSpaceAfterPrefix", defaultValue: false), |
| | 1 | 672 | | prefixMode, |
| | 1 | 673 | | GetNamedBool(attribute, "IgnoreCase", defaultValue: true), |
| | 1 | 674 | | TextFilters: [], |
| | 1 | 675 | | filters, |
| | 1 | 676 | | ChatMemberTransitions: [], |
| | 1 | 677 | | RoleRequirements: roleRequirements, |
| | 1 | 678 | | routeValues)); |
| | | 679 | | } |
| | | 680 | | |
| | 60 | 681 | | foreach (AttributeData attribute in GetRouteAttributes( |
| | 29 | 682 | | method, |
| | 29 | 683 | | TelegramHandlerSymbols.CommandRegexAttribute, |
| | 29 | 684 | | includeClassRouteAttributes)) |
| | | 685 | | { |
| | 1 | 686 | | string? pattern = GetConstructorString(attribute); |
| | 1 | 687 | | int prefixMode = GetCommandPrefixMode(attribute); |
| | | 688 | | |
| | 1 | 689 | | if (string.IsNullOrWhiteSpace(pattern) || |
| | 1 | 690 | | !TryParseRegexRouteValues(pattern, out ImmutableDictionary<string, GeneratedRouteValue> routeValues) || |
| | 1 | 691 | | !TryGetPrefixes(attribute, out ImmutableArray<string> prefixes) || |
| | 1 | 692 | | !IsSupportedCommandPrefixMode(prefixMode) || |
| | 1 | 693 | | HasInvalidNoPrefixConfiguration(attribute, prefixMode)) |
| | | 694 | | { |
| | 0 | 695 | | return []; |
| | | 696 | | } |
| | | 697 | | |
| | 1 | 698 | | routes.Add(new GeneratedRoute( |
| | 1 | 699 | | GeneratedHandlerKind.Command, |
| | 1 | 700 | | GeneratedRouteKind.CommandRegex, |
| | 1 | 701 | | Command: null, |
| | 1 | 702 | | Pattern: pattern, |
| | 1 | 703 | | prefixes, |
| | 1 | 704 | | GetNamedBool(attribute, "AllowSpaceAfterPrefix", defaultValue: false), |
| | 1 | 705 | | prefixMode, |
| | 1 | 706 | | GetNamedBool(attribute, "IgnoreCase", defaultValue: true), |
| | 1 | 707 | | TextFilters: [], |
| | 1 | 708 | | filters, |
| | 1 | 709 | | ChatMemberTransitions: [], |
| | 1 | 710 | | RoleRequirements: roleRequirements, |
| | 1 | 711 | | routeValues)); |
| | | 712 | | } |
| | | 713 | | |
| | 29 | 714 | | ImmutableArray<GeneratedTextFilter> textFilters = BuildTextFilters(method, includeClassRouteAttributes); |
| | 29 | 715 | | bool hasMessageAttribute = GetRouteAttributes( |
| | 29 | 716 | | method, |
| | 29 | 717 | | TelegramHandlerSymbols.MessageAttribute, |
| | 29 | 718 | | includeClassRouteAttributes).Count > 0; |
| | | 719 | | |
| | 29 | 720 | | if (hasMessageAttribute) |
| | | 721 | | { |
| | 13 | 722 | | routes.Add(new GeneratedRoute( |
| | 13 | 723 | | GeneratedHandlerKind.Message, |
| | 13 | 724 | | GeneratedRouteKind.MessageAny, |
| | 13 | 725 | | Command: null, |
| | 13 | 726 | | Pattern: null, |
| | 13 | 727 | | CommandPrefixes: ["/"], |
| | 13 | 728 | | AllowSpaceAfterPrefix: false, |
| | 13 | 729 | | PrefixMode: CommandPrefixModeRequired, |
| | 13 | 730 | | IgnoreCase: true, |
| | 13 | 731 | | textFilters, |
| | 13 | 732 | | filters, |
| | 13 | 733 | | ChatMemberTransitions: [], |
| | 13 | 734 | | RoleRequirements: roleRequirements, |
| | 13 | 735 | | RouteValues: ImmutableDictionary<string, GeneratedRouteValue>.Empty)); |
| | | 736 | | } |
| | | 737 | | else |
| | | 738 | | { |
| | 34 | 739 | | foreach (GeneratedTextFilter filter in textFilters) |
| | | 740 | | { |
| | 1 | 741 | | routes.Add(new GeneratedRoute( |
| | 1 | 742 | | GeneratedHandlerKind.Message, |
| | 1 | 743 | | GeneratedRouteKind.TextExact, |
| | 1 | 744 | | Command: null, |
| | 1 | 745 | | Pattern: filter.Value, |
| | 1 | 746 | | CommandPrefixes: ["/"], |
| | 1 | 747 | | AllowSpaceAfterPrefix: false, |
| | 1 | 748 | | PrefixMode: CommandPrefixModeRequired, |
| | 1 | 749 | | IgnoreCase: filter.IgnoreCase, |
| | 1 | 750 | | TextFilters: [filter], |
| | 1 | 751 | | filters, |
| | 1 | 752 | | ChatMemberTransitions: [], |
| | 1 | 753 | | RoleRequirements: roleRequirements, |
| | 1 | 754 | | RouteValues: ImmutableDictionary<string, GeneratedRouteValue>.Empty)); |
| | | 755 | | } |
| | | 756 | | } |
| | | 757 | | |
| | 65 | 758 | | foreach (AttributeData attribute in GetRouteAttributes( |
| | 29 | 759 | | method, |
| | 29 | 760 | | TelegramHandlerSymbols.TextTemplateAttribute, |
| | 29 | 761 | | includeClassRouteAttributes)) |
| | | 762 | | { |
| | 4 | 763 | | string? template = GetConstructorString(attribute); |
| | | 764 | | |
| | 4 | 765 | | if (template is null || |
| | 4 | 766 | | string.IsNullOrWhiteSpace(template) || |
| | 4 | 767 | | !TryParseTemplateRouteValues(template, out ImmutableDictionary<string, GeneratedRouteValue> routeValues) |
| | | 768 | | { |
| | 1 | 769 | | return []; |
| | | 770 | | } |
| | | 771 | | |
| | 3 | 772 | | routes.Add(new GeneratedRoute( |
| | 3 | 773 | | GeneratedHandlerKind.Message, |
| | 3 | 774 | | GeneratedRouteKind.TextTemplate, |
| | 3 | 775 | | Command: null, |
| | 3 | 776 | | Pattern: template.Trim(), |
| | 3 | 777 | | CommandPrefixes: ["/"], |
| | 3 | 778 | | AllowSpaceAfterPrefix: false, |
| | 3 | 779 | | PrefixMode: CommandPrefixModeRequired, |
| | 3 | 780 | | IgnoreCase: GetNamedBool(attribute, "IgnoreCase", defaultValue: true), |
| | 3 | 781 | | TextFilters: [], |
| | 3 | 782 | | filters, |
| | 3 | 783 | | ChatMemberTransitions: [], |
| | 3 | 784 | | RoleRequirements: roleRequirements, |
| | 3 | 785 | | routeValues)); |
| | | 786 | | } |
| | | 787 | | |
| | 56 | 788 | | foreach (AttributeData attribute in GetRouteAttributes( |
| | 28 | 789 | | method, |
| | 28 | 790 | | TelegramHandlerSymbols.TextRegexAttribute, |
| | 28 | 791 | | includeClassRouteAttributes)) |
| | | 792 | | { |
| | 0 | 793 | | string? pattern = GetConstructorString(attribute); |
| | | 794 | | |
| | 0 | 795 | | if (string.IsNullOrWhiteSpace(pattern) || |
| | 0 | 796 | | !TryParseRegexRouteValues(pattern, out ImmutableDictionary<string, GeneratedRouteValue> routeValues)) |
| | | 797 | | { |
| | 0 | 798 | | return []; |
| | | 799 | | } |
| | | 800 | | |
| | 0 | 801 | | routes.Add(new GeneratedRoute( |
| | 0 | 802 | | GeneratedHandlerKind.Message, |
| | 0 | 803 | | GeneratedRouteKind.TextRegex, |
| | 0 | 804 | | Command: null, |
| | 0 | 805 | | Pattern: pattern, |
| | 0 | 806 | | CommandPrefixes: ["/"], |
| | 0 | 807 | | AllowSpaceAfterPrefix: false, |
| | 0 | 808 | | PrefixMode: CommandPrefixModeRequired, |
| | 0 | 809 | | IgnoreCase: GetNamedBool(attribute, "IgnoreCase", defaultValue: true), |
| | 0 | 810 | | TextFilters: [], |
| | 0 | 811 | | filters, |
| | 0 | 812 | | ChatMemberTransitions: [], |
| | 0 | 813 | | RoleRequirements: roleRequirements, |
| | 0 | 814 | | routeValues)); |
| | | 815 | | } |
| | | 816 | | |
| | 28 | 817 | | return routes.ToImmutable(); |
| | 4 | 818 | | } |
| | | 819 | | |
| | | 820 | | private static bool TryBuildFilters( |
| | | 821 | | IMethodSymbol method, |
| | | 822 | | out ImmutableArray<GeneratedFilter> filters) |
| | | 823 | | { |
| | 45 | 824 | | ImmutableArray<GeneratedFilter>.Builder builder = ImmutableArray.CreateBuilder<GeneratedFilter>(); |
| | | 825 | | |
| | 45 | 826 | | if (!AppendFilters(builder, method.ContainingType) || |
| | 45 | 827 | | !AppendFilters(builder, method)) |
| | | 828 | | { |
| | 0 | 829 | | filters = []; |
| | 0 | 830 | | return false; |
| | | 831 | | } |
| | | 832 | | |
| | 45 | 833 | | filters = builder.ToImmutable(); |
| | 45 | 834 | | return true; |
| | | 835 | | } |
| | | 836 | | |
| | | 837 | | private static bool AppendFilters( |
| | | 838 | | ImmutableArray<GeneratedFilter>.Builder builder, |
| | | 839 | | ISymbol symbol) |
| | | 840 | | { |
| | 90 | 841 | | if (!AppendChatTypeFilters( |
| | 90 | 842 | | builder, |
| | 90 | 843 | | symbol, |
| | 90 | 844 | | TelegramHandlerSymbols.ChatTypeAttribute, |
| | 90 | 845 | | "ChatType") || |
| | 90 | 846 | | !AppendChatTypeFilters( |
| | 90 | 847 | | builder, |
| | 90 | 848 | | symbol, |
| | 90 | 849 | | TelegramHandlerSymbols.SenderChatTypeAttribute, |
| | 90 | 850 | | "SenderChatType")) |
| | | 851 | | { |
| | 0 | 852 | | return false; |
| | | 853 | | } |
| | | 854 | | |
| | 184 | 855 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes( |
| | 90 | 856 | | symbol, |
| | 90 | 857 | | TelegramHandlerSymbols.ChatIdAttribute, |
| | 90 | 858 | | inherit: true)) |
| | | 859 | | { |
| | 2 | 860 | | ImmutableArray<long> values = attribute.ConstructorArguments.Length > 0 |
| | 2 | 861 | | ? attribute.ConstructorArguments[0].Values |
| | 2 | 862 | | .Select(static value => value.Value) |
| | 2 | 863 | | .OfType<long>() |
| | 2 | 864 | | .ToImmutableArray() |
| | 2 | 865 | | : []; |
| | | 866 | | |
| | 2 | 867 | | if (values.Length == 0 || |
| | 4 | 868 | | values.Any(static value => value == 0)) |
| | | 869 | | { |
| | 0 | 870 | | return false; |
| | | 871 | | } |
| | | 872 | | |
| | 2 | 873 | | builder.Add(new GeneratedFilter("ChatId", StringValues: [], values)); |
| | | 874 | | } |
| | | 875 | | |
| | 182 | 876 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes( |
| | 90 | 877 | | symbol, |
| | 90 | 878 | | TelegramHandlerSymbols.ChatUsernameAttribute, |
| | 90 | 879 | | inherit: true)) |
| | | 880 | | { |
| | 1 | 881 | | ImmutableArray<string> values = attribute.ConstructorArguments.Length > 0 |
| | 1 | 882 | | ? attribute.ConstructorArguments[0].Values |
| | 1 | 883 | | .Select(static value => value.Value as string) |
| | 1 | 884 | | .Select(CanonicalizeChatUsername) |
| | 1 | 885 | | .ToImmutableArray() |
| | 1 | 886 | | : []; |
| | | 887 | | |
| | 1 | 888 | | if (values.Length == 0 || |
| | 2 | 889 | | values.Any(static value => value.Length == 0)) |
| | | 890 | | { |
| | 0 | 891 | | return false; |
| | | 892 | | } |
| | | 893 | | |
| | 1 | 894 | | builder.Add(new GeneratedFilter("ChatUsername", values, LongValues: [])); |
| | | 895 | | } |
| | | 896 | | |
| | 90 | 897 | | if (!AppendSenderIdFilters( |
| | 90 | 898 | | builder, |
| | 90 | 899 | | symbol, |
| | 90 | 900 | | TelegramHandlerSymbols.FromUserAttribute, |
| | 90 | 901 | | "FromUser") || |
| | 90 | 902 | | !AppendSenderIdFilters( |
| | 90 | 903 | | builder, |
| | 90 | 904 | | symbol, |
| | 90 | 905 | | TelegramHandlerSymbols.FromBotAttribute, |
| | 90 | 906 | | "FromBot")) |
| | | 907 | | { |
| | 0 | 908 | | return false; |
| | | 909 | | } |
| | | 910 | | |
| | 3780 | 911 | | foreach (TelegramBuiltInFilterSpec spec in TelegramBuiltInFilterFacts.MarkerSpecs) |
| | | 912 | | { |
| | 1800 | 913 | | AppendMarkerFilter(builder, symbol, spec); |
| | | 914 | | } |
| | | 915 | | |
| | 182 | 916 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes( |
| | 90 | 917 | | symbol, |
| | 90 | 918 | | TelegramHandlerSymbols.MessageThreadIdAttribute, |
| | 90 | 919 | | inherit: true)) |
| | | 920 | | { |
| | 1 | 921 | | ImmutableArray<long> values = attribute.ConstructorArguments.Length > 0 |
| | 1 | 922 | | ? attribute.ConstructorArguments[0].Values |
| | 1 | 923 | | .Select(static value => value.Value) |
| | 1 | 924 | | .OfType<long>() |
| | 1 | 925 | | .ToImmutableArray() |
| | 1 | 926 | | : []; |
| | | 927 | | |
| | 1 | 928 | | if (values.Length == 0 || |
| | 2 | 929 | | values.Any(static value => value <= 0)) |
| | | 930 | | { |
| | 0 | 931 | | return false; |
| | | 932 | | } |
| | | 933 | | |
| | 1 | 934 | | builder.Add(new GeneratedFilter("MessageThreadId", StringValues: [], values)); |
| | | 935 | | } |
| | | 936 | | |
| | 184 | 937 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes( |
| | 90 | 938 | | symbol, |
| | 90 | 939 | | TelegramHandlerSymbols.CallbackDataPrefixAttribute, |
| | 90 | 940 | | inherit: true)) |
| | | 941 | | { |
| | 2 | 942 | | if (GetConstructorString(attribute) is not { } prefix || |
| | 2 | 943 | | string.IsNullOrWhiteSpace(prefix)) |
| | | 944 | | { |
| | 0 | 945 | | return false; |
| | | 946 | | } |
| | | 947 | | |
| | 2 | 948 | | builder.Add(new GeneratedFilter("CallbackDataPrefix", [prefix], LongValues: [])); |
| | | 949 | | } |
| | | 950 | | |
| | 184 | 951 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetGenericAttributes( |
| | 90 | 952 | | symbol, |
| | 90 | 953 | | TelegramHandlerSymbols.GenericUseFilterAttribute, |
| | 90 | 954 | | inherit: true)) |
| | | 955 | | { |
| | 2 | 956 | | if (attribute.AttributeClass is not { TypeArguments.Length: 1 } filterAttributeType || |
| | 2 | 957 | | IsInvalidCustomFilterType(filterAttributeType.TypeArguments[0]) || |
| | 2 | 958 | | !TryGetTelegramFilterContextTypes( |
| | 2 | 959 | | filterAttributeType.TypeArguments[0], |
| | 2 | 960 | | attributeType: null, |
| | 2 | 961 | | out ImmutableArray<string> contextMetadataNames)) |
| | | 962 | | { |
| | 0 | 963 | | return false; |
| | | 964 | | } |
| | | 965 | | |
| | 2 | 966 | | builder.Add(new GeneratedFilter( |
| | 2 | 967 | | "Custom", |
| | 2 | 968 | | StringValues: [], |
| | 2 | 969 | | LongValues: [], |
| | 2 | 970 | | filterAttributeType.TypeArguments[0].ToDisplayString(FullyQualifiedFormat), |
| | 2 | 971 | | CustomContextMetadataNames: contextMetadataNames)); |
| | | 972 | | } |
| | | 973 | | |
| | 182 | 974 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetTelegramFilterAttributes(symbol, inherit: true)) |
| | | 975 | | { |
| | 1 | 976 | | if (attribute.AttributeClass is not { IsGenericType: false } attributeType || |
| | 1 | 977 | | !TelegramHandlerSymbols.TryGetTelegramFilterAttributeFilterType(attribute, out ITypeSymbol filterType) | |
| | 1 | 978 | | IsInvalidCustomFilterType(filterType) || |
| | 1 | 979 | | !TryGetTelegramFilterContextTypes( |
| | 1 | 980 | | filterType, |
| | 1 | 981 | | attributeType, |
| | 1 | 982 | | out ImmutableArray<string> contextMetadataNames)) |
| | | 983 | | { |
| | 0 | 984 | | return false; |
| | | 985 | | } |
| | | 986 | | |
| | 1 | 987 | | builder.Add(new GeneratedFilter( |
| | 1 | 988 | | "Custom", |
| | 1 | 989 | | StringValues: [], |
| | 1 | 990 | | LongValues: [], |
| | 1 | 991 | | filterType.ToDisplayString(FullyQualifiedFormat), |
| | 1 | 992 | | ToAttributeCreationExpression(attribute), |
| | 1 | 993 | | CustomContextMetadataNames: contextMetadataNames)); |
| | | 994 | | } |
| | | 995 | | |
| | 90 | 996 | | return true; |
| | 0 | 997 | | } |
| | | 998 | | |
| | | 999 | | private static bool AppendChatTypeFilters( |
| | | 1000 | | ImmutableArray<GeneratedFilter>.Builder builder, |
| | | 1001 | | ISymbol symbol, |
| | | 1002 | | string attributeMetadataName, |
| | | 1003 | | string generatedKind) |
| | | 1004 | | { |
| | 368 | 1005 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes( |
| | 180 | 1006 | | symbol, |
| | 180 | 1007 | | attributeMetadataName, |
| | 180 | 1008 | | inherit: true)) |
| | | 1009 | | { |
| | 4 | 1010 | | if (attribute.ConstructorArguments.Length == 0 || |
| | 4 | 1011 | | attribute.ConstructorArguments[0].Values.IsDefaultOrEmpty) |
| | | 1012 | | { |
| | 0 | 1013 | | return false; |
| | | 1014 | | } |
| | | 1015 | | |
| | 4 | 1016 | | ImmutableArray<string>.Builder values = ImmutableArray.CreateBuilder<string>(); |
| | | 1017 | | |
| | 22 | 1018 | | foreach (TypedConstant value in attribute.ConstructorArguments[0].Values) |
| | | 1019 | | { |
| | 7 | 1020 | | if (value.Value is not int chatType || |
| | 7 | 1021 | | !TelegramChatTypeFacts.TryMapToTelegramValue(chatType, out string mappedValue)) |
| | | 1022 | | { |
| | 0 | 1023 | | return false; |
| | | 1024 | | } |
| | | 1025 | | |
| | 7 | 1026 | | values.Add(mappedValue); |
| | | 1027 | | } |
| | | 1028 | | |
| | 4 | 1029 | | builder.Add(new GeneratedFilter(generatedKind, values.ToImmutable(), LongValues: [])); |
| | | 1030 | | } |
| | | 1031 | | |
| | 180 | 1032 | | return true; |
| | 0 | 1033 | | } |
| | | 1034 | | |
| | | 1035 | | private static bool AppendSenderIdFilters( |
| | | 1036 | | ImmutableArray<GeneratedFilter>.Builder builder, |
| | | 1037 | | ISymbol symbol, |
| | | 1038 | | string attributeMetadataName, |
| | | 1039 | | string generatedKind) |
| | | 1040 | | { |
| | 366 | 1041 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes( |
| | 180 | 1042 | | symbol, |
| | 180 | 1043 | | attributeMetadataName, |
| | 180 | 1044 | | inherit: true)) |
| | | 1045 | | { |
| | 3 | 1046 | | if (attribute.ConstructorArguments.Length != 1) |
| | | 1047 | | { |
| | 0 | 1048 | | return false; |
| | | 1049 | | } |
| | | 1050 | | |
| | 3 | 1051 | | ImmutableArray<TypedConstant> arguments = attribute.ConstructorArguments[0].Values; |
| | | 1052 | | |
| | 3 | 1053 | | if (arguments.IsDefault || |
| | 5 | 1054 | | arguments.Any(static value => value.Value is not long id || id <= 0)) |
| | | 1055 | | { |
| | 0 | 1056 | | return false; |
| | | 1057 | | } |
| | | 1058 | | |
| | 3 | 1059 | | builder.Add(new GeneratedFilter( |
| | 3 | 1060 | | generatedKind, |
| | 3 | 1061 | | StringValues: [], |
| | 5 | 1062 | | arguments.Select(static value => (long)value.Value!).ToImmutableArray())); |
| | | 1063 | | } |
| | | 1064 | | |
| | 180 | 1065 | | return true; |
| | 0 | 1066 | | } |
| | | 1067 | | |
| | | 1068 | | private static void AppendMarkerFilter( |
| | | 1069 | | ImmutableArray<GeneratedFilter>.Builder builder, |
| | | 1070 | | ISymbol symbol, |
| | | 1071 | | TelegramBuiltInFilterSpec spec) |
| | | 1072 | | { |
| | 1800 | 1073 | | if (TelegramHandlerSymbols.HasAttribute(symbol, spec.AttributeMetadataName, inherit: true)) |
| | | 1074 | | { |
| | 10 | 1075 | | builder.Add(new GeneratedFilter(spec.GeneratedKind, StringValues: [], LongValues: [])); |
| | | 1076 | | } |
| | 1800 | 1077 | | } |
| | | 1078 | | |
| | | 1079 | | private static bool SupportsGeneratedFilter( |
| | | 1080 | | GeneratedFilter filter, |
| | | 1081 | | GeneratedHandlerKind handlerKind) |
| | | 1082 | | { |
| | 23 | 1083 | | return TelegramBuiltInFilterFacts.TryGetSpecByGeneratedKind(filter.Kind, out TelegramBuiltInFilterSpec spec) && |
| | 23 | 1084 | | TelegramBuiltInFilterFacts.SupportsRouteKind(spec.Target, ToMetadataRouteKind(handlerKind)); |
| | | 1085 | | } |
| | | 1086 | | |
| | | 1087 | | private static bool TryPrepareCustomFilters( |
| | | 1088 | | ImmutableArray<GeneratedFilter> filters, |
| | | 1089 | | GeneratedHandlerKind handlerKind, |
| | | 1090 | | out ImmutableArray<GeneratedFilter> preparedFilters) |
| | | 1091 | | { |
| | 45 | 1092 | | ImmutableArray<GeneratedFilter>.Builder builder = ImmutableArray.CreateBuilder<GeneratedFilter>(filters.Length); |
| | | 1093 | | |
| | 142 | 1094 | | foreach (GeneratedFilter filter in filters) |
| | | 1095 | | { |
| | 26 | 1096 | | if (filter.CustomTypeName is null) |
| | | 1097 | | { |
| | 23 | 1098 | | builder.Add(filter); |
| | 23 | 1099 | | continue; |
| | | 1100 | | } |
| | | 1101 | | |
| | 3 | 1102 | | if (!TryResolveCustomFilterContextType(filter, handlerKind, out string contextTypeName)) |
| | | 1103 | | { |
| | 0 | 1104 | | preparedFilters = []; |
| | 0 | 1105 | | return false; |
| | | 1106 | | } |
| | | 1107 | | |
| | 3 | 1108 | | builder.Add(filter with { CustomContextTypeName = contextTypeName }); |
| | | 1109 | | } |
| | | 1110 | | |
| | 45 | 1111 | | preparedFilters = builder.ToImmutable(); |
| | 45 | 1112 | | return true; |
| | | 1113 | | } |
| | | 1114 | | |
| | | 1115 | | private static bool TryResolveCustomFilterContextType( |
| | | 1116 | | GeneratedFilter filter, |
| | | 1117 | | GeneratedHandlerKind handlerKind, |
| | | 1118 | | out string contextTypeName) |
| | | 1119 | | { |
| | 3 | 1120 | | string expectedContext = handlerKind switch |
| | 3 | 1121 | | { |
| | 0 | 1122 | | GeneratedHandlerKind.Callback => TelegramHandlerSymbols.CallbackQueryContext, |
| | 0 | 1123 | | GeneratedHandlerKind.ChatMember => TelegramHandlerSymbols.ChatMemberUpdatedContext, |
| | 3 | 1124 | | _ => TelegramHandlerSymbols.MessageContext |
| | 3 | 1125 | | }; |
| | | 1126 | | |
| | 3 | 1127 | | if (filter.CustomContextMetadataNames.Contains(expectedContext, StringComparer.Ordinal)) |
| | | 1128 | | { |
| | 2 | 1129 | | contextTypeName = ToFullyQualifiedTypeName(expectedContext); |
| | 2 | 1130 | | return true; |
| | | 1131 | | } |
| | | 1132 | | |
| | 1 | 1133 | | if (filter.CustomContextMetadataNames.Contains(TelegramHandlerSymbols.TelegramUpdateContext, StringComparer.Ordi |
| | | 1134 | | { |
| | 1 | 1135 | | contextTypeName = ToFullyQualifiedTypeName(TelegramHandlerSymbols.TelegramUpdateContext); |
| | 1 | 1136 | | return true; |
| | | 1137 | | } |
| | | 1138 | | |
| | 0 | 1139 | | contextTypeName = string.Empty; |
| | 0 | 1140 | | return false; |
| | | 1141 | | } |
| | | 1142 | | |
| | | 1143 | | private static string ToFullyQualifiedTypeName(string metadataName) |
| | | 1144 | | { |
| | 3 | 1145 | | return $"global::{metadataName}"; |
| | | 1146 | | } |
| | | 1147 | | |
| | | 1148 | | private static string CanonicalizeChatUsername(string? username) |
| | | 1149 | | { |
| | 1 | 1150 | | if (username is null) |
| | | 1151 | | { |
| | 0 | 1152 | | return string.Empty; |
| | | 1153 | | } |
| | | 1154 | | |
| | 1 | 1155 | | string value = username.Trim(); |
| | | 1156 | | |
| | 1 | 1157 | | if (value.StartsWith("@", StringComparison.Ordinal)) |
| | | 1158 | | { |
| | 1 | 1159 | | return value.Substring(1); |
| | | 1160 | | } |
| | | 1161 | | |
| | 0 | 1162 | | return value; |
| | | 1163 | | } |
| | | 1164 | | |
| | | 1165 | | private static bool HasChatMemberTransitionAttributes(IMethodSymbol method) |
| | | 1166 | | { |
| | 38 | 1167 | | return TelegramHandlerSymbols.HasAttribute(method.ContainingType, TelegramHandlerSymbols.ChatMemberTransitionAtt |
| | 38 | 1168 | | TelegramHandlerSymbols.HasAttribute(method, TelegramHandlerSymbols.ChatMemberTransitionAttribute, inherit |
| | 38 | 1169 | | TelegramHandlerSymbols.HasAttribute(method.ContainingType, TelegramHandlerSymbols.ChatMemberChangedAttrib |
| | 38 | 1170 | | TelegramHandlerSymbols.HasAttribute(method, TelegramHandlerSymbols.ChatMemberChangedAttribute, inherit: t |
| | | 1171 | | } |
| | | 1172 | | |
| | | 1173 | | private static bool TryBuildRoleRequirements( |
| | | 1174 | | IMethodSymbol method, |
| | | 1175 | | out ImmutableArray<int> roleRequirements) |
| | | 1176 | | { |
| | 45 | 1177 | | ImmutableArray<int>.Builder builder = ImmutableArray.CreateBuilder<int>(); |
| | | 1178 | | |
| | 94 | 1179 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes(method.ContainingType, TelegramHandlerS |
| | 45 | 1180 | | .Concat(TelegramHandlerSymbols.GetAttributes(method, TelegramHandlerSymbols.RequireTelegramRoleAttr |
| | | 1181 | | { |
| | 2 | 1182 | | if (!TelegramMemberStatusFacts.TryGetRoleRequirementMask(attribute, out int allowedStatuses) || |
| | 2 | 1183 | | !TelegramMemberStatusFacts.IsValid(allowedStatuses)) |
| | | 1184 | | { |
| | 0 | 1185 | | roleRequirements = []; |
| | 0 | 1186 | | return false; |
| | | 1187 | | } |
| | | 1188 | | |
| | 2 | 1189 | | builder.Add(allowedStatuses); |
| | | 1190 | | } |
| | | 1191 | | |
| | 45 | 1192 | | roleRequirements = builder.ToImmutable(); |
| | 45 | 1193 | | return true; |
| | 0 | 1194 | | } |
| | | 1195 | | |
| | | 1196 | | private static bool TryBuildChatMemberTransitions( |
| | | 1197 | | IMethodSymbol method, |
| | | 1198 | | out ImmutableArray<GeneratedChatMemberTransition> transitions) |
| | | 1199 | | { |
| | 7 | 1200 | | ImmutableArray<GeneratedChatMemberTransition>.Builder builder = ImmutableArray.CreateBuilder<GeneratedChatMember |
| | | 1201 | | |
| | 26 | 1202 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes(method.ContainingType, TelegramHandlerS |
| | 7 | 1203 | | .Concat(TelegramHandlerSymbols.GetAttributes(method, TelegramHandlerSymbols.ChatMemberTransitionAtt |
| | | 1204 | | { |
| | 6 | 1205 | | if (attribute.ConstructorArguments.Length == 0 || |
| | 6 | 1206 | | attribute.ConstructorArguments[0].Value is not int transition || |
| | 6 | 1207 | | !TelegramMemberStatusFacts.TryMapTransition(transition, out int oldStatus, out int newStatus)) |
| | | 1208 | | { |
| | 0 | 1209 | | transitions = []; |
| | 0 | 1210 | | return false; |
| | | 1211 | | } |
| | | 1212 | | |
| | 6 | 1213 | | builder.Add(new GeneratedChatMemberTransition(oldStatus, newStatus)); |
| | | 1214 | | } |
| | | 1215 | | |
| | 16 | 1216 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes(method.ContainingType, TelegramHandlerS |
| | 7 | 1217 | | .Concat(TelegramHandlerSymbols.GetAttributes(method, TelegramHandlerSymbols.ChatMemberChangedAttrib |
| | | 1218 | | { |
| | 1 | 1219 | | if (attribute.ConstructorArguments.Length < 2 || |
| | 1 | 1220 | | attribute.ConstructorArguments[0].Value is not int oldStatus || |
| | 1 | 1221 | | attribute.ConstructorArguments[1].Value is not int newStatus || |
| | 1 | 1222 | | !TelegramMemberStatusFacts.IsValid(oldStatus) || |
| | 1 | 1223 | | !TelegramMemberStatusFacts.IsValid(newStatus)) |
| | | 1224 | | { |
| | 0 | 1225 | | transitions = []; |
| | 0 | 1226 | | return false; |
| | | 1227 | | } |
| | | 1228 | | |
| | 1 | 1229 | | builder.Add(new GeneratedChatMemberTransition(oldStatus, newStatus)); |
| | | 1230 | | } |
| | | 1231 | | |
| | 7 | 1232 | | transitions = builder.ToImmutable(); |
| | 7 | 1233 | | return true; |
| | 0 | 1234 | | } |
| | | 1235 | | |
| | | 1236 | | private static TelegramHandlerMetadataRouteKind ToMetadataRouteKind(GeneratedHandlerKind handlerKind) |
| | | 1237 | | { |
| | 23 | 1238 | | return handlerKind switch |
| | 23 | 1239 | | { |
| | 0 | 1240 | | GeneratedHandlerKind.Command => TelegramHandlerMetadataRouteKind.Command, |
| | 19 | 1241 | | GeneratedHandlerKind.Message => TelegramHandlerMetadataRouteKind.Message, |
| | 4 | 1242 | | GeneratedHandlerKind.Callback => TelegramHandlerMetadataRouteKind.Callback, |
| | 0 | 1243 | | GeneratedHandlerKind.ChatMember => TelegramHandlerMetadataRouteKind.ChatMember, |
| | 0 | 1244 | | _ => TelegramHandlerMetadataRouteKind.Message |
| | 23 | 1245 | | }; |
| | | 1246 | | } |
| | | 1247 | | |
| | | 1248 | | private static bool IsInvalidCustomFilterType(ITypeSymbol type) |
| | | 1249 | | { |
| | 3 | 1250 | | return IsInvalidConcreteNamedType(type); |
| | | 1251 | | } |
| | | 1252 | | |
| | | 1253 | | private static bool TryGetTelegramFilterContextTypes( |
| | | 1254 | | ITypeSymbol type, |
| | | 1255 | | ITypeSymbol? attributeType, |
| | | 1256 | | out ImmutableArray<string> contextTypes) |
| | | 1257 | | { |
| | 3 | 1258 | | ImmutableArray<string>.Builder builder = ImmutableArray.CreateBuilder<string>(); |
| | | 1259 | | |
| | 12 | 1260 | | foreach (INamedTypeSymbol candidate in type.AllInterfaces) |
| | | 1261 | | { |
| | 3 | 1262 | | if (attributeType is null) |
| | | 1263 | | { |
| | 2 | 1264 | | if (string.Equals( |
| | 2 | 1265 | | candidate.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat), |
| | 2 | 1266 | | TelegramHandlerSymbols.GenericTelegramFilter, |
| | 2 | 1267 | | StringComparison.Ordinal) && |
| | 2 | 1268 | | candidate.TypeArguments.Length == 1) |
| | | 1269 | | { |
| | 2 | 1270 | | builder.Add(candidate.TypeArguments[0].ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) |
| | | 1271 | | } |
| | | 1272 | | |
| | 2 | 1273 | | continue; |
| | | 1274 | | } |
| | | 1275 | | |
| | 1 | 1276 | | if (string.Equals( |
| | 1 | 1277 | | candidate.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat), |
| | 1 | 1278 | | TelegramHandlerSymbols.GenericParameterizedTelegramFilter, |
| | 1 | 1279 | | StringComparison.Ordinal) && |
| | 1 | 1280 | | candidate.TypeArguments.Length == 2 && |
| | 1 | 1281 | | SymbolEqualityComparer.Default.Equals(candidate.TypeArguments[1], attributeType)) |
| | | 1282 | | { |
| | 1 | 1283 | | builder.Add(candidate.TypeArguments[0].ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)); |
| | | 1284 | | } |
| | | 1285 | | } |
| | | 1286 | | |
| | 3 | 1287 | | contextTypes = builder.ToImmutable(); |
| | 3 | 1288 | | return contextTypes.Length > 0; |
| | | 1289 | | } |
| | | 1290 | | |
| | | 1291 | | private static string? GetModuleName(INamedTypeSymbol handlerType) |
| | | 1292 | | { |
| | 42 | 1293 | | AttributeData? attribute = TelegramHandlerSymbols.GetFirstAttribute( |
| | 42 | 1294 | | handlerType, |
| | 42 | 1295 | | TelegramHandlerSymbols.TelegramModuleAttribute); |
| | | 1296 | | |
| | 42 | 1297 | | if (attribute?.ConstructorArguments.Length > 0 && |
| | 42 | 1298 | | attribute.ConstructorArguments[0].Value is string name && |
| | 42 | 1299 | | !string.IsNullOrWhiteSpace(name)) |
| | | 1300 | | { |
| | 5 | 1301 | | return name.Trim(); |
| | | 1302 | | } |
| | | 1303 | | |
| | 37 | 1304 | | return null; |
| | | 1305 | | } |
| | | 1306 | | |
| | | 1307 | | private static bool TryGetExpectedContextType(GeneratedHandlerKind kind, out string contextType) |
| | | 1308 | | { |
| | 40 | 1309 | | contextType = kind switch |
| | 40 | 1310 | | { |
| | 6 | 1311 | | GeneratedHandlerKind.Callback => TelegramHandlerSymbols.CallbackQueryContext, |
| | 7 | 1312 | | GeneratedHandlerKind.ChatMember => TelegramHandlerSymbols.ChatMemberUpdatedContext, |
| | 27 | 1313 | | _ => TelegramHandlerSymbols.MessageContext |
| | 40 | 1314 | | }; |
| | 40 | 1315 | | return true; |
| | | 1316 | | } |
| | | 1317 | | |
| | | 1318 | | private static bool HasExactlyOneExpectedContext(IMethodSymbol method, string expectedContextType) |
| | | 1319 | | { |
| | 40 | 1320 | | IParameterSymbol[] contextParameters = method.Parameters |
| | 40 | 1321 | | .Where(static parameter => |
| | 56 | 1322 | | TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.MessageContext) || |
| | 56 | 1323 | | TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.CallbackQueryContext) || |
| | 56 | 1324 | | TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.ChatMemberUpdatedContext) || |
| | 56 | 1325 | | TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.TelegramUpdateContext)) |
| | 40 | 1326 | | .ToArray(); |
| | | 1327 | | |
| | 40 | 1328 | | return contextParameters.Length == 1 && |
| | 40 | 1329 | | TelegramHandlerSymbols.IsType(contextParameters[0].Type, expectedContextType); |
| | | 1330 | | } |
| | | 1331 | | |
| | | 1332 | | private static bool HasValidCallbackPayloadParameter( |
| | | 1333 | | IMethodSymbol method, |
| | | 1334 | | ITypeSymbol? callbackPayloadType) |
| | | 1335 | | { |
| | 40 | 1336 | | if (callbackPayloadType is null) |
| | | 1337 | | { |
| | 37 | 1338 | | return true; |
| | | 1339 | | } |
| | | 1340 | | |
| | 3 | 1341 | | if (IsInvalidCallbackPayloadType(callbackPayloadType)) |
| | | 1342 | | { |
| | 0 | 1343 | | return false; |
| | | 1344 | | } |
| | | 1345 | | |
| | 3 | 1346 | | return method.Parameters.Count(parameter => |
| | 11 | 1347 | | SymbolEqualityComparer.Default.Equals(parameter.Type, callbackPayloadType)) == 1; |
| | | 1348 | | } |
| | | 1349 | | |
| | | 1350 | | private static bool IsInvalidCallbackPayloadType(ITypeSymbol type) |
| | | 1351 | | { |
| | 3 | 1352 | | return IsInvalidConcreteNamedType(type); |
| | | 1353 | | } |
| | | 1354 | | |
| | | 1355 | | private static bool IsAccessibleFromGeneratedCode(INamedTypeSymbol type) |
| | | 1356 | | { |
| | 8 | 1357 | | for (INamedTypeSymbol? current = type; current is not null; current = current.ContainingType) |
| | | 1358 | | { |
| | 2 | 1359 | | if (current.DeclaredAccessibility is not (Accessibility.Public or Accessibility.Internal)) |
| | | 1360 | | { |
| | 0 | 1361 | | return false; |
| | | 1362 | | } |
| | | 1363 | | } |
| | | 1364 | | |
| | 2 | 1365 | | return true; |
| | | 1366 | | } |
| | | 1367 | | |
| | | 1368 | | private static bool TryGetCallbackDataFields( |
| | | 1369 | | INamedTypeSymbol type, |
| | | 1370 | | out bool usesConstructor, |
| | | 1371 | | out ImmutableArray<GeneratedCallbackDataField> fields) |
| | | 1372 | | { |
| | 2 | 1373 | | usesConstructor = false; |
| | 2 | 1374 | | fields = ImmutableArray<GeneratedCallbackDataField>.Empty; |
| | | 1375 | | |
| | 2 | 1376 | | IMethodSymbol? constructor = type.InstanceConstructors |
| | 2 | 1377 | | .Where(static candidate => |
| | 4 | 1378 | | candidate.DeclaredAccessibility == Accessibility.Public && |
| | 4 | 1379 | | candidate.Parameters.Length > 0) |
| | 2 | 1380 | | .OrderByDescending(static candidate => candidate.Parameters.Length) |
| | 2 | 1381 | | .FirstOrDefault(); |
| | | 1382 | | |
| | 2 | 1383 | | if (constructor is not null) |
| | | 1384 | | { |
| | 2 | 1385 | | return TryGetConstructorCallbackDataFields(type, constructor, out usesConstructor, out fields); |
| | | 1386 | | } |
| | | 1387 | | |
| | 0 | 1388 | | return TryGetPropertyCallbackDataFields(type, out fields); |
| | | 1389 | | } |
| | | 1390 | | |
| | | 1391 | | private static bool TryGetConstructorCallbackDataFields( |
| | | 1392 | | INamedTypeSymbol type, |
| | | 1393 | | IMethodSymbol constructor, |
| | | 1394 | | out bool usesConstructor, |
| | | 1395 | | out ImmutableArray<GeneratedCallbackDataField> fields) |
| | | 1396 | | { |
| | 2 | 1397 | | usesConstructor = true; |
| | 2 | 1398 | | fields = ImmutableArray<GeneratedCallbackDataField>.Empty; |
| | | 1399 | | |
| | 2 | 1400 | | Dictionary<string, IPropertySymbol> properties = type |
| | 2 | 1401 | | .GetMembers() |
| | 2 | 1402 | | .OfType<IPropertySymbol>() |
| | 2 | 1403 | | .Where(static property => |
| | 10 | 1404 | | !property.IsStatic && |
| | 10 | 1405 | | property.GetMethod is { DeclaredAccessibility: Accessibility.Public }) |
| | 10 | 1406 | | .ToDictionary(static property => property.Name, StringComparer.OrdinalIgnoreCase); |
| | 2 | 1407 | | ImmutableArray<GeneratedCallbackDataField>.Builder builder = ImmutableArray.CreateBuilder<GeneratedCallbackDataF |
| | | 1408 | | |
| | 20 | 1409 | | foreach (IParameterSymbol parameter in constructor.Parameters) |
| | | 1410 | | { |
| | 8 | 1411 | | if (!properties.TryGetValue(parameter.Name, out IPropertySymbol? property) || |
| | 8 | 1412 | | !TryGetCallbackDataFieldKind(property.Type, out GeneratedCallbackDataFieldKind kind)) |
| | | 1413 | | { |
| | 0 | 1414 | | return false; |
| | | 1415 | | } |
| | | 1416 | | |
| | 8 | 1417 | | builder.Add(new GeneratedCallbackDataField( |
| | 8 | 1418 | | property.Name, |
| | 8 | 1419 | | property.Type.ToDisplayString(FullyQualifiedFormat), |
| | 8 | 1420 | | kind)); |
| | | 1421 | | } |
| | | 1422 | | |
| | 2 | 1423 | | fields = builder.ToImmutable(); |
| | 2 | 1424 | | return true; |
| | | 1425 | | } |
| | | 1426 | | |
| | | 1427 | | private static bool TryGetPropertyCallbackDataFields( |
| | | 1428 | | INamedTypeSymbol type, |
| | | 1429 | | out ImmutableArray<GeneratedCallbackDataField> fields) |
| | | 1430 | | { |
| | 0 | 1431 | | ImmutableArray<GeneratedCallbackDataField>.Builder builder = ImmutableArray.CreateBuilder<GeneratedCallbackDataF |
| | | 1432 | | |
| | 0 | 1433 | | foreach (IPropertySymbol property in type |
| | 0 | 1434 | | .GetMembers() |
| | 0 | 1435 | | .OfType<IPropertySymbol>() |
| | 0 | 1436 | | .Where(static property => |
| | 0 | 1437 | | !property.IsStatic && |
| | 0 | 1438 | | property.GetMethod is { DeclaredAccessibility: Accessibility.Public })) |
| | | 1439 | | { |
| | 0 | 1440 | | if (property.SetMethod is not { DeclaredAccessibility: Accessibility.Public } || |
| | 0 | 1441 | | !TryGetCallbackDataFieldKind(property.Type, out GeneratedCallbackDataFieldKind kind)) |
| | | 1442 | | { |
| | 0 | 1443 | | return false; |
| | | 1444 | | } |
| | | 1445 | | |
| | 0 | 1446 | | builder.Add(new GeneratedCallbackDataField( |
| | 0 | 1447 | | property.Name, |
| | 0 | 1448 | | property.Type.ToDisplayString(FullyQualifiedFormat), |
| | 0 | 1449 | | kind)); |
| | | 1450 | | } |
| | | 1451 | | |
| | 0 | 1452 | | fields = builder.ToImmutable(); |
| | 0 | 1453 | | return true; |
| | 0 | 1454 | | } |
| | | 1455 | | |
| | | 1456 | | private static bool TryGetCallbackDataFieldKind( |
| | | 1457 | | ITypeSymbol type, |
| | | 1458 | | out GeneratedCallbackDataFieldKind kind) |
| | | 1459 | | { |
| | 8 | 1460 | | kind = type.SpecialType switch |
| | 8 | 1461 | | { |
| | 2 | 1462 | | SpecialType.System_String => GeneratedCallbackDataFieldKind.String, |
| | 0 | 1463 | | SpecialType.System_Int32 => GeneratedCallbackDataFieldKind.Int32, |
| | 2 | 1464 | | SpecialType.System_Int64 => GeneratedCallbackDataFieldKind.Int64, |
| | 2 | 1465 | | SpecialType.System_Boolean => GeneratedCallbackDataFieldKind.Boolean, |
| | 2 | 1466 | | _ => default |
| | 8 | 1467 | | }; |
| | | 1468 | | |
| | 8 | 1469 | | if (type.SpecialType is SpecialType.System_String or |
| | 8 | 1470 | | SpecialType.System_Int32 or |
| | 8 | 1471 | | SpecialType.System_Int64 or |
| | 8 | 1472 | | SpecialType.System_Boolean) |
| | | 1473 | | { |
| | 6 | 1474 | | return true; |
| | | 1475 | | } |
| | | 1476 | | |
| | 2 | 1477 | | if (type.TypeKind == TypeKind.Enum) |
| | | 1478 | | { |
| | 2 | 1479 | | kind = GeneratedCallbackDataFieldKind.Enum; |
| | 2 | 1480 | | return true; |
| | | 1481 | | } |
| | | 1482 | | |
| | 0 | 1483 | | return false; |
| | | 1484 | | } |
| | | 1485 | | |
| | | 1486 | | private static bool IsInvalidConcreteNamedType(ITypeSymbol type) |
| | | 1487 | | { |
| | 6 | 1488 | | return type.TypeKind is TypeKind.Interface or TypeKind.TypeParameter || |
| | 6 | 1489 | | type is not INamedTypeSymbol namedType || |
| | 6 | 1490 | | namedType.IsAbstract || |
| | 6 | 1491 | | namedType.IsUnboundGenericType || |
| | 6 | 1492 | | namedType.TypeArguments.Any(static argument => argument.TypeKind == TypeKind.TypeParameter); |
| | | 1493 | | } |
| | | 1494 | | |
| | | 1495 | | private static bool IsSupportedReturnType(ITypeSymbol returnType) |
| | | 1496 | | { |
| | 40 | 1497 | | return TelegramHandlerSymbols.IsType(returnType, TelegramHandlerSymbols.Task) || |
| | 40 | 1498 | | TelegramHandlerSymbols.IsType(returnType, TelegramHandlerSymbols.ValueTask); |
| | | 1499 | | } |
| | | 1500 | | |
| | | 1501 | | private static bool TryGetErrorReturnKind( |
| | | 1502 | | ITypeSymbol returnType, |
| | | 1503 | | out GeneratedErrorReturnKind returnKind) |
| | | 1504 | | { |
| | 3 | 1505 | | if (TelegramHandlerSymbols.IsType(returnType, TelegramHandlerSymbols.TelegramErrorHandlingResult)) |
| | | 1506 | | { |
| | 1 | 1507 | | returnKind = GeneratedErrorReturnKind.Sync; |
| | 1 | 1508 | | return true; |
| | | 1509 | | } |
| | | 1510 | | |
| | 2 | 1511 | | if (returnType is INamedTypeSymbol |
| | 2 | 1512 | | { |
| | 2 | 1513 | | IsGenericType: true, |
| | 2 | 1514 | | TypeArguments.Length: 1 |
| | 2 | 1515 | | } namedType && |
| | 2 | 1516 | | TelegramHandlerSymbols.IsType(namedType.TypeArguments[0], TelegramHandlerSymbols.TelegramErrorHandlingResult |
| | | 1517 | | { |
| | 2 | 1518 | | GeneratedErrorReturnKind? matchedReturnKind = namedType.ConstructedFrom |
| | 2 | 1519 | | .ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) switch |
| | 2 | 1520 | | { |
| | 1 | 1521 | | "System.Threading.Tasks.Task<TResult>" => GeneratedErrorReturnKind.Task, |
| | 1 | 1522 | | "System.Threading.Tasks.ValueTask<TResult>" => GeneratedErrorReturnKind.ValueTask, |
| | 0 | 1523 | | _ => null |
| | 2 | 1524 | | }; |
| | | 1525 | | |
| | 2 | 1526 | | if (matchedReturnKind is not null) |
| | | 1527 | | { |
| | 2 | 1528 | | returnKind = matchedReturnKind.Value; |
| | 2 | 1529 | | return true; |
| | | 1530 | | } |
| | | 1531 | | } |
| | | 1532 | | |
| | 0 | 1533 | | returnKind = default; |
| | 0 | 1534 | | return false; |
| | | 1535 | | } |
| | | 1536 | | |
| | | 1537 | | private static bool IsValidCommand(string? command) |
| | | 1538 | | { |
| | 12 | 1539 | | return command is not null && |
| | 12 | 1540 | | !string.IsNullOrWhiteSpace(command) && |
| | 12 | 1541 | | !command.StartsWith("/", StringComparison.Ordinal) && |
| | 12 | 1542 | | !command.Contains("@", StringComparison.Ordinal) && |
| | 12 | 1543 | | !command.Any(char.IsWhiteSpace); |
| | | 1544 | | } |
| | | 1545 | | |
| | | 1546 | | private static string? GetConstructorString(AttributeData attribute) |
| | | 1547 | | { |
| | 25 | 1548 | | if (attribute.ConstructorArguments.Length > 0 && |
| | 25 | 1549 | | attribute.ConstructorArguments[0].Value is string value) |
| | | 1550 | | { |
| | 25 | 1551 | | return value.Trim(); |
| | | 1552 | | } |
| | | 1553 | | |
| | 0 | 1554 | | return null; |
| | | 1555 | | } |
| | | 1556 | | |
| | | 1557 | | private static bool TryGetPrefixes( |
| | | 1558 | | AttributeData attribute, |
| | | 1559 | | out ImmutableArray<string> prefixes) |
| | | 1560 | | { |
| | 16 | 1561 | | prefixes = ["/"]; |
| | | 1562 | | |
| | 40 | 1563 | | foreach (KeyValuePair<string, TypedConstant> argument in attribute.NamedArguments) |
| | | 1564 | | { |
| | 6 | 1565 | | if (!string.Equals(argument.Key, "Prefixes", StringComparison.Ordinal)) |
| | | 1566 | | { |
| | | 1567 | | continue; |
| | | 1568 | | } |
| | | 1569 | | |
| | 4 | 1570 | | if (argument.Value.Values.IsDefaultOrEmpty) |
| | | 1571 | | { |
| | 0 | 1572 | | return false; |
| | | 1573 | | } |
| | | 1574 | | |
| | 4 | 1575 | | ImmutableArray<string>.Builder builder = ImmutableArray.CreateBuilder<string>(); |
| | 4 | 1576 | | HashSet<string> seen = new HashSet<string>(StringComparer.Ordinal); |
| | | 1577 | | |
| | 19 | 1578 | | foreach (TypedConstant value in argument.Value.Values) |
| | | 1579 | | { |
| | 6 | 1580 | | if (value.Value is not string prefix || |
| | 6 | 1581 | | string.IsNullOrWhiteSpace(prefix)) |
| | | 1582 | | { |
| | 1 | 1583 | | return false; |
| | | 1584 | | } |
| | | 1585 | | |
| | 5 | 1586 | | string normalized = prefix.Trim(); |
| | | 1587 | | |
| | 5 | 1588 | | if (seen.Add(normalized)) |
| | | 1589 | | { |
| | 5 | 1590 | | builder.Add(normalized); |
| | | 1591 | | } |
| | | 1592 | | } |
| | | 1593 | | |
| | 3 | 1594 | | prefixes = builder.ToImmutable(); |
| | 3 | 1595 | | return true; |
| | | 1596 | | } |
| | | 1597 | | |
| | 12 | 1598 | | return true; |
| | | 1599 | | } |
| | | 1600 | | |
| | | 1601 | | private static int GetCommandPrefixMode(AttributeData attribute) |
| | | 1602 | | { |
| | 38 | 1603 | | foreach (KeyValuePair<string, TypedConstant> argument in attribute.NamedArguments) |
| | | 1604 | | { |
| | 4 | 1605 | | if (string.Equals(argument.Key, "PrefixMode", StringComparison.Ordinal) && |
| | 4 | 1606 | | argument.Value.Value is int value) |
| | | 1607 | | { |
| | 2 | 1608 | | return value; |
| | | 1609 | | } |
| | | 1610 | | } |
| | | 1611 | | |
| | 14 | 1612 | | return CommandPrefixModeRequired; |
| | | 1613 | | } |
| | | 1614 | | |
| | | 1615 | | private static bool HasInvalidNoPrefixConfiguration( |
| | | 1616 | | AttributeData attribute, |
| | | 1617 | | int prefixMode) |
| | | 1618 | | { |
| | 15 | 1619 | | return prefixMode == CommandPrefixModeNoPrefix && HasNamedArgument(attribute, "Prefixes"); |
| | | 1620 | | } |
| | | 1621 | | |
| | | 1622 | | private static bool IsSupportedCommandPrefixMode(int value) |
| | | 1623 | | { |
| | 15 | 1624 | | return value is CommandPrefixModeRequired or |
| | 15 | 1625 | | CommandPrefixModeOptional or |
| | 15 | 1626 | | CommandPrefixModeNoPrefix; |
| | | 1627 | | } |
| | | 1628 | | |
| | | 1629 | | private static bool HasNamedArgument( |
| | | 1630 | | AttributeData attribute, |
| | | 1631 | | string name) |
| | | 1632 | | { |
| | 3 | 1633 | | return attribute.NamedArguments.Any(argument => string.Equals(argument.Key, name, StringComparison.Ordinal)); |
| | | 1634 | | } |
| | | 1635 | | |
| | | 1636 | | private static bool CommandPatternStartsWithPrefix( |
| | | 1637 | | string pattern, |
| | | 1638 | | ImmutableArray<string> prefixes) |
| | | 1639 | | { |
| | 2 | 1640 | | string normalized = pattern.Trim(); |
| | | 1641 | | |
| | 5 | 1642 | | return prefixes.Any(prefix => normalized.StartsWith(prefix, StringComparison.Ordinal)); |
| | | 1643 | | } |
| | | 1644 | | |
| | | 1645 | | private static bool GetNamedBool( |
| | | 1646 | | AttributeData attribute, |
| | | 1647 | | string name, |
| | | 1648 | | bool defaultValue) |
| | | 1649 | | { |
| | 77 | 1650 | | foreach (KeyValuePair<string, TypedConstant> argument in attribute.NamedArguments) |
| | | 1651 | | { |
| | 8 | 1652 | | if (string.Equals(argument.Key, name, StringComparison.Ordinal) && |
| | 8 | 1653 | | argument.Value.Value is bool value) |
| | | 1654 | | { |
| | 1 | 1655 | | return value; |
| | | 1656 | | } |
| | | 1657 | | } |
| | | 1658 | | |
| | 30 | 1659 | | return defaultValue; |
| | | 1660 | | } |
| | | 1661 | | |
| | | 1662 | | private static bool TryParseTemplateRouteValues( |
| | | 1663 | | string? template, |
| | | 1664 | | out ImmutableDictionary<string, GeneratedRouteValue> routeValues) |
| | | 1665 | | { |
| | 7 | 1666 | | routeValues = ImmutableDictionary<string, GeneratedRouteValue>.Empty; |
| | | 1667 | | |
| | 7 | 1668 | | if (template is null || |
| | 7 | 1669 | | string.IsNullOrWhiteSpace(template)) |
| | | 1670 | | { |
| | 0 | 1671 | | return false; |
| | | 1672 | | } |
| | | 1673 | | |
| | 7 | 1674 | | ImmutableDictionary<string, GeneratedRouteValue>.Builder values = ImmutableDictionary.CreateBuilder<string, Gene |
| | 7 | 1675 | | Regex placeholderRegex = new Regex( |
| | 7 | 1676 | | @"\{(?<name>[A-Za-z_][A-Za-z0-9_]*)(?:(?<nameOptional>\?)|:(?<constraint>[A-Za-z][A-Za-z0-9_]*)(?<constraint |
| | 7 | 1677 | | RegexOptions.CultureInvariant); |
| | 7 | 1678 | | int position = 0; |
| | | 1679 | | |
| | 26 | 1680 | | foreach (Match match in placeholderRegex.Matches(template)) |
| | | 1681 | | { |
| | 6 | 1682 | | if (match.Index != position && |
| | 6 | 1683 | | ContainsBrace(template.Substring(position, match.Index - position))) |
| | | 1684 | | { |
| | 0 | 1685 | | return false; |
| | | 1686 | | } |
| | | 1687 | | |
| | 6 | 1688 | | string name = match.Groups["name"].Value; |
| | 6 | 1689 | | bool hasNameOptional = match.Groups["nameOptional"].Success; |
| | 6 | 1690 | | bool hasConstraintOptional = match.Groups["constraintOptional"].Success; |
| | 6 | 1691 | | string constraint = match.Groups["constraint"].Success |
| | 6 | 1692 | | ? match.Groups["constraint"].Value |
| | 6 | 1693 | | : "string"; |
| | | 1694 | | |
| | 6 | 1695 | | if (values.ContainsKey(name) || |
| | 6 | 1696 | | constraint is not ("string" or "int" or "long")) |
| | | 1697 | | { |
| | 0 | 1698 | | return false; |
| | | 1699 | | } |
| | | 1700 | | |
| | 6 | 1701 | | values.Add(name, new GeneratedRouteValue(constraint, hasNameOptional || hasConstraintOptional)); |
| | 6 | 1702 | | position = match.Index + match.Length; |
| | | 1703 | | } |
| | | 1704 | | |
| | 7 | 1705 | | if (ContainsBrace(template.Substring(position))) |
| | | 1706 | | { |
| | 1 | 1707 | | return false; |
| | | 1708 | | } |
| | | 1709 | | |
| | 6 | 1710 | | routeValues = values.ToImmutable(); |
| | 6 | 1711 | | return true; |
| | 0 | 1712 | | } |
| | | 1713 | | |
| | | 1714 | | private static bool TryParseRegexRouteValues( |
| | | 1715 | | string? pattern, |
| | | 1716 | | out ImmutableDictionary<string, GeneratedRouteValue> routeValues) |
| | | 1717 | | { |
| | 1 | 1718 | | routeValues = ImmutableDictionary<string, GeneratedRouteValue>.Empty; |
| | | 1719 | | |
| | 1 | 1720 | | if (string.IsNullOrWhiteSpace(pattern)) |
| | | 1721 | | { |
| | 0 | 1722 | | return false; |
| | | 1723 | | } |
| | | 1724 | | |
| | | 1725 | | Regex regex; |
| | | 1726 | | |
| | | 1727 | | try |
| | | 1728 | | { |
| | 1 | 1729 | | regex = new Regex(pattern, RegexOptions.CultureInvariant); |
| | 1 | 1730 | | } |
| | 0 | 1731 | | catch (ArgumentException) |
| | | 1732 | | { |
| | 0 | 1733 | | return false; |
| | | 1734 | | } |
| | | 1735 | | |
| | 1 | 1736 | | ImmutableDictionary<string, GeneratedRouteValue>.Builder values = ImmutableDictionary.CreateBuilder<string, Gene |
| | | 1737 | | |
| | 6 | 1738 | | foreach (string name in regex.GetGroupNames()) |
| | | 1739 | | { |
| | 2 | 1740 | | if (!int.TryParse(name, out _)) |
| | | 1741 | | { |
| | 1 | 1742 | | values[name] = new GeneratedRouteValue(Constraint: null, IsOptional: false); |
| | | 1743 | | } |
| | | 1744 | | } |
| | | 1745 | | |
| | 1 | 1746 | | routeValues = values.ToImmutable(); |
| | 1 | 1747 | | return true; |
| | 0 | 1748 | | } |
| | | 1749 | | |
| | | 1750 | | private static bool ContainsBrace(string value) |
| | | 1751 | | { |
| | 13 | 1752 | | return value.IndexOf("{", StringComparison.Ordinal) >= 0 || |
| | 13 | 1753 | | value.IndexOf("}", StringComparison.Ordinal) >= 0; |
| | | 1754 | | } |
| | | 1755 | | |
| | | 1756 | | private static bool TryResolveRouteValueNames( |
| | | 1757 | | IMethodSymbol method, |
| | | 1758 | | ImmutableArray<GeneratedRoute> routes, |
| | | 1759 | | out ImmutableHashSet<string> routeValueNames) |
| | | 1760 | | { |
| | 40 | 1761 | | routeValueNames = ImmutableHashSet<string>.Empty.WithComparer(StringComparer.Ordinal); |
| | | 1762 | | |
| | 40 | 1763 | | GeneratedRoute[] routeValueRoutes = routes |
| | 42 | 1764 | | .Where(static route => route.RouteValues.Count > 0) |
| | 40 | 1765 | | .ToArray(); |
| | | 1766 | | |
| | 40 | 1767 | | if (routeValueRoutes.Length == 0) |
| | | 1768 | | { |
| | 36 | 1769 | | return true; |
| | | 1770 | | } |
| | | 1771 | | |
| | 9 | 1772 | | if (routes.Any(static route => route.RouteValues.Count == 0)) |
| | | 1773 | | { |
| | 0 | 1774 | | return false; |
| | | 1775 | | } |
| | | 1776 | | |
| | 4 | 1777 | | string[] firstNames = routeValueRoutes[0].RouteValues.Keys.OrderBy(static name => name, StringComparer.Ordinal). |
| | | 1778 | | |
| | 10 | 1779 | | foreach (GeneratedRoute route in routeValueRoutes.Skip(1)) |
| | | 1780 | | { |
| | 1 | 1781 | | string[] names = route.RouteValues.Keys.OrderBy(static name => name, StringComparer.Ordinal).ToArray(); |
| | | 1782 | | |
| | 1 | 1783 | | if (!firstNames.SequenceEqual(names, StringComparer.Ordinal)) |
| | | 1784 | | { |
| | 0 | 1785 | | return false; |
| | | 1786 | | } |
| | | 1787 | | } |
| | | 1788 | | |
| | 4 | 1789 | | ImmutableHashSet<string>.Builder builder = ImmutableHashSet.CreateBuilder<string>(StringComparer.Ordinal); |
| | | 1790 | | |
| | 16 | 1791 | | foreach (string name in firstNames) |
| | | 1792 | | { |
| | 12 | 1793 | | IParameterSymbol? parameter = method.Parameters.FirstOrDefault(parameter => string.Equals(parameter.Name, na |
| | | 1794 | | |
| | 4 | 1795 | | if (parameter is null || |
| | 4 | 1796 | | !IsSupportedRouteValueParameterType(parameter.Type)) |
| | | 1797 | | { |
| | 0 | 1798 | | return false; |
| | | 1799 | | } |
| | | 1800 | | |
| | 18 | 1801 | | foreach (GeneratedRoute route in routeValueRoutes) |
| | | 1802 | | { |
| | 5 | 1803 | | GeneratedRouteValue routeValue = route.RouteValues[name]; |
| | | 1804 | | |
| | 5 | 1805 | | if (routeValue.IsOptional != IsNullableRouteValueParameter(parameter.Type)) |
| | | 1806 | | { |
| | 0 | 1807 | | return false; |
| | | 1808 | | } |
| | | 1809 | | |
| | 5 | 1810 | | if (routeValue.Constraint is not null && |
| | 5 | 1811 | | !RouteConstraintMatchesParameter(routeValue.Constraint, parameter.Type)) |
| | | 1812 | | { |
| | 0 | 1813 | | return false; |
| | | 1814 | | } |
| | | 1815 | | } |
| | | 1816 | | |
| | 4 | 1817 | | builder.Add(name); |
| | | 1818 | | } |
| | | 1819 | | |
| | 4 | 1820 | | routeValueNames = builder.ToImmutable(); |
| | 4 | 1821 | | return true; |
| | 0 | 1822 | | } |
| | | 1823 | | |
| | | 1824 | | private static bool IsSupportedRouteValueParameterType(ITypeSymbol type) |
| | | 1825 | | { |
| | 4 | 1826 | | return IsRouteValueParameterType(type); |
| | | 1827 | | } |
| | | 1828 | | |
| | | 1829 | | private static bool IsNullableRouteValueParameter(ITypeSymbol type) |
| | | 1830 | | { |
| | 5 | 1831 | | if (type.SpecialType == SpecialType.System_String) |
| | | 1832 | | { |
| | 0 | 1833 | | return type.NullableAnnotation == NullableAnnotation.Annotated; |
| | | 1834 | | } |
| | | 1835 | | |
| | 5 | 1836 | | return type is INamedTypeSymbol |
| | 5 | 1837 | | { |
| | 5 | 1838 | | OriginalDefinition.SpecialType: SpecialType.System_Nullable_T |
| | 5 | 1839 | | }; |
| | | 1840 | | } |
| | | 1841 | | |
| | | 1842 | | private static SpecialType GetRouteValueParameterSpecialType(ITypeSymbol type) |
| | | 1843 | | { |
| | 4 | 1844 | | if (type is INamedTypeSymbol |
| | 4 | 1845 | | { |
| | 4 | 1846 | | OriginalDefinition.SpecialType: SpecialType.System_Nullable_T, |
| | 4 | 1847 | | TypeArguments.Length: 1 |
| | 4 | 1848 | | } nullable) |
| | | 1849 | | { |
| | 1 | 1850 | | return nullable.TypeArguments[0].SpecialType; |
| | | 1851 | | } |
| | | 1852 | | |
| | 3 | 1853 | | return type.SpecialType; |
| | | 1854 | | } |
| | | 1855 | | |
| | | 1856 | | private static bool RouteConstraintMatchesParameter( |
| | | 1857 | | string constraint, |
| | | 1858 | | ITypeSymbol parameterType) |
| | | 1859 | | { |
| | 4 | 1860 | | SpecialType specialType = GetRouteValueParameterSpecialType(parameterType); |
| | | 1861 | | |
| | 4 | 1862 | | return constraint switch |
| | 4 | 1863 | | { |
| | 0 | 1864 | | "string" => specialType == SpecialType.System_String, |
| | 1 | 1865 | | "int" => specialType == SpecialType.System_Int32, |
| | 3 | 1866 | | "long" => specialType == SpecialType.System_Int64, |
| | 0 | 1867 | | _ => false |
| | 4 | 1868 | | }; |
| | | 1869 | | } |
| | | 1870 | | |
| | | 1871 | | private static string GetRouteValueTypeName(GeneratedRouteValue routeValue) |
| | | 1872 | | { |
| | 4 | 1873 | | return routeValue.Constraint switch |
| | 4 | 1874 | | { |
| | 1 | 1875 | | "int" => "int", |
| | 3 | 1876 | | "long" => "long", |
| | 0 | 1877 | | _ => "string" |
| | 4 | 1878 | | }; |
| | | 1879 | | } |
| | | 1880 | | |
| | | 1881 | | private static ImmutableArray<GeneratedTextFilter> BuildTextFilters( |
| | | 1882 | | IMethodSymbol method, |
| | | 1883 | | bool includeClassRouteAttributes) |
| | | 1884 | | { |
| | 29 | 1885 | | ImmutableArray<GeneratedTextFilter>.Builder builder = ImmutableArray.CreateBuilder<GeneratedTextFilter>(); |
| | | 1886 | | |
| | 62 | 1887 | | foreach (AttributeData attribute in GetRouteAttributes( |
| | 29 | 1888 | | method, |
| | 29 | 1889 | | TelegramHandlerSymbols.TextAttribute, |
| | 29 | 1890 | | includeClassRouteAttributes)) |
| | | 1891 | | { |
| | 2 | 1892 | | string? value = attribute.ConstructorArguments.Length > 0 |
| | 2 | 1893 | | ? attribute.ConstructorArguments[0].Value as string |
| | 2 | 1894 | | : null; |
| | | 1895 | | |
| | 2 | 1896 | | if (value is null || |
| | 2 | 1897 | | string.IsNullOrWhiteSpace(value)) |
| | | 1898 | | { |
| | | 1899 | | continue; |
| | | 1900 | | } |
| | | 1901 | | |
| | 2 | 1902 | | int mode = attribute.ConstructorArguments.Length > 1 && |
| | 2 | 1903 | | attribute.ConstructorArguments[1].Value is int modeValue |
| | 2 | 1904 | | ? modeValue |
| | 2 | 1905 | | : 0; |
| | 2 | 1906 | | bool ignoreCase = attribute.ConstructorArguments.Length > 2 && |
| | 2 | 1907 | | attribute.ConstructorArguments[2].Value is bool ignoreCaseValue |
| | 2 | 1908 | | ? ignoreCaseValue |
| | 2 | 1909 | | : true; |
| | | 1910 | | |
| | 2 | 1911 | | builder.Add(new GeneratedTextFilter(value, mode, ignoreCase)); |
| | | 1912 | | } |
| | | 1913 | | |
| | 29 | 1914 | | return builder.ToImmutable(); |
| | | 1915 | | } |
| | | 1916 | | |
| | | 1917 | | private static IReadOnlyList<AttributeData> GetRouteAttributes( |
| | | 1918 | | IMethodSymbol method, |
| | | 1919 | | string metadataName, |
| | | 1920 | | bool includeClassRouteAttributes) |
| | | 1921 | | { |
| | 657 | 1922 | | IReadOnlyList<AttributeData> methodAttributes = TelegramHandlerSymbols.GetAttributes(method, metadataName, inher |
| | | 1923 | | |
| | 657 | 1924 | | if (!includeClassRouteAttributes) |
| | | 1925 | | { |
| | 603 | 1926 | | return methodAttributes; |
| | | 1927 | | } |
| | | 1928 | | |
| | 54 | 1929 | | IReadOnlyList<AttributeData> typeAttributes = TelegramHandlerSymbols.GetAttributes(method.ContainingType, metada |
| | | 1930 | | |
| | 54 | 1931 | | return typeAttributes.Count == 0 |
| | 54 | 1932 | | ? methodAttributes |
| | 54 | 1933 | | : typeAttributes.Concat(methodAttributes).ToArray(); |
| | | 1934 | | } |
| | | 1935 | | |
| | | 1936 | | private static IReadOnlyList<AttributeData> GetGenericRouteAttributes( |
| | | 1937 | | IMethodSymbol method, |
| | | 1938 | | string metadataName, |
| | | 1939 | | bool includeClassRouteAttributes) |
| | | 1940 | | { |
| | 45 | 1941 | | IReadOnlyList<AttributeData> methodAttributes = TelegramHandlerSymbols.GetGenericAttributes(method, metadataName |
| | | 1942 | | |
| | 45 | 1943 | | if (!includeClassRouteAttributes) |
| | | 1944 | | { |
| | 41 | 1945 | | return methodAttributes; |
| | | 1946 | | } |
| | | 1947 | | |
| | 4 | 1948 | | IReadOnlyList<AttributeData> typeAttributes = TelegramHandlerSymbols.GetGenericAttributes(method.ContainingType, |
| | | 1949 | | |
| | 4 | 1950 | | return typeAttributes.Count == 0 |
| | 4 | 1951 | | ? methodAttributes |
| | 4 | 1952 | | : typeAttributes.Concat(methodAttributes).ToArray(); |
| | | 1953 | | } |
| | | 1954 | | |
| | | 1955 | | private static bool IsClassBasedHandlerType(INamedTypeSymbol type) |
| | | 1956 | | { |
| | 143 | 1957 | | return IsAssignableTo(type, TelegramHandlerSymbols.MessageHandler) || |
| | 143 | 1958 | | IsAssignableTo(type, TelegramHandlerSymbols.CallbackHandler) || |
| | 143 | 1959 | | IsAssignableTo(type, TelegramHandlerSymbols.ChatMemberUpdateHandler); |
| | | 1960 | | } |
| | | 1961 | | |
| | | 1962 | | private static bool IsClassBasedRouteCompatible( |
| | | 1963 | | INamedTypeSymbol type, |
| | | 1964 | | GeneratedHandlerKind kind, |
| | | 1965 | | ITypeSymbol? callbackPayloadType) |
| | | 1966 | | { |
| | 40 | 1967 | | if (!IsClassBasedHandlerType(type)) |
| | | 1968 | | { |
| | 36 | 1969 | | return true; |
| | | 1970 | | } |
| | | 1971 | | |
| | 4 | 1972 | | if (IsAssignableTo(type, TelegramHandlerSymbols.MessageHandler)) |
| | | 1973 | | { |
| | 2 | 1974 | | return kind is GeneratedHandlerKind.Command or GeneratedHandlerKind.Message; |
| | | 1975 | | } |
| | | 1976 | | |
| | 2 | 1977 | | if (TryGetCallbackHandlerPayloadType(type, out ITypeSymbol handlerPayloadType)) |
| | | 1978 | | { |
| | 1 | 1979 | | return kind == GeneratedHandlerKind.Callback && |
| | 1 | 1980 | | callbackPayloadType is not null && |
| | 1 | 1981 | | SymbolEqualityComparer.Default.Equals(handlerPayloadType, callbackPayloadType); |
| | | 1982 | | } |
| | | 1983 | | |
| | 1 | 1984 | | if (IsDirectlyAssignableTo(type, TelegramHandlerSymbols.CallbackHandler)) |
| | | 1985 | | { |
| | 0 | 1986 | | return kind == GeneratedHandlerKind.Callback && |
| | 0 | 1987 | | callbackPayloadType is null; |
| | | 1988 | | } |
| | | 1989 | | |
| | 1 | 1990 | | return IsAssignableTo(type, TelegramHandlerSymbols.ChatMemberUpdateHandler) && |
| | 1 | 1991 | | kind == GeneratedHandlerKind.ChatMember; |
| | | 1992 | | } |
| | | 1993 | | |
| | | 1994 | | private static ImmutableArray<IMethodSymbol> GetDeclaredHandleAsyncMethods(INamedTypeSymbol type) |
| | | 1995 | | { |
| | 4 | 1996 | | return type.GetMembers("HandleAsync") |
| | 4 | 1997 | | .OfType<IMethodSymbol>() |
| | 4 | 1998 | | .Where(static method => |
| | 4 | 1999 | | method.DeclaredAccessibility == Accessibility.Public && |
| | 4 | 2000 | | !method.IsStatic && |
| | 4 | 2001 | | method.MethodKind == MethodKind.Ordinary && |
| | 4 | 2002 | | !method.IsGenericMethod) |
| | 4 | 2003 | | .ToImmutableArray(); |
| | | 2004 | | } |
| | | 2005 | | |
| | | 2006 | | private static bool TryGetCallbackHandlerPayloadType( |
| | | 2007 | | INamedTypeSymbol type, |
| | | 2008 | | out ITypeSymbol payloadType) |
| | | 2009 | | { |
| | 12 | 2010 | | for (INamedTypeSymbol? current = type; current is not null; current = current.BaseType) |
| | | 2011 | | { |
| | 5 | 2012 | | if (current.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) == |
| | 5 | 2013 | | TelegramHandlerSymbols.GenericCallbackHandler && |
| | 5 | 2014 | | current.TypeArguments.Length == 1) |
| | | 2015 | | { |
| | 1 | 2016 | | payloadType = current.TypeArguments[0]; |
| | 1 | 2017 | | return true; |
| | | 2018 | | } |
| | | 2019 | | } |
| | | 2020 | | |
| | 1 | 2021 | | payloadType = null!; |
| | 1 | 2022 | | return false; |
| | | 2023 | | } |
| | | 2024 | | |
| | | 2025 | | private static bool IsAssignableTo(INamedTypeSymbol type, string metadataName) |
| | | 2026 | | { |
| | 2830 | 2027 | | for (INamedTypeSymbol? current = type; current is not null; current = current.BaseType) |
| | | 2028 | | { |
| | 987 | 2029 | | if (current.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) == metadataName |
| | 987 | 2030 | | current.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) == metadataName) |
| | | 2031 | | { |
| | 27 | 2032 | | return true; |
| | | 2033 | | } |
| | | 2034 | | } |
| | | 2035 | | |
| | 428 | 2036 | | return false; |
| | | 2037 | | } |
| | | 2038 | | |
| | | 2039 | | private static bool IsDirectlyAssignableTo(INamedTypeSymbol type, string metadataName) |
| | | 2040 | | { |
| | 1 | 2041 | | return IsAssignableTo(type, metadataName) && |
| | 1 | 2042 | | !TryGetCallbackHandlerPayloadType(type, out _); |
| | | 2043 | | } |
| | | 2044 | | |
| | | 2045 | | private static bool TryBuildStates( |
| | | 2046 | | IMethodSymbol method, |
| | | 2047 | | out ImmutableArray<string> states, |
| | | 2048 | | out string? sceneName) |
| | | 2049 | | { |
| | 40 | 2050 | | ImmutableArray<string>.Builder builder = ImmutableArray.CreateBuilder<string>(); |
| | 40 | 2051 | | states = []; |
| | 40 | 2052 | | sceneName = null; |
| | | 2053 | | |
| | 88 | 2054 | | foreach (AttributeData attribute in TelegramHandlerSymbols.GetAttributes(method.ContainingType, TelegramHandlerS |
| | 40 | 2055 | | .Concat(TelegramHandlerSymbols.GetAttributes(method, TelegramHandlerSymbols.StateAttribute, inherit |
| | | 2056 | | { |
| | 4 | 2057 | | if (attribute.ConstructorArguments.Length > 0 && |
| | 4 | 2058 | | attribute.ConstructorArguments[0].Value is string state && |
| | 4 | 2059 | | !string.IsNullOrWhiteSpace(state)) |
| | | 2060 | | { |
| | 4 | 2061 | | builder.Add(state); |
| | | 2062 | | } |
| | | 2063 | | } |
| | | 2064 | | |
| | 40 | 2065 | | AttributeData[] hasGenericStateAttributes = GetGenericStateAttributes(method.ContainingType) |
| | 40 | 2066 | | .Concat(GetGenericStateAttributes(method)) |
| | 40 | 2067 | | .ToArray(); |
| | | 2068 | | |
| | 82 | 2069 | | foreach (AttributeData attribute in hasGenericStateAttributes) |
| | | 2070 | | { |
| | 1 | 2071 | | if (TryResolveTypedState(attribute, out string state)) |
| | | 2072 | | { |
| | 1 | 2073 | | builder.Add(state); |
| | | 2074 | | } |
| | | 2075 | | } |
| | | 2076 | | |
| | 40 | 2077 | | AttributeData? sceneStep = TelegramHandlerSymbols.GetFirstAttribute( |
| | 40 | 2078 | | method, |
| | 40 | 2079 | | TelegramHandlerSymbols.SceneStepAttribute, |
| | 40 | 2080 | | inherit: true); |
| | | 2081 | | |
| | 40 | 2082 | | if (sceneStep is not null) |
| | | 2083 | | { |
| | 2 | 2084 | | if (builder.Count > 0 || |
| | 2 | 2085 | | !TryResolveSceneStep(method, sceneStep, out string sceneState, out sceneName)) |
| | | 2086 | | { |
| | 1 | 2087 | | return false; |
| | | 2088 | | } |
| | | 2089 | | |
| | 1 | 2090 | | builder.Add(sceneState); |
| | | 2091 | | } |
| | | 2092 | | |
| | 39 | 2093 | | states = builder.ToImmutable(); |
| | 39 | 2094 | | return true; |
| | | 2095 | | } |
| | | 2096 | | |
| | | 2097 | | private static IEnumerable<AttributeData> GetGenericStateAttributes(ISymbol symbol) |
| | | 2098 | | { |
| | 80 | 2099 | | return TelegramHandlerSymbols.GetGenericAttributes( |
| | 80 | 2100 | | symbol, |
| | 80 | 2101 | | TelegramHandlerSymbols.GenericStateAttribute, |
| | 80 | 2102 | | inherit: true); |
| | | 2103 | | } |
| | | 2104 | | |
| | | 2105 | | private static bool TryResolveTypedState(AttributeData attribute, out string state) |
| | | 2106 | | { |
| | 1 | 2107 | | state = string.Empty; |
| | | 2108 | | |
| | 1 | 2109 | | if (attribute.AttributeClass is not { TypeArguments.Length: 1 } attributeType || |
| | 1 | 2110 | | attribute.ConstructorArguments.Length == 0 || |
| | 1 | 2111 | | attribute.ConstructorArguments[0].Value is not string stateName || |
| | 1 | 2112 | | string.IsNullOrWhiteSpace(stateName) || |
| | 1 | 2113 | | attributeType.TypeArguments[0] is not INamedTypeSymbol groupType) |
| | | 2114 | | { |
| | 0 | 2115 | | return false; |
| | | 2116 | | } |
| | | 2117 | | |
| | 1 | 2118 | | if (!TryGetStateContainerPrefix(groupType, out string prefix)) |
| | | 2119 | | { |
| | 0 | 2120 | | return false; |
| | | 2121 | | } |
| | | 2122 | | |
| | 1 | 2123 | | IPropertySymbol? property = groupType |
| | 1 | 2124 | | .GetMembers(stateName) |
| | 1 | 2125 | | .OfType<IPropertySymbol>() |
| | 1 | 2126 | | .FirstOrDefault(IsUsableStateProperty); |
| | | 2127 | | |
| | 1 | 2128 | | if (property is null || !IsPartialStateProperty(property)) |
| | | 2129 | | { |
| | 0 | 2130 | | return false; |
| | | 2131 | | } |
| | | 2132 | | |
| | 1 | 2133 | | state = $"{prefix}:{GetStateSegment(property)}"; |
| | 1 | 2134 | | return true; |
| | | 2135 | | } |
| | | 2136 | | |
| | | 2137 | | private static bool TryResolveSceneStep( |
| | | 2138 | | IMethodSymbol method, |
| | | 2139 | | AttributeData attribute, |
| | | 2140 | | out string state, |
| | | 2141 | | out string? sceneName) |
| | | 2142 | | { |
| | 2 | 2143 | | state = string.Empty; |
| | 2 | 2144 | | sceneName = null; |
| | | 2145 | | |
| | 2 | 2146 | | if (!TryGetScenePrefix(method.ContainingType, out string prefix) || |
| | 2 | 2147 | | attribute.ConstructorArguments.Length == 0 || |
| | 2 | 2148 | | attribute.ConstructorArguments[0].Value is not string stateName || |
| | 2 | 2149 | | string.IsNullOrWhiteSpace(stateName)) |
| | | 2150 | | { |
| | 0 | 2151 | | return false; |
| | | 2152 | | } |
| | | 2153 | | |
| | 2 | 2154 | | IPropertySymbol? property = method.ContainingType |
| | 2 | 2155 | | .GetMembers(stateName) |
| | 2 | 2156 | | .OfType<IPropertySymbol>() |
| | 2 | 2157 | | .FirstOrDefault(IsUsableStateProperty); |
| | | 2158 | | |
| | 2 | 2159 | | if (property is null || !IsPartialStateProperty(property)) |
| | | 2160 | | { |
| | 1 | 2161 | | return false; |
| | | 2162 | | } |
| | | 2163 | | |
| | 1 | 2164 | | sceneName = prefix; |
| | 1 | 2165 | | state = $"{prefix}:{GetStateSegment(property)}"; |
| | 1 | 2166 | | return true; |
| | | 2167 | | } |
| | | 2168 | | |
| | | 2169 | | private static bool TryGetStateContainerPrefix(INamedTypeSymbol type, out string prefix) |
| | | 2170 | | { |
| | 20 | 2171 | | return TryGetStateGroupPrefix(type, out prefix) || |
| | 20 | 2172 | | TryGetScenePrefix(type, out prefix); |
| | | 2173 | | } |
| | | 2174 | | |
| | | 2175 | | private static bool TryGetStateGroupPrefix(INamedTypeSymbol type, out string prefix) |
| | | 2176 | | { |
| | 20 | 2177 | | return TryGetAttributeStringPrefix(type, TelegramHandlerSymbols.StateGroupAttribute, out prefix); |
| | | 2178 | | } |
| | | 2179 | | |
| | | 2180 | | private static bool TryGetScenePrefix(INamedTypeSymbol type, out string prefix) |
| | | 2181 | | { |
| | 20 | 2182 | | return TryGetAttributeStringPrefix(type, TelegramHandlerSymbols.SceneAttribute, out prefix); |
| | | 2183 | | } |
| | | 2184 | | |
| | | 2185 | | private static bool TryGetAttributeStringPrefix( |
| | | 2186 | | INamedTypeSymbol type, |
| | | 2187 | | string metadataName, |
| | | 2188 | | out string prefix) |
| | | 2189 | | { |
| | 40 | 2190 | | AttributeData? attribute = TelegramHandlerSymbols.GetFirstAttribute(type, metadataName); |
| | | 2191 | | |
| | 40 | 2192 | | if (attribute?.ConstructorArguments.Length > 0 && |
| | 40 | 2193 | | attribute.ConstructorArguments[0].Value is string value && |
| | 40 | 2194 | | !string.IsNullOrWhiteSpace(value)) |
| | | 2195 | | { |
| | 7 | 2196 | | prefix = value; |
| | 7 | 2197 | | return true; |
| | | 2198 | | } |
| | | 2199 | | |
| | 33 | 2200 | | prefix = string.Empty; |
| | 33 | 2201 | | return false; |
| | | 2202 | | } |
| | | 2203 | | |
| | | 2204 | | private static string GetStateSegment(IPropertySymbol property) |
| | | 2205 | | { |
| | 8 | 2206 | | AttributeData? attribute = TelegramHandlerSymbols.GetFirstAttribute( |
| | 8 | 2207 | | property, |
| | 8 | 2208 | | TelegramHandlerSymbols.StateValueAttribute); |
| | | 2209 | | |
| | 8 | 2210 | | if (attribute?.ConstructorArguments.Length > 0 && |
| | 8 | 2211 | | attribute.ConstructorArguments[0].Value is string value && |
| | 8 | 2212 | | !string.IsNullOrWhiteSpace(value)) |
| | | 2213 | | { |
| | 2 | 2214 | | return value; |
| | | 2215 | | } |
| | | 2216 | | |
| | 6 | 2217 | | return ToCamelCase(property.Name); |
| | | 2218 | | } |
| | | 2219 | | |
| | | 2220 | | private static bool IsUsableStateProperty(IPropertySymbol property) |
| | | 2221 | | { |
| | 11 | 2222 | | return property.DeclaredAccessibility == Accessibility.Public && |
| | 11 | 2223 | | property.IsStatic && |
| | 11 | 2224 | | TelegramHandlerSymbols.IsType(property.Type, TelegramHandlerSymbols.State); |
| | | 2225 | | } |
| | | 2226 | | |
| | | 2227 | | private static bool IsPartialStateProperty(IPropertySymbol property) |
| | | 2228 | | { |
| | 9 | 2229 | | return property |
| | 9 | 2230 | | .DeclaringSyntaxReferences |
| | 9 | 2231 | | .Select(static reference => reference.GetSyntax()) |
| | 9 | 2232 | | .OfType<PropertyDeclarationSyntax>() |
| | 44 | 2233 | | .Any(static declaration => declaration.Modifiers.Any(modifier => modifier.Text == "partial")); |
| | | 2234 | | } |
| | | 2235 | | |
| | | 2236 | | private static string ToCamelCase(string value) |
| | | 2237 | | { |
| | 6 | 2238 | | return value.Length == 0 |
| | 6 | 2239 | | ? value |
| | 6 | 2240 | | : char.ToLowerInvariant(value[0]) + value.Substring(1); |
| | | 2241 | | } |
| | | 2242 | | |
| | | 2243 | | private static ImmutableArray<GeneratedParameter> BuildParameters( |
| | | 2244 | | IMethodSymbol method, |
| | | 2245 | | GeneratedHandlerKind kind, |
| | | 2246 | | string expectedContextType, |
| | | 2247 | | ITypeSymbol? callbackPayloadType, |
| | | 2248 | | ImmutableHashSet<string> routeValueNames) |
| | | 2249 | | { |
| | 39 | 2250 | | ImmutableArray<GeneratedParameter>.Builder builder = ImmutableArray.CreateBuilder<GeneratedParameter>(); |
| | | 2251 | | |
| | 188 | 2252 | | foreach (IParameterSymbol parameter in method.Parameters) |
| | | 2253 | | { |
| | 55 | 2254 | | GeneratedParameterKind parameterKind = GetParameterKind(parameter, kind, expectedContextType, callbackPayloa |
| | | 2255 | | |
| | 55 | 2256 | | builder.Add(new GeneratedParameter( |
| | 55 | 2257 | | parameter.Type.ToDisplayString(FullyQualifiedFormat), |
| | 55 | 2258 | | parameterKind, |
| | 55 | 2259 | | parameter.Name)); |
| | | 2260 | | } |
| | | 2261 | | |
| | 39 | 2262 | | return builder.ToImmutable(); |
| | | 2263 | | } |
| | | 2264 | | |
| | | 2265 | | private static GeneratedParameterKind GetParameterKind( |
| | | 2266 | | IParameterSymbol parameter, |
| | | 2267 | | GeneratedHandlerKind kind, |
| | | 2268 | | string expectedContextType, |
| | | 2269 | | ITypeSymbol? callbackPayloadType, |
| | | 2270 | | ImmutableHashSet<string> routeValueNames) |
| | | 2271 | | { |
| | 55 | 2272 | | if (TelegramHandlerSymbols.IsType(parameter.Type, expectedContextType)) |
| | | 2273 | | { |
| | 39 | 2274 | | return GeneratedParameterKind.Context; |
| | | 2275 | | } |
| | | 2276 | | |
| | 16 | 2277 | | if (TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.CancellationToken)) |
| | | 2278 | | { |
| | 1 | 2279 | | return GeneratedParameterKind.CancellationToken; |
| | | 2280 | | } |
| | | 2281 | | |
| | 15 | 2282 | | if (routeValueNames.Contains(parameter.Name)) |
| | | 2283 | | { |
| | 4 | 2284 | | return GeneratedParameterKind.RouteValue; |
| | | 2285 | | } |
| | | 2286 | | |
| | 11 | 2287 | | if (kind == GeneratedHandlerKind.Callback && |
| | 11 | 2288 | | callbackPayloadType is not null && |
| | 11 | 2289 | | SymbolEqualityComparer.Default.Equals(parameter.Type, callbackPayloadType)) |
| | | 2290 | | { |
| | 3 | 2291 | | return GeneratedParameterKind.CallbackPayload; |
| | | 2292 | | } |
| | | 2293 | | |
| | 8 | 2294 | | return GeneratedParameterKind.Service; |
| | | 2295 | | } |
| | | 2296 | | |
| | | 2297 | | private static ImmutableArray<GeneratedErrorParameter> BuildErrorParameters(IMethodSymbol method) |
| | | 2298 | | { |
| | 3 | 2299 | | ImmutableArray<GeneratedErrorParameter>.Builder builder = ImmutableArray.CreateBuilder<GeneratedErrorParameter>( |
| | | 2300 | | |
| | 28 | 2301 | | foreach (IParameterSymbol parameter in method.Parameters) |
| | | 2302 | | { |
| | 11 | 2303 | | builder.Add(new GeneratedErrorParameter( |
| | 11 | 2304 | | parameter.Type.ToDisplayString(FullyQualifiedFormat), |
| | 11 | 2305 | | GetErrorParameterKind(parameter), |
| | 11 | 2306 | | parameter.Name)); |
| | | 2307 | | } |
| | | 2308 | | |
| | 3 | 2309 | | return builder.ToImmutable(); |
| | | 2310 | | } |
| | | 2311 | | |
| | | 2312 | | private static GeneratedErrorParameterKind GetErrorParameterKind(IParameterSymbol parameter) |
| | | 2313 | | { |
| | 11 | 2314 | | if (TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.TelegramErrorContext)) |
| | | 2315 | | { |
| | 2 | 2316 | | return GeneratedErrorParameterKind.ErrorContext; |
| | | 2317 | | } |
| | | 2318 | | |
| | 9 | 2319 | | if (TelegramHandlerSymbols.IsType(parameter.Type, TelegramHandlerSymbols.CancellationToken)) |
| | | 2320 | | { |
| | 2 | 2321 | | return GeneratedErrorParameterKind.CancellationToken; |
| | | 2322 | | } |
| | | 2323 | | |
| | 7 | 2324 | | if (IsExceptionType(parameter.Type)) |
| | | 2325 | | { |
| | 3 | 2326 | | return GeneratedErrorParameterKind.Exception; |
| | | 2327 | | } |
| | | 2328 | | |
| | 4 | 2329 | | if (IsTelegramContextType(parameter.Type)) |
| | | 2330 | | { |
| | 2 | 2331 | | return GeneratedErrorParameterKind.TelegramContext; |
| | | 2332 | | } |
| | | 2333 | | |
| | 2 | 2334 | | if (IsErrorRouteValueParameterType(parameter.Type)) |
| | | 2335 | | { |
| | 1 | 2336 | | return GeneratedErrorParameterKind.RouteValue; |
| | | 2337 | | } |
| | | 2338 | | |
| | 1 | 2339 | | return GeneratedErrorParameterKind.Service; |
| | | 2340 | | } |
| | | 2341 | | |
| | | 2342 | | private static bool IsExceptionType(ITypeSymbol type) |
| | | 2343 | | { |
| | 20 | 2344 | | return type is INamedTypeSymbol namedType && |
| | 20 | 2345 | | IsAssignableTo(namedType, TelegramHandlerSymbols.Exception); |
| | | 2346 | | } |
| | | 2347 | | |
| | | 2348 | | private static bool IsTelegramContextType(ITypeSymbol type) |
| | | 2349 | | { |
| | 15 | 2350 | | return type is INamedTypeSymbol namedType && |
| | 15 | 2351 | | IsAssignableTo(namedType, TelegramHandlerSymbols.TelegramUpdateContext); |
| | | 2352 | | } |
| | | 2353 | | |
| | | 2354 | | private static bool IsAssignableFrom(ITypeSymbol targetType, ITypeSymbol sourceType) |
| | | 2355 | | { |
| | 2 | 2356 | | if (sourceType is not INamedTypeSymbol source) |
| | | 2357 | | { |
| | 0 | 2358 | | return false; |
| | | 2359 | | } |
| | | 2360 | | |
| | 4 | 2361 | | for (INamedTypeSymbol? current = source; current is not null; current = current.BaseType) |
| | | 2362 | | { |
| | 2 | 2363 | | if (SymbolEqualityComparer.Default.Equals(current, targetType)) |
| | | 2364 | | { |
| | 2 | 2365 | | return true; |
| | | 2366 | | } |
| | | 2367 | | } |
| | | 2368 | | |
| | 0 | 2369 | | return false; |
| | | 2370 | | } |
| | | 2371 | | |
| | | 2372 | | private static bool IsErrorRouteValueParameterType(ITypeSymbol type) |
| | | 2373 | | { |
| | 2 | 2374 | | return IsRouteValueParameterType(type); |
| | | 2375 | | } |
| | | 2376 | | |
| | | 2377 | | private static bool IsRouteValueParameterType(ITypeSymbol type) |
| | | 2378 | | { |
| | 6 | 2379 | | return type.SpecialType is |
| | 6 | 2380 | | SpecialType.System_String or |
| | 6 | 2381 | | SpecialType.System_Int32 or |
| | 6 | 2382 | | SpecialType.System_Int64 || |
| | 6 | 2383 | | type is INamedTypeSymbol |
| | 6 | 2384 | | { |
| | 6 | 2385 | | OriginalDefinition.SpecialType: SpecialType.System_Nullable_T, |
| | 6 | 2386 | | TypeArguments.Length: 1 |
| | 6 | 2387 | | } nullable && |
| | 6 | 2388 | | nullable.TypeArguments[0].SpecialType is |
| | 6 | 2389 | | SpecialType.System_Int32 or |
| | 6 | 2390 | | SpecialType.System_Int64; |
| | | 2391 | | } |
| | | 2392 | | } |