| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using Microsoft.CodeAnalysis; |
| | | 8 | | using Microsoft.CodeAnalysis.Text; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.BuildTools; |
| | | 11 | | |
| | | 12 | | public sealed partial class OperationParameterInjectionGenerator |
| | | 13 | | { |
| | | 14 | | private record struct MessageProperty( |
| | 4 | 15 | | string PropertyName, |
| | 4 | 16 | | string PropertyTypeFullName, |
| | 4 | 17 | | string OutputVarName); |
| | | 18 | | |
| | 16 | 19 | | private record struct KeyedService(TypedConstant ServiceKey); |
| | | 20 | | |
| | | 21 | | private sealed class Emitter |
| | | 22 | | { |
| | | 23 | | private readonly StringBuilder _builder; |
| | | 24 | | private readonly OperationParameterInjectionSourceGenerationContext _sourceGenerationContext; |
| | | 25 | | private readonly SourceGenerationSpec _generationSpec; |
| | | 26 | | |
| | 352 | 27 | | public Emitter(in OperationParameterInjectionSourceGenerationContext sourceGenerationContext, in SourceGeneratio |
| | | 28 | | { |
| | 352 | 29 | | _sourceGenerationContext = sourceGenerationContext; |
| | 352 | 30 | | _generationSpec = generationSpec; |
| | 352 | 31 | | _builder = new StringBuilder(); |
| | 352 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void Emit() |
| | | 35 | | { |
| | 352 | 36 | | _builder.Clear(); |
| | 352 | 37 | | _builder.AppendLine($@"// <auto-generated> |
| | 352 | 38 | | // Generated by the CoreWCF.BuildTools.OperationParameterInjectionGenerator source generator. DO NOT EDIT! |
| | 352 | 39 | | // </auto-generated> |
| | 352 | 40 | | #nullable disable |
| | 352 | 41 | | using System; |
| | 352 | 42 | | using Microsoft.Extensions.DependencyInjection;"); |
| | | 43 | | |
| | 1440 | 44 | | foreach (var operationContractSpec in _generationSpec.OperationContractSpecs) |
| | | 45 | | { |
| | 368 | 46 | | EmitOperationContract(operationContractSpec); |
| | | 47 | | } |
| | | 48 | | |
| | 352 | 49 | | if (_generationSpec.OperationContractSpecs.Length > 0) |
| | | 50 | | { |
| | 352 | 51 | | _builder.AppendLine("#nullable restore"); |
| | 352 | 52 | | _sourceGenerationContext.AddSource("OperationParameterInjection.g.cs", SourceText.From(_builder.ToString |
| | | 53 | | } |
| | 352 | 54 | | } |
| | | 55 | | |
| | | 56 | | private void EmitOperationContract(OperationContractSpec operationContractSpec) |
| | | 57 | | { |
| | 368 | 58 | | Dictionary<IParameterSymbol, string> messagePropertyNames = new(SymbolEqualityComparer.Default); |
| | 368 | 59 | | Dictionary<IParameterSymbol, KeyedService> keyedServices = new(SymbolEqualityComparer.Default); |
| | 368 | 60 | | List<MessageProperty> messageProperties = new(); |
| | | 61 | | |
| | 2360 | 62 | | foreach (var parameter in operationContractSpec.UserProvidedOperationContractImplementation!.Parameters) |
| | | 63 | | { |
| | 1672 | 64 | | if (operationContractSpec.MissingOperationContract.Parameters.Any(p => p.IsMatchingParameter(parameter)) |
| | | 65 | | { |
| | | 66 | | continue; |
| | | 67 | | } |
| | | 68 | | |
| | 1728 | 69 | | foreach (AttributeData attribute in parameter.GetAttributes()) |
| | | 70 | | { |
| | 436 | 71 | | if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, |
| | 436 | 72 | | _generationSpec.CoreWCFInjectedSymbol)) |
| | | 73 | | { |
| | 240 | 74 | | if (attribute.NamedArguments.Length > 1) |
| | | 75 | | { |
| | 8 | 76 | | _sourceGenerationContext.ReportDiagnostic(DiagnosticDescriptors.OperationParameterInjectionG |
| | 8 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | |
| | 488 | 80 | | foreach (var namedArgument in attribute.NamedArguments) |
| | | 81 | | { |
| | 16 | 82 | | if (namedArgument.Key == "PropertyName") |
| | | 83 | | { |
| | 12 | 84 | | if (namedArgument.Value.IsNull) |
| | | 85 | | { |
| | 4 | 86 | | _sourceGenerationContext.ReportDiagnostic( |
| | 4 | 87 | | DiagnosticDescriptors.OperationParameterInjectionGenerator_01XX |
| | 4 | 88 | | .RaisePropertyNameCannotBeNullOrEmptyError(parameter.Locations[0])); |
| | 4 | 89 | | return; |
| | | 90 | | } |
| | 8 | 91 | | var propertyName = namedArgument.Value.Value!.ToString(); |
| | 8 | 92 | | if (propertyName is "") |
| | | 93 | | { |
| | 4 | 94 | | _sourceGenerationContext.ReportDiagnostic( |
| | 4 | 95 | | DiagnosticDescriptors.OperationParameterInjectionGenerator_01XX |
| | 4 | 96 | | .RaisePropertyNameCannotBeNullOrEmptyError(parameter.Locations[0])); |
| | 4 | 97 | | return; |
| | | 98 | | } |
| | 4 | 99 | | var messagePropertyVariableName = propertyName.ToMessagePropertyVariableName(); |
| | 4 | 100 | | messagePropertyNames[parameter] = messagePropertyVariableName; |
| | 4 | 101 | | messageProperties.Add(new MessageProperty(propertyName, parameter.Type.ToDisplayString() |
| | 4 | 102 | | messagePropertyVariableName)); |
| | | 103 | | } |
| | 4 | 104 | | else if (namedArgument.Key == "ServiceKey") |
| | | 105 | | { |
| | 4 | 106 | | keyedServices[parameter] = new KeyedService(namedArgument.Value); |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | } |
| | 196 | 110 | | else if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, |
| | 196 | 111 | | _generationSpec.MicrosoftExtensionsDependencyInjectionFromKeyedServicesSymbol)) |
| | | 112 | | { |
| | 4 | 113 | | keyedServices[parameter] = new KeyedService(attribute.ConstructorArguments.First()); |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | 352 | 118 | | Dictionary<ITypeSymbol, string> dependencyNames = new(SymbolEqualityComparer.Default); |
| | 1132 | 119 | | var dependencies = operationContractSpec.UserProvidedOperationContractImplementation!.Parameters.Where(x => |
| | 1944 | 120 | | p.IsMatchingParameter(x))).ToList(); |
| | | 121 | | |
| | 352 | 122 | | bool shouldGenerateAsyncAwait = SymbolEqualityComparer.Default.Equals(operationContractSpec.MissingOperation |
| | 352 | 123 | | || (operationContractSpec.MissingOperationContract.ReturnType is INamedTypeS |
| | 352 | 124 | | SymbolEqualityComparer.Default.Equals(symbol.ConstructedFrom, _generatio |
| | | 125 | | |
| | 352 | 126 | | string @async = shouldGenerateAsyncAwait |
| | 352 | 127 | | ? "async " |
| | 352 | 128 | | : string.Empty; |
| | | 129 | | |
| | 352 | 130 | | string @await = shouldGenerateAsyncAwait |
| | 352 | 131 | | ? "await " |
| | 352 | 132 | | : string.Empty; |
| | | 133 | | |
| | 352 | 134 | | string @return = (operationContractSpec.MissingOperationContract.ReturnsVoid || SymbolEqualityComparer.Defau |
| | 352 | 135 | | string.Empty |
| | 352 | 136 | | : "return "; |
| | | 137 | | |
| | 456 | 138 | | string GetAccessibilityModifier(Accessibility accessibility) => accessibility switch |
| | 456 | 139 | | { |
| | 32 | 140 | | Accessibility.Private => "private ", |
| | 16 | 141 | | Accessibility.Protected => "protected ", |
| | 344 | 142 | | Accessibility.Public => "public ", |
| | 64 | 143 | | _ => "internal " |
| | 456 | 144 | | }; |
| | | 145 | | |
| | 352 | 146 | | bool isServiceContractImplInGlobalNamespace = operationContractSpec.ServiceContractImplementation!.Containin |
| | 352 | 147 | | .IsGlobalNamespace; |
| | | 148 | | |
| | 352 | 149 | | string returnType = operationContractSpec.MissingOperationContract.ReturnsVoid |
| | 352 | 150 | | ? "void" |
| | 352 | 151 | | : $"{operationContractSpec.MissingOperationContract.ReturnType}"; |
| | | 152 | | |
| | 352 | 153 | | string parameters = string.Join(", ", operationContractSpec.MissingOperationContract.Parameters |
| | 720 | 154 | | .Select(static p => p.RefKind switch |
| | 720 | 155 | | { |
| | 8 | 156 | | RefKind.Ref => $"ref {p.Type} {p.Name}", |
| | 8 | 157 | | RefKind.Out => $"out {p.Type} {p.Name}", |
| | 352 | 158 | | _ => $"{p.Type} {p.Name}", |
| | 720 | 159 | | })); |
| | | 160 | | |
| | 352 | 161 | | var indentor = new Indentor(); |
| | | 162 | | |
| | 352 | 163 | | if (!isServiceContractImplInGlobalNamespace) |
| | | 164 | | { |
| | 336 | 165 | | _builder.AppendLine($@"namespace {operationContractSpec.ServiceContractImplementation!.ContainingNamespa |
| | 336 | 166 | | {{"); |
| | 336 | 167 | | indentor.Increment(); |
| | | 168 | | } |
| | | 169 | | |
| | 352 | 170 | | Stack<INamedTypeSymbol> classes = new(); |
| | 352 | 171 | | INamedTypeSymbol containingType = operationContractSpec.ServiceContractImplementation; |
| | 808 | 172 | | while (containingType != null) |
| | | 173 | | { |
| | 456 | 174 | | classes.Push(containingType); |
| | 456 | 175 | | containingType = containingType.ContainingType; |
| | | 176 | | } |
| | | 177 | | |
| | 808 | 178 | | while (classes.Count > 0) |
| | | 179 | | { |
| | 456 | 180 | | containingType = classes.Pop(); |
| | 456 | 181 | | _builder.AppendLine($@"{indentor}{GetAccessibilityModifier(containingType.DeclaredAccessibility)}partial |
| | 456 | 182 | | _builder.AppendLine($@"{indentor}{{"); |
| | 456 | 183 | | indentor.Increment(); |
| | | 184 | | } |
| | | 185 | | |
| | 784 | 186 | | foreach (AttributeData attributeData in operationContractSpec.UserProvidedOperationContractImplementation.Ge |
| | | 187 | | { |
| | 40 | 188 | | _builder.Append($"{indentor}[{attributeData.AttributeClass}("); |
| | 104 | 189 | | _builder.Append(string.Join(", ", attributeData.ConstructorArguments.Select(x => x.ToSafeCSharpString()) |
| | 40 | 190 | | _builder.Append(")]"); |
| | 40 | 191 | | _builder.AppendLine(); |
| | | 192 | | } |
| | | 193 | | |
| | 352 | 194 | | _builder.AppendLine($@"{indentor}public {@async}{returnType} {operationContractSpec.MissingOperationContract |
| | 352 | 195 | | _builder.AppendLine($@"{indentor}{{"); |
| | 352 | 196 | | indentor.Increment(); |
| | 352 | 197 | | _builder.AppendLine($@"{indentor}var serviceProvider = CoreWCF.OperationContext.Current.InstanceContext.Exte |
| | 352 | 198 | | _builder.AppendLine($@"{indentor}if (serviceProvider == null) throw new InvalidOperationException(""Missing |
| | | 199 | | |
| | 352 | 200 | | int objectIndex = 0; |
| | 748 | 201 | | if (dependencies.Any(x => SymbolEqualityComparer.Default.Equals(x.Type, operationContractSpec.HttpContextSym |
| | 748 | 202 | | || SymbolEqualityComparer.Default.Equals(x.Type, operationContractSpec.HttpRequest |
| | 748 | 203 | | || SymbolEqualityComparer.Default.Equals(x.Type, operationContractSpec.HttpRespons |
| | | 204 | | { |
| | 32 | 205 | | _builder.AppendLine($@"{indentor}var httpContext = (CoreWCF.OperationContext.Current.RequestContext.Requ |
| | 32 | 206 | | indentor.Increment(); |
| | 32 | 207 | | _builder.AppendLine($@"{indentor}&& o{objectIndex} is Microsoft.AspNetCore.Http.HttpContext p{objectInde |
| | 32 | 208 | | _builder.AppendLine($@"{indentor}? p{objectIndex}"); |
| | 32 | 209 | | _builder.AppendLine($@"{indentor}: null;"); |
| | 32 | 210 | | indentor.Decrement(); |
| | 32 | 211 | | _builder.AppendLine($@"{indentor}if (httpContext == null) throw new InvalidOperationException(""Missing |
| | 32 | 212 | | objectIndex++; |
| | | 213 | | } |
| | | 214 | | |
| | 712 | 215 | | foreach ((string propertyName, string propertyTypeFullName, string outputVar) in messageProperties) |
| | | 216 | | { |
| | 4 | 217 | | _builder.AppendLine($@"{indentor}var {outputVar} = (CoreWCF.OperationContext.Current.IncomingMessageProp |
| | 4 | 218 | | indentor.Increment(); |
| | 4 | 219 | | _builder.AppendLine($@"{indentor}&& o{objectIndex} is {propertyTypeFullName} p{objectIndex})"); |
| | 4 | 220 | | _builder.AppendLine($@"{indentor}? p{objectIndex}"); |
| | 4 | 221 | | _builder.AppendLine($@"{indentor}: null;"); |
| | 4 | 222 | | indentor.Decrement(); |
| | 4 | 223 | | objectIndex++; |
| | | 224 | | } |
| | | 225 | | |
| | 352 | 226 | | _builder.AppendLine($@"{indentor}if (CoreWCF.OperationContext.Current.InstanceContext.IsSingleton)"); |
| | 352 | 227 | | _builder.AppendLine($@"{indentor}{{"); |
| | 352 | 228 | | indentor.Increment(); |
| | 352 | 229 | | _builder.AppendLine($@"{indentor}using (var scope = serviceProvider.CreateScope())"); |
| | 352 | 230 | | _builder.AppendLine($@"{indentor}{{"); |
| | 352 | 231 | | indentor.Increment(); |
| | | 232 | | |
| | 352 | 233 | | string dependencyNamePrefix = "d"; |
| | 352 | 234 | | string serviceProviderName = "scope.ServiceProvider"; |
| | | 235 | | |
| | 352 | 236 | | AppendResolveDependencies(); |
| | 352 | 237 | | AppendInvokeUserProvidedImplementation(); |
| | | 238 | | |
| | 352 | 239 | | if (operationContractSpec.MissingOperationContract.ReturnsVoid || SymbolEqualityComparer.Default.Equals(oper |
| | | 240 | | { |
| | 16 | 241 | | _builder.AppendLine($@"{indentor}return;"); |
| | | 242 | | } |
| | | 243 | | |
| | 352 | 244 | | indentor.Decrement(); |
| | 352 | 245 | | _builder.AppendLine($@"{indentor}}}"); |
| | 352 | 246 | | indentor.Decrement(); |
| | 352 | 247 | | _builder.AppendLine($@"{indentor}}}"); |
| | | 248 | | |
| | 352 | 249 | | dependencyNamePrefix = "e"; |
| | 352 | 250 | | serviceProviderName = "serviceProvider"; |
| | | 251 | | |
| | 352 | 252 | | AppendResolveDependencies(); |
| | 352 | 253 | | AppendInvokeUserProvidedImplementation(); |
| | | 254 | | |
| | 1496 | 255 | | while (indentor.Level > 0) |
| | | 256 | | { |
| | 1144 | 257 | | indentor.Decrement(); |
| | 1144 | 258 | | _builder.AppendLine($@"{indentor}}}"); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | void AppendResolveDependencies() |
| | | 262 | | { |
| | | 263 | | for (int i = 0; i < dependencies.Count; i++) |
| | | 264 | | { |
| | | 265 | | dependencyNames[dependencies[i].Type] = $"{dependencyNamePrefix}{i}"; |
| | | 266 | | if (SymbolEqualityComparer.Default.Equals(operationContractSpec.HttpContextSymbol, dependencies[i].T |
| | | 267 | | { |
| | | 268 | | _builder.AppendLine($@"{indentor}var {dependencyNamePrefix}{i} = httpContext;"); |
| | | 269 | | } |
| | | 270 | | else if (SymbolEqualityComparer.Default.Equals(operationContractSpec.HttpRequestSymbol, dependencies |
| | | 271 | | { |
| | | 272 | | _builder.AppendLine($@"{indentor}var {dependencyNamePrefix}{i} = httpContext.Request;"); |
| | | 273 | | } |
| | | 274 | | else if (SymbolEqualityComparer.Default.Equals(operationContractSpec.HttpResponseSymbol, dependencie |
| | | 275 | | { |
| | | 276 | | _builder.AppendLine($@"{indentor}var {dependencyNamePrefix}{i} = httpContext.Response;"); |
| | | 277 | | } |
| | | 278 | | else if (messagePropertyNames.TryGetValue(dependencies[i], out string messagePropertyVariableName)) |
| | | 279 | | { |
| | | 280 | | _builder.AppendLine($@"{indentor}var {dependencyNamePrefix}{i} = {messagePropertyVariableName};" |
| | | 281 | | } |
| | | 282 | | else if (keyedServices.TryGetValue(dependencies[i], out var keyedService)) |
| | | 283 | | { |
| | | 284 | | _builder.AppendLine($@"{indentor}var {dependencyNamePrefix}{i} = {serviceProviderName}.GetKeyedS |
| | | 285 | | } |
| | | 286 | | else |
| | | 287 | | { |
| | | 288 | | _builder.AppendLine($@"{indentor}var {dependencyNamePrefix}{i} = {serviceProviderName}.GetServic |
| | | 289 | | } |
| | | 290 | | } |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | void AppendInvokeUserProvidedImplementation() |
| | | 294 | | { |
| | | 295 | | _builder.Append($"{indentor}{@return}{@await}{operationContractSpec.UserProvidedOperationContractImpleme |
| | | 296 | | for (int i = 0,j = 0; i < operationContractSpec.UserProvidedOperationContractImplementation.Parameters.L |
| | | 297 | | { |
| | | 298 | | IParameterSymbol parameter = operationContractSpec.UserProvidedOperationContractImplementation.Param |
| | | 299 | | if (i != 0) |
| | | 300 | | { |
| | | 301 | | _builder.Append(", "); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | if (parameter.GetOneAttributeOf(_generationSpec.CoreWCFInjectedSymbol, |
| | | 305 | | _generationSpec.MicrosoftAspNetCoreMvcFromServicesSymbol, |
| | | 306 | | _generationSpec.MicrosoftExtensionsDependencyInjectionFromKeyedServicesSymbol) is not null) |
| | | 307 | | { |
| | | 308 | | _builder.Append(dependencyNames[parameter.Type]); |
| | | 309 | | } |
| | | 310 | | else |
| | | 311 | | { |
| | | 312 | | IParameterSymbol originalParameter = operationContractSpec.MissingOperationContract.Parameters[j |
| | | 313 | | _builder.Append(originalParameter.RefKind switch |
| | | 314 | | { |
| | | 315 | | RefKind.Ref => $"ref {originalParameter.Name}", |
| | | 316 | | RefKind.Out => $"out {originalParameter.Name}", |
| | | 317 | | _ => originalParameter.Name, |
| | | 318 | | }); |
| | | 319 | | j++; |
| | | 320 | | } |
| | | 321 | | } |
| | | 322 | | _builder.AppendLine(");"); |
| | | 323 | | } |
| | 352 | 324 | | } |
| | | 325 | | } |
| | | 326 | | } |