| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 3 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 4 | | |
| | | 5 | | using NexusLabs.Needlr.Generators.Models; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.Generators; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Helper for discovering <c>[HttpClientOptions]</c> attributes and the associated |
| | | 11 | | /// capability interfaces on Roslyn symbols. Also resolves the HttpClient name from the |
| | | 12 | | /// three supported sources (attribute argument, <c>ClientName</c> property, type-name |
| | | 13 | | /// inference). |
| | | 14 | | /// </summary> |
| | | 15 | | internal static class HttpClientOptionsAttributeHelper |
| | | 16 | | { |
| | | 17 | | private const string HttpClientOptionsAttributeName = "HttpClientOptionsAttribute"; |
| | | 18 | | private const string GeneratorsNamespace = "NexusLabs.Needlr.Generators"; |
| | | 19 | | |
| | | 20 | | private const string INamedHttpClientOptionsName = "INamedHttpClientOptions"; |
| | | 21 | | private const string IHttpClientTimeoutName = "IHttpClientTimeout"; |
| | | 22 | | private const string IHttpClientUserAgentName = "IHttpClientUserAgent"; |
| | | 23 | | private const string IHttpClientBaseAddressName = "IHttpClientBaseAddress"; |
| | | 24 | | private const string IHttpClientDefaultHeadersName = "IHttpClientDefaultHeaders"; |
| | | 25 | | |
| | | 26 | | /// <summary>Suffixes stripped from the type name (in order) when inferring the client name.</summary> |
| | 0 | 27 | | private static readonly string[] ClientNameSuffixes = ["HttpClientOptions", "HttpClientSettings", "HttpClient"]; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Extracted state from an <c>[HttpClientOptions]</c> attribute on a type. |
| | | 31 | | /// </summary> |
| | | 32 | | public readonly struct HttpClientOptionsAttributeInfo |
| | | 33 | | { |
| | | 34 | | public HttpClientOptionsAttributeInfo(string? sectionName, string? name, Location? attributeLocation) |
| | | 35 | | { |
| | 0 | 36 | | SectionName = sectionName; |
| | 0 | 37 | | Name = name; |
| | 0 | 38 | | AttributeLocation = attributeLocation; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary>Explicit section name from the attribute, or null to infer.</summary> |
| | 0 | 42 | | public string? SectionName { get; } |
| | | 43 | | |
| | | 44 | | /// <summary>Explicit client name override from the attribute, or null to fall through.</summary> |
| | 0 | 45 | | public string? Name { get; } |
| | | 46 | | |
| | | 47 | | /// <summary>Source location of the attribute, used for analyzer diagnostics.</summary> |
| | 0 | 48 | | public Location? AttributeLocation { get; } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Checks if a type has the <c>[HttpClientOptions]</c> attribute. |
| | | 53 | | /// </summary> |
| | | 54 | | public static bool HasHttpClientOptionsAttribute(INamedTypeSymbol typeSymbol) |
| | | 55 | | { |
| | 7002772 | 56 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 57 | | { |
| | 2044129 | 58 | | if (IsHttpClientOptionsAttribute(attribute.AttributeClass)) |
| | 0 | 59 | | return true; |
| | | 60 | | } |
| | | 61 | | |
| | 1457257 | 62 | | return false; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Extracts the <c>[HttpClientOptions]</c> attribute info from a type, or <c>null</c> |
| | | 67 | | /// if the attribute is not present. |
| | | 68 | | /// </summary> |
| | | 69 | | public static HttpClientOptionsAttributeInfo? GetHttpClientOptionsAttribute(INamedTypeSymbol typeSymbol) |
| | | 70 | | { |
| | 0 | 71 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 72 | | { |
| | 0 | 73 | | if (!IsHttpClientOptionsAttribute(attribute.AttributeClass)) |
| | | 74 | | continue; |
| | | 75 | | |
| | 0 | 76 | | string? sectionName = null; |
| | 0 | 77 | | if (attribute.ConstructorArguments.Length > 0 && |
| | 0 | 78 | | attribute.ConstructorArguments[0].Value is string section) |
| | | 79 | | { |
| | 0 | 80 | | sectionName = section; |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | string? name = null; |
| | 0 | 84 | | foreach (var namedArg in attribute.NamedArguments) |
| | | 85 | | { |
| | 0 | 86 | | if (namedArg.Key == "Name" && namedArg.Value.Value is string n) |
| | | 87 | | { |
| | 0 | 88 | | name = n; |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | var location = attribute.ApplicationSyntaxReference?.GetSyntax().GetLocation(); |
| | 0 | 93 | | return new HttpClientOptionsAttributeInfo(sectionName, name, location); |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return null; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Detects which v1 capability interfaces the type implements. Returns a bit flag set |
| | | 101 | | /// which drives the conditional emission in <c>HttpClientCodeGenerator</c>. |
| | | 102 | | /// </summary> |
| | | 103 | | public static HttpClientCapabilities DetectCapabilities(INamedTypeSymbol typeSymbol) |
| | | 104 | | { |
| | 0 | 105 | | var caps = HttpClientCapabilities.None; |
| | | 106 | | |
| | 0 | 107 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 108 | | { |
| | 0 | 109 | | if (iface.ContainingNamespace?.ToDisplayString() != GeneratorsNamespace) |
| | | 110 | | continue; |
| | | 111 | | |
| | 0 | 112 | | switch (iface.Name) |
| | | 113 | | { |
| | | 114 | | case IHttpClientTimeoutName: |
| | 0 | 115 | | caps |= HttpClientCapabilities.Timeout; |
| | 0 | 116 | | break; |
| | | 117 | | case IHttpClientUserAgentName: |
| | 0 | 118 | | caps |= HttpClientCapabilities.UserAgent; |
| | 0 | 119 | | break; |
| | | 120 | | case IHttpClientBaseAddressName: |
| | 0 | 121 | | caps |= HttpClientCapabilities.BaseAddress; |
| | 0 | 122 | | break; |
| | | 123 | | case IHttpClientDefaultHeadersName: |
| | 0 | 124 | | caps |= HttpClientCapabilities.Headers; |
| | | 125 | | break; |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | return caps; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Returns <c>true</c> if the type implements <c>INamedHttpClientOptions</c>. |
| | | 134 | | /// </summary> |
| | | 135 | | public static bool ImplementsNamedHttpClientOptions(INamedTypeSymbol typeSymbol) |
| | | 136 | | { |
| | 0 | 137 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 138 | | { |
| | 0 | 139 | | if (iface.Name == INamedHttpClientOptionsName && |
| | 0 | 140 | | iface.ContainingNamespace?.ToDisplayString() == GeneratorsNamespace) |
| | | 141 | | { |
| | 0 | 142 | | return true; |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Resolves the HttpClient name from the three allowed sources, in precedence order: |
| | | 151 | | /// (1) attribute <c>Name</c>, (2) <c>ClientName</c> property literal body, |
| | | 152 | | /// (3) inferred from type name with suffix stripping. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="typeSymbol">The options type.</param> |
| | | 155 | | /// <param name="attributeInfo">The extracted attribute info for the type.</param> |
| | | 156 | | /// <param name="propertyNameFromType"> |
| | | 157 | | /// The literal <c>ClientName</c> property value if present and resolvable, or <c>null</c>. |
| | | 158 | | /// </param> |
| | | 159 | | /// <param name="resolvedName">The resolved client name on success.</param> |
| | | 160 | | /// <returns><c>true</c> if a non-empty name could be resolved; otherwise <c>false</c>.</returns> |
| | | 161 | | public static bool TryResolveClientName( |
| | | 162 | | INamedTypeSymbol typeSymbol, |
| | | 163 | | HttpClientOptionsAttributeInfo attributeInfo, |
| | | 164 | | string? propertyNameFromType, |
| | | 165 | | out string resolvedName) |
| | | 166 | | { |
| | 0 | 167 | | if (!string.IsNullOrWhiteSpace(attributeInfo.Name)) |
| | | 168 | | { |
| | 0 | 169 | | resolvedName = attributeInfo.Name!; |
| | 0 | 170 | | return true; |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | if (!string.IsNullOrWhiteSpace(propertyNameFromType)) |
| | | 174 | | { |
| | 0 | 175 | | resolvedName = propertyNameFromType!; |
| | 0 | 176 | | return true; |
| | | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | var inferred = InferClientNameFromTypeName(typeSymbol.Name); |
| | 0 | 180 | | if (!string.IsNullOrWhiteSpace(inferred)) |
| | | 181 | | { |
| | 0 | 182 | | resolvedName = inferred; |
| | 0 | 183 | | return true; |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | resolvedName = string.Empty; |
| | 0 | 187 | | return false; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Strips known suffixes from a type name to infer a client name. |
| | | 192 | | /// Returns the original name if no suffix matches. |
| | | 193 | | /// </summary> |
| | | 194 | | public static string InferClientNameFromTypeName(string typeName) |
| | | 195 | | { |
| | 0 | 196 | | foreach (var suffix in ClientNameSuffixes) |
| | | 197 | | { |
| | 0 | 198 | | if (typeName.EndsWith(suffix, System.StringComparison.Ordinal) && |
| | 0 | 199 | | typeName.Length > suffix.Length) |
| | | 200 | | { |
| | 0 | 201 | | return typeName.Substring(0, typeName.Length - suffix.Length); |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | return typeName; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Attempts to read a <c>ClientName</c> property from the type and extract its literal |
| | | 210 | | /// expression value. Returns a tri-state: |
| | | 211 | | /// <list type="bullet"> |
| | | 212 | | /// <item><description><c>Absent</c> — no <c>ClientName</c> property declared</description></item> |
| | | 213 | | /// <item><description><c>Literal</c> — <c>ClientName</c> exists with a string literal expression body; value is in |
| | | 214 | | /// <item><description><c>NonLiteral</c> — <c>ClientName</c> exists but its body is not a simple literal (e.g., comp |
| | | 215 | | /// </list> |
| | | 216 | | /// </summary> |
| | | 217 | | public static ClientNamePropertyResult TryGetClientNameProperty( |
| | | 218 | | INamedTypeSymbol typeSymbol, |
| | | 219 | | out string? literalValue) |
| | | 220 | | { |
| | 0 | 221 | | literalValue = null; |
| | 0 | 222 | | IPropertySymbol? clientNameProp = null; |
| | | 223 | | |
| | 0 | 224 | | foreach (var member in typeSymbol.GetMembers("ClientName")) |
| | | 225 | | { |
| | 0 | 226 | | if (member is IPropertySymbol p) |
| | | 227 | | { |
| | 0 | 228 | | clientNameProp = p; |
| | 0 | 229 | | break; |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | if (clientNameProp is null) |
| | 0 | 234 | | return ClientNamePropertyResult.Absent; |
| | | 235 | | |
| | | 236 | | // Must be a string-typed, readable, instance property — otherwise treat as non-literal |
| | | 237 | | // and let the analyzer handle it (NDLRHTTP006 fires on shape violations). |
| | 0 | 238 | | if (clientNameProp.Type.SpecialType != SpecialType.System_String || clientNameProp.IsStatic) |
| | 0 | 239 | | return ClientNamePropertyResult.NonLiteral; |
| | | 240 | | |
| | 0 | 241 | | foreach (var declRef in clientNameProp.DeclaringSyntaxReferences) |
| | | 242 | | { |
| | 0 | 243 | | var syntax = declRef.GetSyntax(); |
| | 0 | 244 | | if (syntax is not PropertyDeclarationSyntax propSyntax) |
| | | 245 | | continue; |
| | | 246 | | |
| | | 247 | | // Expression-bodied arrow form: public string ClientName => "WebFetch"; |
| | 0 | 248 | | if (propSyntax.ExpressionBody is ArrowExpressionClauseSyntax arrow && |
| | 0 | 249 | | arrow.Expression is LiteralExpressionSyntax exprLit && |
| | 0 | 250 | | exprLit.IsKind(SyntaxKind.StringLiteralExpression)) |
| | | 251 | | { |
| | 0 | 252 | | literalValue = exprLit.Token.ValueText; |
| | 0 | 253 | | return ClientNamePropertyResult.Literal; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | // Getter-only body form: public string ClientName { get { return "WebFetch"; } } |
| | 0 | 257 | | if (propSyntax.AccessorList is { } accessors) |
| | | 258 | | { |
| | 0 | 259 | | foreach (var accessor in accessors.Accessors) |
| | | 260 | | { |
| | 0 | 261 | | if (!accessor.IsKind(SyntaxKind.GetAccessorDeclaration)) |
| | | 262 | | continue; |
| | | 263 | | |
| | | 264 | | // Expression body on getter |
| | 0 | 265 | | if (accessor.ExpressionBody is ArrowExpressionClauseSyntax getArrow && |
| | 0 | 266 | | getArrow.Expression is LiteralExpressionSyntax getExprLit && |
| | 0 | 267 | | getExprLit.IsKind(SyntaxKind.StringLiteralExpression)) |
| | | 268 | | { |
| | 0 | 269 | | literalValue = getExprLit.Token.ValueText; |
| | 0 | 270 | | return ClientNamePropertyResult.Literal; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | // Single-return block body |
| | 0 | 274 | | if (accessor.Body is { } block && |
| | 0 | 275 | | block.Statements.Count == 1 && |
| | 0 | 276 | | block.Statements[0] is ReturnStatementSyntax ret && |
| | 0 | 277 | | ret.Expression is LiteralExpressionSyntax retLit && |
| | 0 | 278 | | retLit.IsKind(SyntaxKind.StringLiteralExpression)) |
| | | 279 | | { |
| | 0 | 280 | | literalValue = retLit.Token.ValueText; |
| | 0 | 281 | | return ClientNamePropertyResult.Literal; |
| | | 282 | | } |
| | | 283 | | } |
| | | 284 | | } |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | return ClientNamePropertyResult.NonLiteral; |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Computes the configuration section name from the attribute (if explicit) or by inference |
| | | 292 | | /// from the resolved client name. |
| | | 293 | | /// </summary> |
| | | 294 | | public static string ResolveSectionName(HttpClientOptionsAttributeInfo attributeInfo, string clientName) |
| | | 295 | | { |
| | 0 | 296 | | if (!string.IsNullOrWhiteSpace(attributeInfo.SectionName)) |
| | 0 | 297 | | return attributeInfo.SectionName!; |
| | | 298 | | |
| | 0 | 299 | | return $"HttpClients:{clientName}"; |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | /// <summary> |
| | | 303 | | /// Returns the symbol for the <c>ClientName</c> property if present, for analyzer shape checks. |
| | | 304 | | /// </summary> |
| | | 305 | | public static IPropertySymbol? GetClientNamePropertySymbol(INamedTypeSymbol typeSymbol) |
| | | 306 | | { |
| | 0 | 307 | | foreach (var member in typeSymbol.GetMembers("ClientName")) |
| | | 308 | | { |
| | 0 | 309 | | if (member is IPropertySymbol p) |
| | 0 | 310 | | return p; |
| | | 311 | | } |
| | 0 | 312 | | return null; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | private static bool IsHttpClientOptionsAttribute(INamedTypeSymbol? attributeClass) |
| | | 316 | | { |
| | 2044129 | 317 | | if (attributeClass is null) |
| | 0 | 318 | | return false; |
| | | 319 | | |
| | 2044129 | 320 | | return attributeClass.Name == HttpClientOptionsAttributeName && |
| | 2044129 | 321 | | attributeClass.ContainingNamespace?.ToDisplayString() == GeneratorsNamespace; |
| | | 322 | | } |
| | | 323 | | } |
| | | 324 | | |
| | | 325 | | /// <summary> |
| | | 326 | | /// Tri-state result from probing a type for a <c>ClientName</c> property. |
| | | 327 | | /// </summary> |
| | | 328 | | internal enum ClientNamePropertyResult |
| | | 329 | | { |
| | | 330 | | Absent, |
| | | 331 | | Literal, |
| | | 332 | | NonLiteral, |
| | | 333 | | } |