protobuf 2.8.0.beta9 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/{UPGRADING.md → CHANGES.md} +20 -12
  4. data/LICENSE.txt +14 -0
  5. data/README.md +60 -74
  6. data/bin/protoc-gen-ruby +26 -0
  7. data/bin/rprotoc +4 -58
  8. data/lib/protobuf.rb +15 -13
  9. data/lib/protobuf/code_generator.rb +41 -0
  10. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +51 -0
  11. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +248 -0
  12. data/lib/protobuf/field/base_field.rb +0 -1
  13. data/lib/protobuf/field/bytes_field.rb +1 -1
  14. data/lib/protobuf/field/message_field.rb +2 -4
  15. data/lib/protobuf/field/varint_field.rb +4 -7
  16. data/lib/protobuf/generators/base.rb +71 -0
  17. data/lib/protobuf/generators/enum_generator.rb +31 -0
  18. data/lib/protobuf/generators/extension_generator.rb +28 -0
  19. data/lib/protobuf/generators/field_generator.rb +132 -0
  20. data/lib/protobuf/generators/file_generator.rb +140 -0
  21. data/lib/protobuf/generators/group_generator.rb +113 -0
  22. data/lib/protobuf/generators/message_generator.rb +99 -0
  23. data/lib/protobuf/generators/printable.rb +161 -0
  24. data/lib/protobuf/generators/service_generator.rb +27 -0
  25. data/lib/protobuf/lifecycle.rb +7 -3
  26. data/lib/protobuf/message.rb +51 -34
  27. data/lib/protobuf/rpc/connectors/common.rb +4 -6
  28. data/lib/protobuf/rpc/server.rb +1 -1
  29. data/lib/protobuf/rpc/servers/zmq/server.rb +2 -2
  30. data/lib/protobuf/rpc/servers/zmq/worker.rb +2 -2
  31. data/lib/protobuf/rpc/service.rb +3 -3
  32. data/lib/protobuf/rpc/service_directory.rb +24 -12
  33. data/lib/protobuf/version.rb +1 -1
  34. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  35. data/proto/google/protobuf/descriptor.proto +620 -0
  36. data/protobuf.gemspec +12 -21
  37. data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
  38. data/spec/data/data.bin +3 -0
  39. data/{test/data/unk.png → spec/data/types.bin} +0 -0
  40. data/spec/encoding/all_types_spec.rb +91 -0
  41. data/spec/encoding/extreme_values_spec.rb +0 -0
  42. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  43. data/spec/lib/protobuf/generators/base_spec.rb +87 -0
  44. data/spec/lib/protobuf/generators/enum_generator_spec.rb +45 -0
  45. data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
  46. data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
  47. data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
  48. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  49. data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
  50. data/spec/lib/protobuf/lifecycle_spec.rb +31 -1
  51. data/spec/lib/protobuf/logger_spec.rb +5 -0
  52. data/spec/lib/protobuf/message_spec.rb +22 -8
  53. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +1 -1
  54. data/spec/lib/protobuf/rpc/service_directory_spec.rb +45 -7
  55. data/spec/lib/protobuf/rpc/service_spec.rb +3 -3
  56. data/spec/spec_helper.rb +15 -16
  57. data/spec/support/test/all_types.data.bin +0 -0
  58. data/spec/support/test/all_types.data.txt +119 -0
  59. data/spec/support/test/defaults.pb.rb +25 -0
  60. data/spec/support/test/defaults.proto +9 -0
  61. data/spec/support/test/enum.pb.rb +10 -8
  62. data/spec/support/test/extended.pb.rb +5 -3
  63. data/spec/support/test/extreme_values.data.bin +0 -0
  64. data/spec/support/test/google_unittest.pb.rb +543 -0
  65. data/spec/support/test/google_unittest.proto +713 -0
  66. data/spec/support/test/google_unittest_import.pb.rb +37 -0
  67. data/{ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h → spec/support/test/google_unittest_import.proto} +27 -17
  68. data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
  69. data/{ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h → spec/support/test/google_unittest_import_public.proto} +5 -31
  70. data/spec/support/test/multi_field_extensions.pb.rb +29 -7
  71. data/spec/support/test/multi_field_extensions.proto +12 -2
  72. data/spec/support/test/resource.pb.rb +20 -16
  73. metadata +149 -236
  74. data/examples/addressbook.pb.rb +0 -55
  75. data/examples/addressbook.proto +0 -24
  76. data/examples/reading_a_message.rb +0 -32
  77. data/examples/writing_a_message.rb +0 -46
  78. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +0 -142
  79. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +0 -318
  80. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +0 -99
  81. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +0 -103
  82. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +0 -85
  83. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +0 -167
  84. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +0 -98
  85. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +0 -72
  86. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +0 -159
  87. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +0 -170
  88. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +0 -102
  89. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +0 -103
  90. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +0 -118
  91. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +0 -104
  92. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +0 -2721
  93. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +0 -303
  94. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +0 -84
  95. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +0 -121
  96. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +0 -77
  97. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +0 -108
  98. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +0 -101
  99. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +0 -72
  100. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +0 -213
  101. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +0 -109
  102. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +0 -134
  103. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +0 -121
  104. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +0 -113
  105. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +0 -120
  106. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +0 -113
  107. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +0 -64
  108. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +0 -434
  109. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +0 -73
  110. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +0 -790
  111. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +0 -156
  112. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +0 -108
  113. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +0 -93
  114. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +0 -1367
  115. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +0 -5223
  116. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +0 -366
  117. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +0 -136
  118. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +0 -904
  119. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +0 -424
  120. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +0 -82
  121. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +0 -1102
  122. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +0 -207
  123. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +0 -136
  124. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +0 -313
  125. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +0 -238
  126. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +0 -357
  127. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +0 -340
  128. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +0 -692
  129. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +0 -239
  130. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +0 -64
  131. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +0 -80
  132. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +0 -1295
  133. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +0 -291
  134. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +0 -1211
  135. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +0 -220
  136. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +0 -119
  137. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +0 -123
  138. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +0 -121
  139. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +0 -457
  140. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +0 -170
  141. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +0 -174
  142. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +0 -101
  143. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +0 -83
  144. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +0 -98
  145. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +0 -285
  146. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +0 -11915
  147. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +0 -2895
  148. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +0 -211
  149. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +0 -56
  150. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +0 -188
  151. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +0 -151
  152. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +0 -4752
  153. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +0 -150
  154. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +0 -816
  155. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +0 -197
  156. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +0 -403
  157. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +0 -268
  158. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +0 -304
  159. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +0 -620
  160. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +0 -774
  161. data/ext/ruby_generator/Makefile +0 -10
  162. data/ext/ruby_generator/RubyGenerator.cpp +0 -544
  163. data/ext/ruby_generator/RubyGenerator.h +0 -206
  164. data/ext/ruby_generator/extconf.rb +0 -35
  165. data/test/data/data_source.py +0 -14
  166. data/test/data/types_source.py +0 -22
  167. data/test/proto/addressbook.pb.rb +0 -66
  168. data/test/proto/addressbook.proto +0 -33
  169. data/test/proto/addressbook_base.pb.rb +0 -58
  170. data/test/proto/addressbook_base.proto +0 -26
  171. data/test/proto/addressbook_ext.pb.rb +0 -20
  172. data/test/proto/addressbook_ext.proto +0 -6
  173. data/test/proto/collision.pb.rb +0 -17
  174. data/test/proto/collision.proto +0 -5
  175. data/test/proto/ext_collision.pb.rb +0 -24
  176. data/test/proto/ext_collision.proto +0 -8
  177. data/test/proto/ext_range.pb.rb +0 -22
  178. data/test/proto/ext_range.proto +0 -7
  179. data/test/proto/float_default.proto +0 -10
  180. data/test/proto/lowercase.pb.rb +0 -30
  181. data/test/proto/lowercase.proto +0 -9
  182. data/test/proto/merge.pb.rb +0 -39
  183. data/test/proto/merge.proto +0 -15
  184. data/test/proto/nested.pb.rb +0 -30
  185. data/test/proto/nested.proto +0 -9
  186. data/test/proto/optional_field.pb.rb +0 -35
  187. data/test/proto/optional_field.proto +0 -12
  188. data/test/proto/packed.pb.rb +0 -22
  189. data/test/proto/packed.proto +0 -6
  190. data/test/proto/rpc.proto +0 -6
  191. data/test/proto/types.pb.rb +0 -84
  192. data/test/proto/types.proto +0 -37
  193. data/test/test_addressbook.rb +0 -56
  194. data/test/test_enum_value.rb +0 -41
  195. data/test/test_extension.rb +0 -36
  196. data/test/test_lowercase.rb +0 -11
  197. data/test/test_message.rb +0 -128
  198. data/test/test_optional_field.rb +0 -103
  199. data/test/test_packed_field.rb +0 -40
  200. data/test/test_parse.rb +0 -15
  201. data/test/test_repeated_types.rb +0 -132
  202. data/test/test_serialize.rb +0 -61
  203. data/test/test_standard_message.rb +0 -96
  204. data/test/test_types.rb +0 -226
