internal static void InterpretPathValue(ParameterInfo parameter, IndentBuilder contentBuilder)
        {
            var pathValue = parameter.GetCustomAttribute <PathValueAttribute>();

            if (pathValue == null)
            {
                return;
            }

            var key = string.IsNullOrEmpty(pathValue.Variable) ? parameter.Name : pathValue.Variable;

            contentBuilder.AppendLine($"仮variables.Add(\"{key}\", {parameter.Name});");
        }
        internal static void InterpretRequestParameter(ParameterInfo parameter, IndentBuilder contentBuilder)
        {
            var paramAttribute = parameter.GetCustomAttribute <RequestParameterAttribute>();

            if (paramAttribute == null)
            {
                return;
            }

            var paramName = string.IsNullOrEmpty(paramAttribute.Parameter)
                ? parameter.Name
                : paramAttribute.Parameter;

            contentBuilder.AppendLine($"仮reqParams.Add(\"{paramName}\", {parameter.Name}{(parameter.ParameterType.IsClass ? "?" : "")}.ToString());");
        }
        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);
                }
        }
示例#4
0
 private IndentBuilder(IndentBuilder parent)
 {
     builder = parent.builder;
     level   = parent.level + 1;
 }
        public IEnumerable <string> GenerateSource(TypeInfo typeInfo, out string fullClassName)
        {
            if (!typeInfo.IsInterface || !typeInfo.IsPublic)
            {
                throw new ArgumentException($"{typeInfo.FullName} must be a public interface");
            }

            if (typeInfo.IsGenericType)
            {
                throw new NotSupportedException($"Generic interfaces are not supported: {typeInfo.FullName}");
            }

            var className = typeInfo.FullName?.Replace(".", string.Empty);

            fullClassName = $"{Config.RootNamespace}.{className}";

            using (logger.BeginScope("Generator {0}:", className))
            {
                var serviceName  = typeInfo.GetCustomAttribute <FauxClientAttribute>().Name;
                var baseRoute    = typeInfo.GetCustomAttribute <FauxClientAttribute>().Route ?? string.Empty;
                var sealedString = Config.GenerateSealedClasses ? "sealed" : string.Empty;

                logger.LogTrace("Beginning to generate source");

                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} : DHaven.Faux.HttpSupport.DiscoveryAwareBase, {typeInfo.FullName}");
                        classBuilder.AppendLine("{");

                        using (var fieldBuilder = classBuilder.Indent())
                        {
                            fieldBuilder.AppendLine("private readonly Microsoft.Extensions.Logging.ILogger 仮logger;");
                        }

                        using (var constructorBuilder = classBuilder.Indent())
                        {
                            constructorBuilder.AppendLine($"public {className}(DHaven.Faux.HttpSupport.IHttpClient client,");
                            constructorBuilder.AppendLine("        Microsoft.Extensions.Logging.ILogger logger)");
                            constructorBuilder.AppendLine($"    : base(client, \"{serviceName}\", \"{baseRoute}\")");
                            constructorBuilder.AppendLine("{");
                            using (var insideCxrBuilder = constructorBuilder.Indent())
                            {
                                insideCxrBuilder.AppendLine("仮logger = logger;");
                            }

                            constructorBuilder.AppendLine("}");
                        }

                        foreach (var method in typeInfo.GetMethods())
                        {
                            using (var methodBuilder = classBuilder.Indent())
                            {
                                BuildMethod(methodBuilder, method);
                            }
                        }

                        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(new[] { 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("}");
        }
        public IEnumerable <string> GenerateSource(TypeInfo typeInfo, out string fullClassName)
        {
            if (!typeInfo.IsInterface || !typeInfo.IsPublic)
            {
                throw new ArgumentException($"{typeInfo.FullName} must be a public interface");
            }

            if (typeInfo.IsGenericType)
            {
                throw new NotSupportedException($"Generic interfaces are not supported: {typeInfo.FullName}");
            }

            var className = typeInfo.FullName?.Replace(".", string.Empty);

            fullClassName = $"{Config.RootNamespace}.{className}";
            var sourceCodeList = new List <string>();

            using (logger.BeginScope("Generator {0}:", className))
            {
                var serviceName   = typeInfo.GetCustomAttribute <FauxClientAttribute>().Name;
                var baseRoute     = typeInfo.GetCustomAttribute <FauxClientAttribute>().Route ?? string.Empty;
                var fallbackClass = typeInfo.GetCustomAttribute <HystrixFauxClientAttribute>().Fallback;
                var sealedString  = Config.GenerateSealedClasses ? "sealed" : string.Empty;

                logger.LogTrace("Beginning to generate source");

                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} : DHaven.Faux.HttpSupport.DiscoveryAwareBase, {typeInfo.FullName}");
                        classBuilder.AppendLine("{");

                        using (var fieldBuilder = classBuilder.Indent())
                        {
                            fieldBuilder.AppendLine("private readonly Microsoft.Extensions.Logging.ILogger 仮logger;");
                            fieldBuilder.AppendLine($"private readonly {typeInfo.FullName} 仮fallback;");
                            fieldBuilder.AppendLine("private readonly Steeltoe.CircuitBreaker.Hystrix.IHystrixCommandGroupKey 仮groupKey;");
                        }

                        using (var constructorBuilder = classBuilder.Indent())
                        {
                            constructorBuilder.AppendLine($"public {className}(DHaven.Faux.HttpSupport.IHttpClient client,");
                            constructorBuilder.AppendLine("        Microsoft.Extensions.Logging.ILogger logger)");
                            constructorBuilder.AppendLine($"    : base(client, \"{serviceName}\", \"{baseRoute}\")");
                            constructorBuilder.AppendLine("{");
                            using (var insideCxrBuilder = constructorBuilder.Indent())
                            {
                                insideCxrBuilder.AppendLine("仮logger = logger;");
                                if (fallbackClass != null)
                                {
                                    insideCxrBuilder.AppendLine($"仮fallback = new {CompilerUtils.ToCompilableName(fallbackClass)}();");
                                }
                                insideCxrBuilder.AppendLine(
                                    $"仮groupKey = Steeltoe.CircuitBreaker.Hystrix.HystrixCommandGroupKeyDefault.AsKey(\"{serviceName}\");");
                            }

                            constructorBuilder.AppendLine("}");
                        }

                        foreach (var method in typeInfo.GetMethods())
                        {
                            sourceCodeList.Add(methodGenerator.GenerateMethodClass(method, out var hystrixCommandName));
                            BuildMethod(classBuilder.Indent(), method, hystrixCommandName);
                        }

                        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);
                        }
                    }

                    sourceCodeList.Add(sourceCode);
                    return(sourceCodeList);
                }
            }
        }
        private void BuildMethod(IndentBuilder classBuilder, MethodInfo method, string hystrixCommandName)
        {
            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));
            }

            classBuilder.Append($" {method.Name}(");
            classBuilder.Append(string.Join(", ", method.GetParameters().Select(CompilerUtils.ToParameterDeclaration)));
            classBuilder.AppendLine(")");
            classBuilder.AppendLine("{");
            using (var methodBuilder = classBuilder.Indent())
            {
                methodBuilder.Append($"var 仮command = new {hystrixCommandName}(this, 仮fallback, ");

                foreach (var inParam in method.GetParameters().Where(p => !p.IsOut))
                {
                    methodBuilder.Append($"{CompilerUtils.ToParameterUsage(inParam)}, ");
                }

                methodBuilder.AppendLine(" 仮logger);");

                methodBuilder.Append(isVoid ? string.Empty : "var 仮value = ");
                methodBuilder.AppendLine(isAsyncCall
                    ? "await 仮command.ExecuteAsync();"
                    : "仮command.Execute();");

                foreach (var outParam in method.GetParameters().Where(p => p.IsOut))
                {
                    methodBuilder.AppendLine($"{outParam.Name} = 仮command.{outParam.Name};");
                }

                if (!isVoid)
                {
                    methodBuilder.AppendLine("return 仮value;");
                }
            }

            classBuilder.AppendLine("}");
        }
        internal static bool CreateContentObjectIfSpecified(BodyAttribute bodyAttr, ParameterInfo bodyParam, IndentBuilder contentBuilder)
        {
            if (bodyAttr == null || bodyParam == null)
            {
                return(false);
            }

            var format = bodyAttr.Format;

            if (format == Format.Auto)
            {
                format = typeof(Stream).IsAssignableFrom(bodyParam.ParameterType) ? Format.Raw : Format.Json;
            }

            // Format.Auto is handled above.  At this point it is always Format.Raw or Format.Json
            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (format)
            {
            case Format.Json:
                contentBuilder.AppendLine($"var 仮content = DHaven.Faux.HttpSupport.DiscoveryAwareBase.ConvertToJson({bodyParam.Name});");
                break;

            case Format.Raw:
                contentBuilder.AppendLine($"var 仮content = DHaven.Faux.HttpSupport.DiscoveryAwareBase.StreamRawContent({bodyParam.Name});");
                break;

            default:
                return(false);
            }

            return(true);
        }
 internal static void ReturnResponseHeader(ResponseHeaderAttribute responseHeaderAttribute, Type returnType, IndentBuilder contentBuilder)
 {
     contentBuilder.AppendLine($"return DHaven.Faux.HttpSupport.DiscoveryAwareBase.GetHeaderValue<{CompilerUtils.ToCompilableName(returnType)}>(仮response, \"{responseHeaderAttribute.Header}\");");
 }
        internal static void ReturnContentObject(BodyAttribute bodyAttr, Type returnType, bool isAsyncCall, IndentBuilder contentBuilder)
        {
            if (bodyAttr == null || returnType == null)
            {
                return;
            }

            var format = bodyAttr.Format;

            if (format == Format.Auto)
            {
                format = typeof(Stream).IsAssignableFrom(returnType) ? Format.Raw : Format.Json;
            }

            // The Format.Auto is handled above, since it will always be Raw or Json at this point
            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (format)
            {
            case Format.Json:
                contentBuilder.AppendLine(isAsyncCall
                        ? $"return await DHaven.Faux.HttpSupport.DiscoveryAwareBase.ConvertToObjectAsync<{CompilerUtils.ToCompilableName(returnType)}>(仮response);"
                        : $"return DHaven.Faux.HttpSupport.DiscoveryAwareBase.ConvertToObject<{CompilerUtils.ToCompilableName(returnType)}>(仮response);");
                break;

            case Format.Raw:
                contentBuilder.AppendLine(isAsyncCall
                        ? "return await 仮response.Content.ReadAsStreamAsync();"
                        : "return 仮response.Content.ReadAsStreamAsync().Result;");
                break;

            default:
                return;
            }
        }