Пример #1
0
        public bool Render(CSharpModuleContext context, Fragment fragment, IndentedTextWriter writer)
        {
            if (!(fragment is ObjectFragment objectFragment))
            {
                return(false);
            }

            if (objectFragment.Hints.TryGetValue("source-type", out var sourceType))
            {
                writer.WriteIndentedLine($"// source: {sourceType}");
                writer.WriteSeparatingLine();
            }

            var genericSuffix = string.Join(", ", objectFragment.GenericArguments.Select(a => a.Name));

            using (writer.WriteIndentedBlock(prefix: $"public class {CS.Type(objectFragment.Name)}{(genericSuffix.Length > 0 ? $"<{genericSuffix}>" : "")} "))
            {
                if (objectFragment.Constants.Any())
                {
                    foreach (var constant in objectFragment.Constants)
                    {
                        writer.WriteIndentedLine($"public const {context.Resolve(constant.Type)} {CS.Field(constant.Name)} = {CS.Constant(constant.Value)};");
                    }
                    writer.WriteSeparatingLine();
                }

                foreach (var field in objectFragment.Fields)
                {
                    writer.WriteIndentedLine($"public {context.Resolve(field.Type)} {CS.Field(field.Name)} {{ get; set; }}");
                }
            }

            return(true);
        }
Пример #2
0
        public bool Render(CSharpModuleContext context, Fragment fragment, IndentedTextWriter writer)
        {
            if (!(fragment is ServiceFragment serviceFragment))
            {
                return(false);
            }

            if (serviceFragment.Hints.TryGetValue("source-type", out var sourceType))
            {
                writer.WriteIndentedLine($"// source: {sourceType}");
                writer.WriteSeparatingLine();
            }

            context.Import("Odachi.Abstractions");

            using (writer.WriteIndentedBlock(prefix: $"public class {CS.Type(serviceFragment.Name)} "))
            {
                if (serviceFragment.Constants.Any())
                {
                    foreach (var constant in serviceFragment.Constants)
                    {
                        writer.WriteIndentedLine($"public const {context.Resolve(constant.Type)} {CS.Field(constant.Name)} = {CS.Constant(constant.Value)};");
                    }
                    writer.WriteSeparatingLine();
                }

                using (writer.WriteIndentedBlock(prefix: $"public {CS.Type(serviceFragment.Name)}(IRpcClient client) "))
                {
                    writer.WriteIndentedLine("this._client = client;");
                }

                writer.WriteIndentedLine("private IRpcClient _client;");
                writer.WriteSeparatingLine();

                foreach (var method in serviceFragment.Methods)
                {
                    context.Import("System.Threading.Tasks");

                    var rpcModuleName = method.Hints["jsonrpc-module-name"];
                    var rpcMethodName = method.Hints["jsonrpc-method-name"] ?? method.Name;

                    writer.WriteIndent();
                    if (method.ReturnType.Name == "void")
                    {
                        writer.Write($"public Task ");
                    }
                    else
                    {
                        writer.Write($"public Task<{context.Resolve(method.ReturnType)}> ");
                    }
                    writer.Write($"{CS.Method(method.Name)}Async({string.Join(", ", method.Parameters.Select(p => $"{context.Resolve(p.Type)} {CS.Parameter(p.Name)}"))})");
                    using (writer.WriteBlock())
                    {
                        if (method.ReturnType.Name == "void")
                        {
                            writer.WriteIndentedLine($"return _client.CallAsync(\"{rpcModuleName}\", \"{rpcMethodName}\", new {{ {string.Join(", ", method.Parameters.Select(p => CS.Parameter(p.Name)))} }});");
                        }
                        else
                        {
                            writer.WriteIndentedLine($"return _client.CallAsync<{context.Resolve(method.ReturnType)}>(\"{rpcModuleName}\", \"{rpcMethodName}\", new {{ {string.Join(", ", method.Parameters.Select(p => CS.Parameter(p.Name)))} }});");
                        }
                    }
                    writer.WriteLine();
                    writer.WriteSeparatingLine();
                }
            }

            return(true);
        }