@@ -1,55 +0,0 @@
1
- ### Generated by rprotoc. DO NOT EDIT!
2
- ### <proto file: examples/addressbook.proto>
3
- # package tutorial;
4
- #
5
- # message Person {
6
- # required string name = 1;
7
- # required int32 id = 2;
8
- # optional string email = 3;
9
- #
10
- # enum PhoneType {
11
- # MOBILE = 0;
12
- # HOME = 1;
13
- # WORK = 2;
14
- # }
15
- #
16
- # message PhoneNumber {
17
- # required string number = 1;
18
- # optional PhoneType type = 2 [default = HOME];
19
- # }
20
- #
21
- # repeated PhoneNumber phone = 4;
22
- # }
23
- #
24
- # message AddressBook {
25
- # repeated Person person = 1;
26
- # }
27
-
28
- require 'protobuf/message/message'
29
- require 'protobuf/enum'
30
- require 'protobuf/message/extend'
31
-
32
- module Tutorial
33
- class Person < ::Protobuf::Message
34
- defined_in __FILE__
35
- required :string, :name, 1
36
- required :int32, :id, 2
37
- optional :string, :email, 3
38
- class PhoneType < ::Protobuf::Enum
39
- defined_in __FILE__
40
- MOBILE = 0
41
- HOME = 1
42
- WORK = 2
43
- end
44
- class PhoneNumber < ::Protobuf::Message
45
- defined_in __FILE__
46
- required :string, :number, 1
47
- optional :PhoneType, :type, 2, :default => :HOME
48
- end
49
- repeated :PhoneNumber, :phone, 4
50
- end
51
- class AddressBook < ::Protobuf::Message
52
- defined_in __FILE__
53
- repeated :Person, :person, 1
54
- end
55
- end
@@ -1,24 +0,0 @@
1
- package tutorial;
2
-
3
- message Person {
4
- required string name = 1;
5
- required int32 id = 2;
6
- optional string email = 3;
7
-
8
- enum PhoneType {
9
- MOBILE = 0;
10
- HOME = 1;
11
- WORK = 2;
12
- }
13
-
14
- message PhoneNumber {
15
- required string number = 1;
16
- optional PhoneType type = 2 [default = HOME];
17
- }
18
-
19
- repeated PhoneNumber phone = 4;
20
- }
21
-
22
- message AddressBook {
23
- repeated Person person = 1;
24
- }
@@ -1,32 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'addressbook.pb'
4
-
5
- def list_people(address_book)
6
- address_book.person.each do |person|
7
- puts "Person ID: #{person.id}"
8
- puts " Name: #{person.name}"
9
- puts " E-mail: #{person.email}" unless person.email.empty?
10
- person.phone.each do |phone_number|
11
- print(case phone_number.type
12
- when Tutorial::Person::PhoneType::MOBILE then
13
- ' Mobile phone #: '
14
- when Tutorial::Person::PhoneType::HOME then
15
- ' Home phone #: '
16
- when Tutorial::Person::PhoneType::WORK then
17
- ' Work phone #: '
18
- end)
19
- puts phone_number.number
20
- end
21
- end
22
- end
23
-
24
- unless ARGV.size == 1
25
- puts "Usage: #{$0} ADDRESS_BOOK_FILE"
26
- exit
27
- end
28
-
29
- address_book = Tutorial::AddressBook.new
30
- address_book.parse_from_file ARGV[0]
31
-
32
- list_people address_book
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'addressbook.pb'
4
-
5
- def prompt_for_address(person)
6
- print 'Enter person ID number: '
7
- person.id = STDIN.gets.strip.to_i
8
- print 'Enter name: '
9
- person.name = STDIN.gets.strip
10
- print 'Enter email address (blank for none): '
11
- email = STDIN.gets.strip
12
- person.email = email unless email.empty?
13
-
14
- loop do
15
- print 'Enter a phone number (or leave blank to finish): '
16
- break if (number = STDIN.gets.strip).empty?
17
-
18
- person.phone << Tutorial::Person::PhoneNumber.new
19
- person.phone.last.number = number
20
-
21
- print 'Is this a mobile, home, or work phone? '
22
- person.phone.last.type =
23
- case type = STDIN.gets.strip
24
- when 'mobile' then
25
- Tutorial::Person::PhoneType::MOBILE
26
- when 'home' then
27
- Tutorial::Person::PhoneType::HOME
28
- when 'work' then
29
- Tutorial::Person::PhoneType::WORK
30
- else
31
- puts 'Unknown phone type; leaving as default value.'
32
- nil
33
- end
34
- end
35
- end
36
-
37
- unless ARGV.size == 1
38
- puts "Usage: #{$0} ADDRESS_BOOK_FILE"
39
- exit
40
- end
41
-
42
- address_book = Tutorial::AddressBook.new
43
- address_book.parse_from_file ARGV[0] if File.exist? ARGV[0]
44
- address_book.person << Tutorial::Person.new
45
- prompt_for_address address_book.person.last
46
- address_book.serialize_to_file ARGV[0]
@@ -1,142 +0,0 @@
1
- // Protocol Buffers - Google's data interchange format
2
- // Copyright 2008 Google Inc. All rights reserved.
3
- // http://code.google.com/p/protobuf/
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
-
31
- // Author: kenton@google.com (Kenton Varda)
32
- // Based on original Protocol Buffers design by
33
- // Sanjay Ghemawat, Jeff Dean, and others.
34
- //
35
- // Defines the abstract interface implemented by each of the language-specific
36
- // code generators.
37
-
38
- #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
39
- #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
40
-
41
- #include <google/protobuf/stubs/common.h>
42
- #include <string>
43
- #include <vector>
44
- #include <utility>
45
-
46
- namespace google {
47
- namespace protobuf {
48
-
49
- namespace io { class ZeroCopyOutputStream; }
50
- class FileDescriptor;
51
-
52
- namespace compiler {
53
-
54
- // Defined in this file.
55
- class CodeGenerator;
56
- class GeneratorContext;
57
-
58
- // The abstract interface to a class which generates code implementing a
59
- // particular proto file in a particular language. A number of these may
60
- // be registered with CommandLineInterface to support various languages.
61
- class LIBPROTOC_EXPORT CodeGenerator {
62
- public:
63
- inline CodeGenerator() {}
64
- virtual ~CodeGenerator();
65
-
66
- // Generates code for the given proto file, generating one or more files in
67
- // the given output directory.
68
- //
69
- // A parameter to be passed to the generator can be specified on the
70
- // command line. This is intended to be used by Java and similar languages
71
- // to specify which specific class from the proto file is to be generated,
72
- // though it could have other uses as well. It is empty if no parameter was
73
- // given.
74
- //
75
- // Returns true if successful. Otherwise, sets *error to a description of
76
- // the problem (e.g. "invalid parameter") and returns false.
77
- virtual bool Generate(const FileDescriptor* file,
78
- const string& parameter,
79
- GeneratorContext* generator_context,
80
- string* error) const = 0;
81
-
82
- private:
83
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodeGenerator);
84
- };
85
-
86
- // CodeGenerators generate one or more files in a given directory. This
87
- // abstract interface represents the directory to which the CodeGenerator is
88
- // to write and other information about the context in which the Generator
89
- // runs.
90
- class LIBPROTOC_EXPORT GeneratorContext {
91
- public:
92
- inline GeneratorContext() {}
93
- virtual ~GeneratorContext();
94
-
95
- // Opens the given file, truncating it if it exists, and returns a
96
- // ZeroCopyOutputStream that writes to the file. The caller takes ownership
97
- // of the returned object. This method never fails (a dummy stream will be
98
- // returned instead).
99
- //
100
- // The filename given should be relative to the root of the source tree.
101
- // E.g. the C++ generator, when generating code for "foo/bar.proto", will
102
- // generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that
103
- // "foo/" is included in these filenames. The filename is not allowed to
104
- // contain "." or ".." components.
105
- virtual io::ZeroCopyOutputStream* Open(const string& filename) = 0;
106
-
107
- // Creates a ZeroCopyOutputStream which will insert code into the given file
108
- // at the given insertion point. See plugin.proto (plugin.pb.h) for more
109
- // information on insertion points. The default implementation
110
- // assert-fails -- it exists only for backwards-compatibility.
111
- //
112
- // WARNING: This feature is currently EXPERIMENTAL and is subject to change.
113
- virtual io::ZeroCopyOutputStream* OpenForInsert(
114
- const string& filename, const string& insertion_point);
115
-
116
- // Returns a vector of FileDescriptors for all the files being compiled
117
- // in this run. Useful for languages, such as Go, that treat files
118
- // differently when compiled as a set rather than individually.
119
- virtual void ListParsedFiles(vector<const FileDescriptor*>* output);
120
-
121
- private:
122
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext);
123
- };
124
-
125
- // The type GeneratorContext was once called OutputDirectory. This typedef
126
- // provides backward compatibility.
127
- typedef GeneratorContext OutputDirectory;
128
-
129
- // Several code generators treat the parameter argument as holding a
130
- // list of options separated by commas. This helper function parses
131
- // a set of comma-delimited name/value pairs: e.g.,
132
- // "foo=bar,baz,qux=corge"
133
- // parses to the pairs:
134
- // ("foo", "bar"), ("baz", ""), ("qux", "corge")
135
- extern void ParseGeneratorParameter(const string&,
136
- vector<pair<string, string> >*);
137
-
138
- } // namespace compiler
139
- } // namespace protobuf
140
-
141
- } // namespace google
142
- #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
@@ -1,318 +0,0 @@
1
- // Protocol Buffers - Google's data interchange format
2
- // Copyright 2008 Google Inc. All rights reserved.
3
- // http://code.google.com/p/protobuf/
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
-
31
- // Author: kenton@google.com (Kenton Varda)
32
- // Based on original Protocol Buffers design by
33
- // Sanjay Ghemawat, Jeff Dean, and others.
34
- //
35
- // Implements the Protocol Compiler front-end such that it may be reused by
36
- // custom compilers written to support other languages.
37
-
38
- #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
39
- #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
40
-
41
- #include <google/protobuf/stubs/common.h>
42
- #include <string>
43
- #include <vector>
44
- #include <map>
45
- #include <set>
46
- #include <utility>
47
-
48
- namespace google {
49
- namespace protobuf {
50
-
51
- class FileDescriptor; // descriptor.h
52
- class DescriptorPool; // descriptor.h
53
- class FileDescriptorProto; // descriptor.pb.h
54
- template<typename T> class RepeatedPtrField; // repeated_field.h
55
-
56
- namespace compiler {
57
-
58
- class CodeGenerator; // code_generator.h
59
- class GeneratorContext; // code_generator.h
60
- class DiskSourceTree; // importer.h
61
-
62
- // This class implements the command-line interface to the protocol compiler.
63
- // It is designed to make it very easy to create a custom protocol compiler
64
- // supporting the languages of your choice. For example, if you wanted to
65
- // create a custom protocol compiler binary which includes both the regular
66
- // C++ support plus support for your own custom output "Foo", you would
67
- // write a class "FooGenerator" which implements the CodeGenerator interface,
68
- // then write a main() procedure like this:
69
- //
70
- // int main(int argc, char* argv[]) {
71
- // google::protobuf::compiler::CommandLineInterface cli;
72
- //
73
- // // Support generation of C++ source and headers.
74
- // google::protobuf::compiler::cpp::CppGenerator cpp_generator;
75
- // cli.RegisterGenerator("--cpp_out", &cpp_generator,
76
- // "Generate C++ source and header.");
77
- //
78
- // // Support generation of Foo code.
79
- // FooGenerator foo_generator;
80
- // cli.RegisterGenerator("--foo_out", &foo_generator,
81
- // "Generate Foo file.");
82
- //
83
- // return cli.Run(argc, argv);
84
- // }
85
- //
86
- // The compiler is invoked with syntax like:
87
- // protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto
88
- //
89
- // For a full description of the command-line syntax, invoke it with --help.
90
- class LIBPROTOC_EXPORT CommandLineInterface {
91
- public:
92
- CommandLineInterface();
93
- ~CommandLineInterface();
94
-
95
- // Register a code generator for a language.
96
- //
97
- // Parameters:
98
- // * flag_name: The command-line flag used to specify an output file of
99
- // this type. The name must start with a '-'. If the name is longer
100
- // than one letter, it must start with two '-'s.
101
- // * generator: The CodeGenerator which will be called to generate files
102
- // of this type.
103
- // * help_text: Text describing this flag in the --help output.
104
- //
105
- // Some generators accept extra parameters. You can specify this parameter
106
- // on the command-line by placing it before the output directory, separated
107
- // by a colon:
108
- // protoc --foo_out=enable_bar:outdir
109
- // The text before the colon is passed to CodeGenerator::Generate() as the
110
- // "parameter".
111
- void RegisterGenerator(const string& flag_name,
112
- CodeGenerator* generator,
113
- const string& help_text);
114
-
115
- // Enables "plugins". In this mode, if a command-line flag ends with "_out"
116
- // but does not match any registered generator, the compiler will attempt to
117
- // find a "plugin" to implement the generator. Plugins are just executables.
118
- // They should live somewhere in the PATH.
119
- //
120
- // The compiler determines the executable name to search for by concatenating
121
- // exe_name_prefix with the unrecognized flag name, removing "_out". So, for
122
- // example, if exe_name_prefix is "protoc-" and you pass the flag --foo_out,
123
- // the compiler will try to run the program "protoc-foo".
124
- //
125
- // The plugin program should implement the following usage:
126
- // plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS
127
- // --out indicates the output directory (as passed to the --foo_out
128
- // parameter); if omitted, the current directory should be used. --parameter
129
- // gives the generator parameter, if any was provided. The PROTO_FILES list
130
- // the .proto files which were given on the compiler command-line; these are
131
- // the files for which the plugin is expected to generate output code.
132
- // Finally, DESCRIPTORS is an encoded FileDescriptorSet (as defined in
133
- // descriptor.proto). This is piped to the plugin's stdin. The set will
134
- // include descriptors for all the files listed in PROTO_FILES as well as
135
- // all files that they import. The plugin MUST NOT attempt to read the
136
- // PROTO_FILES directly -- it must use the FileDescriptorSet.
137
- //
138
- // The plugin should generate whatever files are necessary, as code generators
139
- // normally do. It should write the names of all files it generates to
140
- // stdout. The names should be relative to the output directory, NOT absolute
141
- // names or relative to the current directory. If any errors occur, error
142
- // messages should be written to stderr. If an error is fatal, the plugin
143
- // should exit with a non-zero exit code.
144
- void AllowPlugins(const string& exe_name_prefix);
145
-
146
- // Run the Protocol Compiler with the given command-line parameters.
147
- // Returns the error code which should be returned by main().
148
- //
149
- // It may not be safe to call Run() in a multi-threaded environment because
150
- // it calls strerror(). I'm not sure why you'd want to do this anyway.
151
- int Run(int argc, const char* const argv[]);
152
-
153
- // Call SetInputsAreCwdRelative(true) if the input files given on the command
154
- // line should be interpreted relative to the proto import path specified
155
- // using --proto_path or -I flags. Otherwise, input file names will be
156
- // interpreted relative to the current working directory (or as absolute
157
- // paths if they start with '/'), though they must still reside inside
158
- // a directory given by --proto_path or the compiler will fail. The latter
159
- // mode is generally more intuitive and easier to use, especially e.g. when
160
- // defining implicit rules in Makefiles.
161
- void SetInputsAreProtoPathRelative(bool enable) {
162
- inputs_are_proto_path_relative_ = enable;
163
- }
164
-
165
- // Provides some text which will be printed when the --version flag is
166
- // used. The version of libprotoc will also be printed on the next line
167
- // after this text.
168
- void SetVersionInfo(const string& text) {
169
- version_info_ = text;
170
- }
171
-
172
-
173
- private:
174
- // -----------------------------------------------------------------
175
-
176
- class ErrorPrinter;
177
- class GeneratorContextImpl;
178
- class MemoryOutputStream;
179
-
180
- // Clear state from previous Run().
181
- void Clear();
182
-
183
- // Remaps each file in input_files_ so that it is relative to one of the
184
- // directories in proto_path_. Returns false if an error occurred. This
185
- // is only used if inputs_are_proto_path_relative_ is false.
186
- bool MakeInputsBeProtoPathRelative(
187
- DiskSourceTree* source_tree);
188
-
189
- // Parse all command-line arguments.
190
- bool ParseArguments(int argc, const char* const argv[]);
191
-
192
- // Parses a command-line argument into a name/value pair. Returns
193
- // true if the next argument in the argv should be used as the value,
194
- // false otherwise.
195
- //
196
- // Exmaples:
197
- // "-Isrc/protos" ->
198
- // name = "-I", value = "src/protos"
199
- // "--cpp_out=src/foo.pb2.cc" ->
200
- // name = "--cpp_out", value = "src/foo.pb2.cc"
201
- // "foo.proto" ->
202
- // name = "", value = "foo.proto"
203
- bool ParseArgument(const char* arg, string* name, string* value);
204
-
205
- // Interprets arguments parsed with ParseArgument.
206
- bool InterpretArgument(const string& name, const string& value);
207
-
208
- // Print the --help text to stderr.
209
- void PrintHelpText();
210
-
211
- // Generate the given output file from the given input.
212
- struct OutputDirective; // see below
213
- bool GenerateOutput(const vector<const FileDescriptor*>& parsed_files,
214
- const OutputDirective& output_directive,
215
- GeneratorContext* generator_context);
216
- bool GeneratePluginOutput(const vector<const FileDescriptor*>& parsed_files,
217
- const string& plugin_name,
218
- const string& parameter,
219
- GeneratorContext* generator_context,
220
- string* error);
221
-
222
- // Implements --encode and --decode.
223
- bool EncodeOrDecode(const DescriptorPool* pool);
224
-
225
- // Implements the --descriptor_set_out option.
226
- bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files);
227
-
228
- // Get all transitive dependencies of the given file (including the file
229
- // itself), adding them to the given list of FileDescriptorProtos. The
230
- // protos will be ordered such that every file is listed before any file that
231
- // depends on it, so that you can call DescriptorPool::BuildFile() on them
232
- // in order. Any files in *already_seen will not be added, and each file
233
- // added will be inserted into *already_seen.
234
- static void GetTransitiveDependencies(
235
- const FileDescriptor* file,
236
- set<const FileDescriptor*>* already_seen,
237
- RepeatedPtrField<FileDescriptorProto>* output);
238
-
239
- // -----------------------------------------------------------------
240
-
241
- // The name of the executable as invoked (i.e. argv[0]).
242
- string executable_name_;
243
-
244
- // Version info set with SetVersionInfo().
245
- string version_info_;
246
-
247
- // Map from flag names to registered generators.
248
- struct GeneratorInfo {
249
- CodeGenerator* generator;
250
- string help_text;
251
- };
252
- typedef map<string, GeneratorInfo> GeneratorMap;
253
- GeneratorMap generators_;
254
-
255
- // See AllowPlugins(). If this is empty, plugins aren't allowed.
256
- string plugin_prefix_;
257
-
258
- // Maps specific plugin names to files. When executing a plugin, this map
259
- // is searched first to find the plugin executable. If not found here, the
260
- // PATH (or other OS-specific search strategy) is searched.
261
- map<string, string> plugins_;
262
-
263
- // Stuff parsed from command line.
264
- enum Mode {
265
- MODE_COMPILE, // Normal mode: parse .proto files and compile them.
266
- MODE_ENCODE, // --encode: read text from stdin, write binary to stdout.
267
- MODE_DECODE // --decode: read binary from stdin, write text to stdout.
268
- };
269
-
270
- Mode mode_;
271
-
272
- enum ErrorFormat {
273
- ERROR_FORMAT_GCC, // GCC error output format (default).
274
- ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs).
275
- };
276
-
277
- ErrorFormat error_format_;
278
-
279
- vector<pair<string, string> > proto_path_; // Search path for proto files.
280
- vector<string> input_files_; // Names of the input proto files.
281
-
282
- // output_directives_ lists all the files we are supposed to output and what
283
- // generator to use for each.
284
- struct OutputDirective {
285
- string name; // E.g. "--foo_out"
286
- CodeGenerator* generator; // NULL for plugins
287
- string parameter;
288
- string output_location;
289
- };
290
- vector<OutputDirective> output_directives_;
291
-
292
- // When using --encode or --decode, this names the type we are encoding or
293
- // decoding. (Empty string indicates --decode_raw.)
294
- string codec_type_;
295
-
296
- // If --descriptor_set_out was given, this is the filename to which the
297
- // FileDescriptorSet should be written. Otherwise, empty.
298
- string descriptor_set_name_;
299
-
300
- // True if --include_imports was given, meaning that we should
301
- // write all transitive dependencies to the DescriptorSet. Otherwise, only
302
- // the .proto files listed on the command-line are added.
303
- bool imports_in_descriptor_set_;
304
-
305
- // Was the --disallow_services flag used?
306
- bool disallow_services_;
307
-
308
- // See SetInputsAreProtoPathRelative().
309
- bool inputs_are_proto_path_relative_;
310
-
311
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface);
312
- };
313
-
314
- } // namespace compiler
315
- } // namespace protobuf
316
-
317
- } // namespace google
318
- #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__