public GenerateResult Generate(string schema = null, string tooling = null)
        {
            if (string.IsNullOrWhiteSpace(schema))
            {
                return(null);
            }
            var result = new GenerateResult();

            try
            {
                using (var reader = new StringReader(schema))
                {
                    var set = new FileDescriptorSet
                    {
                        ImportValidator = path => ValidateImport(path),
                    };
                    set.AddImportPath(Path.Combine(_host.WebRootPath, "protoc"));
                    set.Add("my.proto", true, reader);

                    set.Process();
                    var errors = set.GetErrors();

                    if (!ProtocTooling.IsDefined(tooling))
                    {
                        if (errors.Length > 0)
                        {
                            result.ParserExceptions = errors;
                        }
                        result.Files = CSharpCodeGenerator.Default.Generate(set).ToArray();
                    }
                    else
                    {
                        // we're going to offer protoc! hold me...
                        if (errors.Length != 0 && schema.Contains("import"))
                        {
                            // code output disabled because of import
                        }
                        else
                        {
                            result.Files = RunProtoc(_host, schema, tooling, out errors);
                            if (errors.Length > 0)
                            {
                                result.ParserExceptions = errors;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return(result);
        }
예제 #2
0
        public GenerateResult Generate(string schema = null, string tooling = null, string names = null)
        {
            if (string.IsNullOrWhiteSpace(schema))
            {
                return(null);
            }

            Dictionary <string, string> options = null;

            foreach (var field in Request.Form)
            {
                switch (field.Key)
                {
                case nameof(schema):
                case nameof(tooling):
                case nameof(names):
                    break;     // handled separately

                default:
                    string s = field.Value;
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        if (options == null)
                        {
                            options = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                        }
                        options[field.Key] = s;
                    }
                    break;
                }
            }

            NameNormalizer nameNormalizer = null;

            switch (names)
            {
            case "auto":
                nameNormalizer = NameNormalizer.Default;
                break;

            case "original":
                nameNormalizer = NameNormalizer.Null;
                break;
            }
            var result = new GenerateResult();

            try
            {
                using (var reader = new StringReader(schema))
                {
                    var set = new FileDescriptorSet
                    {
                        ImportValidator = path => ValidateImport(path),
                    };
                    set.AddImportPath(Path.Combine(_host.WebRootPath, "protoc"));
                    set.Add("my.proto", true, reader);

                    set.Process();
                    var errors = set.GetErrors();

                    if (!ProtocTooling.IsDefined(tooling))
                    {
                        if (errors.Length > 0)
                        {
                            result.ParserExceptions = errors;
                        }
                        CodeGenerator codegen;
                        switch (tooling)
                        {
                        case "protogen:VB":
#pragma warning disable 0618
                            codegen = VBCodeGenerator.Default;
#pragma warning restore 0618
                            break;

                        case "protogen:C#":
                        default:
                            codegen = CSharpCodeGenerator.Default;
                            break;
                        }
                        result.Files = codegen.Generate(set, nameNormalizer, options).ToArray();
                    }
                    else
                    {
                        // we're going to offer protoc! hold me...
                        if (errors.Length != 0 && schema.Contains("import"))
                        {
                            // code output disabled because of import
                        }
                        else
                        {
                            result.Files = RunProtoc(_host, schema, tooling, out errors);
                            if (errors.Length > 0)
                            {
                                result.ParserExceptions = errors;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return(result);
        }