csharp_generator.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <sstream>
  31. #include <google/protobuf/compiler/code_generator.h>
  32. #include <google/protobuf/compiler/plugin.h>
  33. #include <google/protobuf/descriptor.h>
  34. #include <google/protobuf/descriptor.pb.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/io/zero_copy_stream.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. #include <google/protobuf/compiler/csharp/csharp_generator.h>
  39. #include <google/protobuf/compiler/csharp/csharp_helpers.h>
  40. #include <google/protobuf/compiler/csharp/csharp_names.h>
  41. #include <google/protobuf/compiler/csharp/csharp_options.h>
  42. #include <google/protobuf/compiler/csharp/csharp_reflection_class.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace compiler {
  46. namespace csharp {
  47. void GenerateFile(const google::protobuf::FileDescriptor* file,
  48. io::Printer* printer,
  49. const Options* options) {
  50. ReflectionClassGenerator reflectionClassGenerator(file, options);
  51. reflectionClassGenerator.Generate(printer);
  52. }
  53. bool Generator::Generate(
  54. const FileDescriptor* file,
  55. const string& parameter,
  56. GeneratorContext* generator_context,
  57. string* error) const {
  58. std::vector<std::pair<string, string> > options;
  59. ParseGeneratorParameter(parameter, &options);
  60. // We only support proto3 - but we make an exception for descriptor.proto.
  61. if (file->syntax() != FileDescriptor::SYNTAX_PROTO3 && !IsDescriptorProto(file)) {
  62. *error = "C# code generation only supports proto3 syntax";
  63. return false;
  64. }
  65. struct Options cli_options;
  66. for (int i = 0; i < options.size(); i++) {
  67. if (options[i].first == "file_extension") {
  68. cli_options.file_extension = options[i].second;
  69. } else if (options[i].first == "base_namespace") {
  70. cli_options.base_namespace = options[i].second;
  71. cli_options.base_namespace_specified = true;
  72. } else if (options[i].first == "internal_access") {
  73. cli_options.internal_access = true;
  74. } else {
  75. *error = "Unknown generator option: " + options[i].first;
  76. return false;
  77. }
  78. }
  79. string filename_error = "";
  80. std::string filename = GetOutputFile(file,
  81. cli_options.file_extension,
  82. cli_options.base_namespace_specified,
  83. cli_options.base_namespace,
  84. &filename_error);
  85. if (filename.empty()) {
  86. *error = filename_error;
  87. return false;
  88. }
  89. std::unique_ptr<io::ZeroCopyOutputStream> output(
  90. generator_context->Open(filename));
  91. io::Printer printer(output.get(), '$');
  92. GenerateFile(file, &printer, &cli_options);
  93. return true;
  94. }
  95. } // namespace csharp
  96. } // namespace compiler
  97. } // namespace protobuf
  98. } // namespace google