public string GenerateMethodClass(MethodInfo method, out string fullMethodClassName) { var typeInfo = method.DeclaringType; Debug.Assert(typeInfo != null, nameof(typeInfo) + " is null"); var className = $"{typeInfo.FullName?.Replace(".", string.Empty)}_{method.Name}"; fullMethodClassName = $"{Config.RootNamespace}.{className}"; var sealedString = Config.GenerateSealedClasses ? "sealed" : string.Empty; var isAsyncCall = typeof(Task).IsAssignableFrom(method.ReturnType); var returnType = method.ReturnType; var serviceName = typeInfo.GetCustomAttribute <FauxClientAttribute>().Name; if (isAsyncCall && method.ReturnType.IsConstructedGenericType) { returnType = method.ReturnType.GetGenericArguments()[0]; } var isVoid = returnType == typeof(void) || (isAsyncCall && !method.ReturnType.IsConstructedGenericType); var treturn = isVoid ? string.Empty : $"<{CompilerUtils.ToCompilableName(returnType)}>"; var inParams = method.GetParameters().Where(p => !p.IsOut).ToList(); var outParams = method.GetParameters().Where(p => p.IsOut).ToList(); var attribute = method.GetCustomAttribute <HttpMethodAttribute>(); using (logger.BeginScope("Generator {0}.{1}:", className, method.Name)) using (var namespaceBuilder = new IndentBuilder()) { namespaceBuilder.AppendLine($"namespace {Config.RootNamespace}"); namespaceBuilder.AppendLine("{"); using (var classBuilder = namespaceBuilder.Indent()) { classBuilder.AppendLine("// Generated by DHaven.Faux"); classBuilder.AppendLine($"public {sealedString} class {className} : Steeltoe.CircuitBreaker.Hystrix.HystrixCommand{treturn}"); classBuilder.AppendLine("{"); using (var fieldBuilder = classBuilder.Indent()) { fieldBuilder.AppendLine("private readonly DHaven.Faux.HttpSupport.DiscoveryAwareBase 仮core;"); fieldBuilder.AppendLine("private readonly Microsoft.Extensions.Logging.ILogger 仮logger;"); fieldBuilder.AppendLine($"private readonly {typeInfo.FullName} 仮fallback;"); foreach (var inField in inParams) { fieldBuilder.AppendLine($"private readonly {CompilerUtils.ToCompilableName(inField.ParameterType)} {inField.Name};"); } } using (var constructorBuilder = classBuilder.Indent()) { constructorBuilder.AppendLine($"public {className}("); constructorBuilder.AppendLine(" DHaven.Faux.HttpSupport.DiscoveryAwareBase core,"); constructorBuilder.AppendLine($" {typeInfo.FullName} fallback,"); foreach (var inparam in inParams) { constructorBuilder.AppendLine( $" {CompilerUtils.ToCompilableName(inparam.ParameterType)} {inparam.Name},"); } constructorBuilder.AppendLine(" Microsoft.Extensions.Logging.ILogger logger)"); constructorBuilder.AppendLine($" : base(new Steeltoe.CircuitBreaker.Hystrix.HystrixCommandOptions(Steeltoe.CircuitBreaker.Hystrix.HystrixCommandGroupKeyDefault.AsKey(\"{serviceName}\"), Steeltoe.CircuitBreaker.Hystrix.HystrixCommandKeyDefault.AsKey(\"{method.Name}\")), null, null, logger)"); constructorBuilder.AppendLine("{"); using (var insideBuilder = constructorBuilder.Indent()) { insideBuilder.AppendLine("仮core = core;"); insideBuilder.AppendLine("仮logger = logger;"); insideBuilder.AppendLine("仮fallback = fallback;"); foreach (var inparam in inParams) { insideBuilder.AppendLine($"this.{inparam.Name} = {inparam.Name};"); } } constructorBuilder.AppendLine("}"); } using (var propertyBuilder = classBuilder.Indent()) { foreach (var property in outParams) { propertyBuilder.AppendLine($"public {CompilerUtils.ToCompilableName(property.ParameterType)} {property.Name} {{ get; private set; }}"); } } var taskType = isVoid ? "System.Reactive.Unit" : CompilerUtils.ToCompilableName(returnType); using (var runBuilder = classBuilder.Indent()) { runBuilder.AppendLine($"protected override async System.Threading.Tasks.Task<{taskType}> RunAsync()"); runBuilder.AppendLine("{"); using (var contentBuilder = runBuilder.Indent()) { contentBuilder.AppendLine("var 仮variables = new System.Collections.Generic.Dictionary<string,object>();"); contentBuilder.AppendLine("var 仮reqParams = new System.Collections.Generic.Dictionary<string,string>();"); var contentHeaders = new Dictionary <string, ParameterInfo>(); var requestHeaders = new Dictionary <string, ParameterInfo>(); var responseHeaders = new Dictionary <string, ParameterInfo>(); ParameterInfo bodyParam = null; BodyAttribute bodyAttr = null; foreach (var parameter in method.GetParameters()) { AttributeInterpreter.InterpretPathValue(parameter, contentBuilder); AttributeInterpreter.InterpretRequestHeader(parameter, requestHeaders, contentHeaders); AttributeInterpreter.InterpretBodyParameter(parameter, ref bodyParam, ref bodyAttr); AttributeInterpreter.InterpretRequestParameter(parameter, contentBuilder); AttributeInterpreter.InterpretResponseHeaderInParameters(parameter, false, ref responseHeaders); } contentBuilder.AppendLine($"var 仮request = 仮core.CreateRequest({CompilerUtils.ToCompilableName(attribute.Method)}, \"{attribute.Path}\", 仮variables, 仮reqParams);"); var hasContent = AttributeInterpreter.CreateContentObjectIfSpecified(bodyAttr, bodyParam, contentBuilder); foreach (var entry in requestHeaders) { contentBuilder.AppendLine($"仮request.Headers.Add(\"{entry.Key}\", {entry.Value.Name}{(entry.Value.ParameterType.IsClass ? "?" : "")}.ToString());"); } if (hasContent) { // when setting content we can apply the contentHeaders foreach (var entry in contentHeaders) { contentBuilder.AppendLine($"仮content.Headers.Add(\"{entry.Key}\", {entry.Value.Name}{(entry.Value.ParameterType.IsClass ? "?" : "")}.ToString());"); } contentBuilder.AppendLine("仮request.Content = 仮content;"); } contentBuilder.AppendLine("var 仮response = await 仮core.InvokeAsync(仮request);"); foreach (var entry in responseHeaders) { contentBuilder.AppendLine( $"{entry.Value.Name} = DHaven.Faux.HttpSupport.DiscoveryAwareBase.GetHeaderValue<{CompilerUtils.ToCompilableName(entry.Value.ParameterType)}>(仮response, \"{entry.Key}\");"); } if (isVoid) { contentBuilder.AppendLine("return System.Reactive.Unit.Default;"); } else { var returnBodyAttribute = method.ReturnParameter?.GetCustomAttribute <BodyAttribute>(); var returnResponseAttribute = method.ReturnParameter?.GetCustomAttribute <ResponseHeaderAttribute>(); if (returnResponseAttribute != null && returnBodyAttribute != null) { throw new WebServiceCompileException( $"Cannot have different types of response attributes. You had [{string.Join(", ", "Body", "ResponseHeader")}]"); } if (returnResponseAttribute != null) { AttributeInterpreter.ReturnResponseHeader(returnResponseAttribute, returnType, contentBuilder); } else { if (returnBodyAttribute == null) { returnBodyAttribute = new BodyAttribute(); } AttributeInterpreter.ReturnContentObject(returnBodyAttribute, returnType, isAsyncCall, contentBuilder); } } } runBuilder.AppendLine("}"); } using (var fallbackBuilder = classBuilder.Indent()) { fallbackBuilder.AppendLine($"protected override async System.Threading.Tasks.Task<{taskType}> RunFallbackAsync()"); fallbackBuilder.AppendLine("{"); using (var contentBuilder = fallbackBuilder.Indent()) { foreach (var value in outParams) { contentBuilder.AppendLine($"{CompilerUtils.ToCompilableName(value.ParameterType)} {value.Name} = default({CompilerUtils.ToCompilableName(value.ParameterType)});"); } if (!isVoid) { contentBuilder.Append("var 仮value = "); } if (isAsyncCall) { contentBuilder.Append("await "); } contentBuilder.Append($"仮fallback?.{method.Name}("); contentBuilder.Append(string.Join(", ", method.GetParameters().Select(CompilerUtils.ToParameterUsage))); contentBuilder.Append(")"); if (!isVoid && method.ReturnType.IsValueType) { contentBuilder.Append($" ?? default({CompilerUtils.ToCompilableName(method.ReturnType)})"); } contentBuilder.AppendLine(";"); foreach (var value in outParams) { contentBuilder.AppendLine($"this.{value.Name} = {value.Name};"); } var returnVal = isVoid ? "System.Reactive.Unit.Default" : "仮value"; contentBuilder.AppendLine($"return {returnVal};"); } fallbackBuilder.AppendLine("}"); } classBuilder.AppendLine("}"); } namespaceBuilder.AppendLine("}"); var sourceCode = namespaceBuilder.ToString(); logger.LogTrace("Source generated"); if (Config.OutputSourceFiles) { var fullPath = Path.Combine(Config.SourceFilePath, $"{className}.cs"); try { logger.LogTrace("Writing source file: {0}", fullPath); File.WriteAllText(fullPath, sourceCode, Encoding.UTF8); } catch (Exception ex) { logger.LogWarning(ex, "Could not write the source code for {0}", fullPath); } } return(sourceCode); } }
private void BuildMethod(IndentBuilder classBuilder, MethodInfo method) { var isAsyncCall = typeof(Task).IsAssignableFrom(method.ReturnType); var returnType = method.ReturnType; if (isAsyncCall && method.ReturnType.IsConstructedGenericType) { returnType = method.ReturnType.GetGenericArguments()[0]; } var isVoid = returnType == typeof(void) || (isAsyncCall && !method.ReturnType.IsConstructedGenericType); // Write the method declaration classBuilder.Append("public "); if (isAsyncCall) { classBuilder.Append("async "); classBuilder.Append(typeof(Task).FullName); if (!isVoid) { classBuilder.Append($"<{CompilerUtils.ToCompilableName(returnType)}>"); } } else { classBuilder.Append(isVoid ? "void" : CompilerUtils.ToCompilableName(returnType)); } var attribute = method.GetCustomAttribute <HttpMethodAttribute>(); classBuilder.Append($" {method.Name}("); classBuilder.Append(string.Join(", ", method.GetParameters().Select(CompilerUtils.ToParameterDeclaration))); classBuilder.AppendLine(")"); classBuilder.AppendLine("{"); using (var methodBuilder = classBuilder.Indent()) { methodBuilder.AppendLine( "var 仮variables = new System.Collections.Generic.Dictionary<string,object>();"); methodBuilder.AppendLine( "var 仮reqParams = new System.Collections.Generic.Dictionary<string,string>();"); var contentHeaders = new Dictionary <string, ParameterInfo>(); var requestHeaders = new Dictionary <string, ParameterInfo>(); var responseHeaders = new Dictionary <string, ParameterInfo>(); ParameterInfo bodyParam = null; BodyAttribute bodyAttr = null; foreach (var parameter in method.GetParameters()) { AttributeInterpreter.InterpretPathValue(parameter, methodBuilder); AttributeInterpreter.InterpretRequestHeader(parameter, requestHeaders, contentHeaders); AttributeInterpreter.InterpretBodyParameter(parameter, ref bodyParam, ref bodyAttr); AttributeInterpreter.InterpretRequestParameter(parameter, methodBuilder); AttributeInterpreter.InterpretResponseHeaderInParameters(parameter, isAsyncCall, ref responseHeaders); } methodBuilder.AppendLine( $"var 仮request = CreateRequest({CompilerUtils.ToCompilableName(attribute.Method)}, \"{attribute.Path}\", 仮variables, 仮reqParams);"); var hasContent = AttributeInterpreter.CreateContentObjectIfSpecified(bodyAttr, bodyParam, methodBuilder); foreach (var entry in requestHeaders) { methodBuilder.AppendLine( $"仮request.Headers.Add(\"{entry.Key}\", {entry.Value.Name}{(entry.Value.ParameterType.IsClass ? "?" : "")}.ToString());"); } if (hasContent) { // when setting content we can apply the contentHeaders foreach (var entry in contentHeaders) { methodBuilder.AppendLine( $"仮content.Headers.Add(\"{entry.Key}\", {entry.Value.Name}{(entry.Value.ParameterType.IsClass ? "?" : "")}.ToString());"); } methodBuilder.AppendLine("仮request.Content = 仮content;"); } methodBuilder.AppendLine(isAsyncCall ? "var 仮response = await InvokeAsync(仮request);" : "var 仮response = Invoke(仮request);"); foreach (var entry in responseHeaders) { methodBuilder.AppendLine( $"{entry.Value.Name} = GetHeaderValue<{CompilerUtils.ToCompilableName(entry.Value.ParameterType)}>(仮response, \"{entry.Key}\");"); } if (!isVoid) { var returnBodyAttribute = method.ReturnParameter?.GetCustomAttribute <BodyAttribute>(); var returnResponseAttribute = method.ReturnParameter?.GetCustomAttribute <ResponseHeaderAttribute>(); if (returnResponseAttribute != null && returnBodyAttribute != null) { throw new WebServiceCompileException( $"Cannot have different types of response attributes. You had [{string.Join(", ", "Body", "ResponseHeader")}]"); } if (returnResponseAttribute != null) { AttributeInterpreter.ReturnResponseHeader(returnResponseAttribute, returnType, methodBuilder); } else { if (returnBodyAttribute == null) { returnBodyAttribute = new BodyAttribute(); } AttributeInterpreter.ReturnContentObject(returnBodyAttribute, returnType, isAsyncCall, methodBuilder); } } } classBuilder.AppendLine("}"); }