protobuf 1.4.2 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +4 -0
  2. data/Rakefile +7 -7
  3. data/bin/rpc_server +2 -188
  4. data/bin/rprotoc +44 -38
  5. data/examples/addressbook.pb.rb +2 -2
  6. data/ext/Makefile +11 -0
  7. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
  8. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
  9. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
  10. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
  11. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
  12. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
  13. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
  14. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
  15. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
  16. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
  17. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
  18. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
  19. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
  20. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
  21. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
  22. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
  23. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
  24. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
  25. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
  26. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
  27. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
  28. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
  29. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
  30. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
  31. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
  32. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
  33. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
  34. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
  35. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
  36. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
  37. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
  38. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
  39. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
  40. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
  41. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
  42. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
  43. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
  44. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
  45. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
  46. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
  47. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
  48. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
  49. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
  50. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
  51. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
  52. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
  53. data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
  54. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
  55. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
  56. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
  57. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
  58. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
  59. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
  60. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
  61. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
  62. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
  63. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
  64. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
  65. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
  66. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
  67. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
  68. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
  69. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
  70. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
  71. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
  72. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
  73. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
  74. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
  75. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
  76. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
  77. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
  78. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
  79. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
  80. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
  81. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
  82. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
  83. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
  84. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
  85. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
  86. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
  87. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
  88. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
  89. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
  90. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
  91. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
  92. data/ext/ruby_generator/RubyGenerator.cpp +435 -0
  93. data/ext/ruby_generator/RubyGenerator.h +180 -0
  94. data/ext/ruby_generator/extconf.rb +24 -0
  95. data/lib/protobuf.rb +55 -33
  96. data/lib/protobuf/cli.rb +176 -0
  97. data/lib/protobuf/common/logger.rb +11 -11
  98. data/lib/protobuf/{message/enum.rb → enum.rb} +0 -10
  99. data/lib/protobuf/evented.rb +22 -0
  100. data/lib/protobuf/field.rb +57 -0
  101. data/lib/protobuf/field/base_field.rb +210 -0
  102. data/lib/protobuf/field/bool_field.rb +24 -0
  103. data/lib/protobuf/field/bytes_field.rb +38 -0
  104. data/lib/protobuf/field/double_field.rb +19 -0
  105. data/lib/protobuf/field/enum_field.rb +58 -0
  106. data/lib/protobuf/field/extension_fields.rb +24 -0
  107. data/lib/protobuf/field/field_array.rb +55 -0
  108. data/lib/protobuf/field/fixed32_field.rb +19 -0
  109. data/lib/protobuf/field/fixed64_field.rb +22 -0
  110. data/lib/protobuf/field/float_field.rb +29 -0
  111. data/lib/protobuf/field/int32_field.rb +10 -0
  112. data/lib/protobuf/field/int64_field.rb +10 -0
  113. data/lib/protobuf/field/integer_field.rb +19 -0
  114. data/lib/protobuf/field/message_field.rb +51 -0
  115. data/lib/protobuf/field/sfixed32_field.rb +21 -0
  116. data/lib/protobuf/field/sfixed64_field.rb +24 -0
  117. data/lib/protobuf/field/signed_integer_field.rb +23 -0
  118. data/lib/protobuf/field/sint32_field.rb +10 -0
  119. data/lib/protobuf/field/sint64_field.rb +10 -0
  120. data/lib/protobuf/field/string_field.rb +14 -0
  121. data/lib/protobuf/field/uint32_field.rb +10 -0
  122. data/lib/protobuf/field/uint64_field.rb +10 -0
  123. data/lib/protobuf/field/varint_field.rb +63 -0
  124. data/lib/protobuf/message.rb +376 -0
  125. data/lib/protobuf/message/message.rb +1 -475
  126. data/lib/protobuf/rpc/buffer.rb +1 -1
  127. data/lib/protobuf/rpc/client.rb +26 -26
  128. data/lib/protobuf/rpc/connector.rb +15 -14
  129. data/lib/protobuf/rpc/connectors/eventmachine.rb +13 -13
  130. data/lib/protobuf/rpc/rpc.pb.rb +39 -28
  131. data/lib/protobuf/rpc/server.rb +7 -28
  132. data/lib/protobuf/rpc/servers/evented_runner.rb +15 -15
  133. data/lib/protobuf/rpc/servers/socket/server.rb +2 -1
  134. data/lib/protobuf/rpc/servers/socket_runner.rb +5 -6
  135. data/lib/protobuf/rpc/servers/zmq_runner.rb +4 -5
  136. data/lib/protobuf/socket.rb +22 -0
  137. data/lib/protobuf/version.rb +2 -1
  138. data/lib/protobuf/zmq.rb +21 -0
  139. data/proto/rpc.pb.rb +48 -0
  140. data/protobuf.gemspec +14 -6
  141. data/spec/benchmark/tasks.rb +42 -23
  142. data/spec/functional/evented_server_spec.rb +2 -0
  143. data/spec/functional/socket_server_spec.rb +22 -26
  144. data/spec/functional/zmq_server_spec.rb +22 -26
  145. data/spec/lib/protobuf/cli_spec.rb +182 -0
  146. data/spec/{unit → lib/protobuf}/common/logger_spec.rb +0 -0
  147. data/spec/{unit → lib/protobuf/message}/enum_spec.rb +0 -0
  148. data/spec/{unit → lib/protobuf/message}/message_spec.rb +0 -0
  149. data/spec/{unit → lib/protobuf}/rpc/client_spec.rb +6 -4
  150. data/spec/lib/protobuf/rpc/connector_spec.rb +41 -0
  151. data/spec/{unit → lib/protobuf}/rpc/connectors/base_spec.rb +18 -18
  152. data/spec/{unit → lib/protobuf}/rpc/connectors/common_spec.rb +20 -20
  153. data/spec/{unit → lib/protobuf}/rpc/connectors/socket_spec.rb +9 -8
  154. data/spec/{unit → lib/protobuf}/rpc/connectors/zmq_spec.rb +1 -1
  155. data/spec/{unit → lib/protobuf}/rpc/servers/evented_server_spec.rb +4 -4
  156. data/spec/{unit → lib/protobuf}/rpc/servers/socket_server_spec.rb +11 -23
  157. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/broker_spec.rb +5 -0
  158. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/server_spec.rb +5 -0
  159. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/util_spec.rb +5 -0
  160. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/worker_spec.rb +5 -0
  161. data/spec/{unit → lib/protobuf}/rpc/service_spec.rb +0 -0
  162. data/spec/proto/test.pb.rb +48 -25
  163. data/spec/spec_helper.rb +9 -15
  164. data/spec/support/all.rb +7 -0
  165. data/spec/{helper → support}/server.rb +5 -7
  166. data/spec/{helper → support}/silent_constants.rb +4 -0
  167. data/spec/support/test_app_file.rb +2 -0
  168. data/spec/{helper → support}/tolerance_matcher.rb +0 -0
  169. data/test/proto/types.pb.rb +1 -1
  170. data/test/test_addressbook.rb +1 -1
  171. data/test/test_enum_value.rb +1 -1
  172. data/test/test_standard_message.rb +1 -1
  173. metadata +243 -107
  174. data/lib/protobuf/common/util.rb +0 -59
  175. data/lib/protobuf/compiler/compiler.rb +0 -52
  176. data/lib/protobuf/compiler/nodes.rb +0 -323
  177. data/lib/protobuf/compiler/proto.y +0 -216
  178. data/lib/protobuf/compiler/proto2.ebnf +0 -79
  179. data/lib/protobuf/compiler/proto_parser.rb +0 -1425
  180. data/lib/protobuf/compiler/template/rpc_service_implementation.erb +0 -42
  181. data/lib/protobuf/compiler/visitors.rb +0 -282
  182. data/lib/protobuf/descriptor/descriptor.proto +0 -286
  183. data/lib/protobuf/descriptor/descriptor.rb +0 -55
  184. data/lib/protobuf/descriptor/descriptor_builder.rb +0 -143
  185. data/lib/protobuf/descriptor/descriptor_proto.rb +0 -138
  186. data/lib/protobuf/descriptor/enum_descriptor.rb +0 -33
  187. data/lib/protobuf/descriptor/field_descriptor.rb +0 -49
  188. data/lib/protobuf/descriptor/file_descriptor.rb +0 -37
  189. data/lib/protobuf/message/field.rb +0 -684
  190. data/lib/protobuf/message/protoable.rb +0 -38
  191. data/spec/helper/all.rb +0 -7
  192. data/spec/unit/common/util_spec.rb +0 -17
  193. data/spec/unit/rpc/connector_spec.rb +0 -31
  194. data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +0 -32
  195. data/spec/unit/rpc/connectors/eventmachine_spec.rb +0 -0
  196. data/test/check_unbuild.rb +0 -30
  197. data/test/test_compiler.rb +0 -325
  198. data/test/test_descriptor.rb +0 -122
@@ -1,475 +1 @@
1
- require 'set'
2
- require 'protobuf/descriptor/descriptor'
3
- require 'protobuf/message/decoder'
4
- require 'protobuf/message/encoder'
5
- require 'protobuf/message/field'
6
- require 'protobuf/message/protoable'
7
- require 'json'
8
-
9
- module Protobuf
10
- OPTIONS = {}
11
-
12
- class Message
13
-
14
- class ExtensionFields < Hash
15
- def initialize(key_range=0..-1)
16
- @key_range = key_range
17
- end
18
-
19
- def []=(key, value)
20
- raise RangeError, "#{key} is not in #{@key_range}" unless @key_range.include?(key)
21
- super
22
- end
23
-
24
- def include_tag?(tag)
25
- @key_range.include?(tag)
26
- end
27
- end
28
-
29
- def self.inherited(klass)
30
- @_children ||= Set.new
31
- @_children << klass
32
- end
33
-
34
- def self.pre_cache_class_definitions
35
- @_children ||= Set.new
36
- @_children.each do |child_class|
37
- child_class.new
38
- end
39
- end
40
-
41
- class << self
42
- include Protoable
43
-
44
- def all_fields
45
- @all_fields ||= begin
46
- fields_hash = fields.merge(extension_fields)
47
- ordered_keys = fields_hash.keys.sort
48
- ordered_keys.map { |key| fields_hash[key] }
49
- end
50
- end
51
-
52
- # Reserve field numbers for extensions. Don't use this method directly.
53
- def extensions(range)
54
- @extension_fields = ExtensionFields.new(range)
55
- end
56
-
57
- # Define a required field. Don't use this method directly.
58
- def required(type, name, tag, options={})
59
- define_field(:required, type, name, tag, options)
60
- end
61
-
62
- # Define a optional field. Don't use this method directly.
63
- def optional(type, name, tag, options={})
64
- define_field(:optional, type, name, tag, options)
65
- end
66
-
67
- # Define a repeated field. Don't use this method directly.
68
- def repeated(type, name, tag, options={})
69
- define_field(:repeated, type, name, tag, options)
70
- end
71
-
72
- def descriptor
73
- @descriptor ||= Descriptor::Descriptor.new(self)
74
- end
75
-
76
- # Define a field. Don't use this method directly.
77
- def define_field(rule, type, fname, tag, options)
78
- field_hash = options[:extension] ? extension_fields : fields
79
- field_name_hash = options[:extension] ? extension_fields_by_name : fields_by_name
80
-
81
- if field_hash.keys.include?(tag)
82
- raise TagCollisionError, %!{Field number #{tag} has already been used in "#{self.name}" by field "#{fname}".!
83
- end
84
-
85
- field_definition = Field.build(self, rule, type, fname, tag, options)
86
- field_name_hash[fname.to_sym] = field_definition
87
- field_hash[tag] = field_definition
88
- end
89
-
90
- def extension_tag?(tag)
91
- extension_fields.include_tag?(tag)
92
- end
93
-
94
- # An extension field object.
95
- def extension_fields
96
- @extension_fields ||= ExtensionFields.new
97
- end
98
-
99
- def extension_fields_by_name
100
- @extension_fields_by_name ||= {}
101
- end
102
-
103
- # A collection of field object.
104
- def fields
105
- @fields ||= {}
106
- end
107
-
108
- def fields_by_name
109
- @field_by_name ||= {}
110
- end
111
-
112
- def repeated_fields
113
- @repeated_fields ||= []
114
- end
115
-
116
- def repeated_extension_fields
117
- @repeated_extension_fields ||= []
118
- end
119
-
120
- # Find a field object by +name+.
121
- def get_field_by_name(name)
122
- # Check if the name has been used before, if not then set it to the sym value
123
- fields_by_name[name] ||= fields_by_name[name.to_sym]
124
- end
125
-
126
- # Find a field object by +tag+ number.
127
- def get_field_by_tag(tag)
128
- fields[tag]
129
- end
130
-
131
- def field_cache
132
- @field_cache ||= {}
133
- end
134
-
135
- # Find a field object by +tag_or_name+.
136
- def get_field(tag_or_name)
137
- field_cache[tag_or_name] ||= case tag_or_name
138
- when Integer then get_field_by_tag(tag_or_name)
139
- when String, Symbol then get_field_by_name(tag_or_name)
140
- else raise TypeError, tag_or_name.class
141
- end
142
- end
143
-
144
- #TODO merge to get_field_by_name
145
- def get_ext_field_by_name(name)
146
- # Check if the name has been used before, if not then set it to the sym value
147
- extension_fields_by_name[name] ||= extension_fields_by_name[name.to_sym]
148
- end
149
-
150
- #TODO merge to get_field_by_tag
151
- def get_ext_field_by_tag(tag)
152
- extension_fields[tag]
153
- end
154
-
155
- #TODO merge to get_field
156
- def get_ext_field(tag_or_name)
157
- case tag_or_name
158
- when Integer then get_ext_field_by_tag(tag_or_name)
159
- when String, Symbol then get_ext_field_by_name(tag_or_name)
160
- else raise TypeError, tag_or_name.class
161
- end
162
- end
163
-
164
- def initialize_unready_fields
165
- unless @unready_initialized
166
- initialize_type_fields
167
- initialize_type_extension_fields
168
- @unready_initialized = true
169
- end
170
- end
171
-
172
- def initialize_type_fields
173
- fields.each do |tag, field|
174
- unless field.ready?
175
- field = field.setup
176
- fields[tag] = field
177
- fields_by_name[field.name.to_sym] = field
178
- fields_by_name[field.name] = field
179
- end
180
- end
181
- end
182
-
183
- def initialize_type_extension_fields
184
- extension_fields.each do |tag, field|
185
- unless field.ready?
186
- field = field.setup
187
- extension_fields[tag] = field
188
- extension_fields_by_name[field.name.to_sym] = field
189
- extension_fields_by_name[field.name] = field
190
- end
191
- end
192
- end
193
-
194
- def setup_repeated_field_arrays
195
- unless @repeated_fields_setup
196
- all_fields.each do |field|
197
- next unless field.repeated?
198
-
199
- if field.extension?
200
- repeated_extension_fields << field
201
- else
202
- repeated_fields << field
203
- end
204
- end
205
-
206
- @repeated_fields_setup = true
207
- end
208
- end
209
- end
210
-
211
- def initialize(values={})
212
- @values = {}
213
-
214
- self.class.initialize_unready_fields
215
- self.class.setup_repeated_field_arrays
216
-
217
- self.class.repeated_fields.each do |field|
218
- @values[field.name] = Field::FieldArray.new(field)
219
- end
220
-
221
- self.class.repeated_extension_fields.each do |field|
222
- @values[field.name] = Field::FieldArray.new(field)
223
- end
224
-
225
- values.each { |tag, val| self[tag] = val}
226
- end
227
-
228
- def initialized?
229
- fields.all? {|tag, field| field.initialized?(self) } && \
230
- extension_fields.all? {|tag, field| field.initialized?(self) }
231
- end
232
-
233
- def has_field?(tag_or_name)
234
- field = get_field(tag_or_name) || get_ext_field(tag_or_name)
235
- raise ArgumentError, "unknown field: #{tag_or_name.inspect}" unless field
236
- @values.has_key?(field.name)
237
- end
238
-
239
- def ==(obj)
240
- return false unless obj.is_a?(self.class)
241
- each_field do |field, value|
242
- return false unless value == obj.__send__(field.name)
243
- end
244
- true
245
- end
246
-
247
- def clear!
248
- @values.delete_if do |_, value|
249
- if value.is_a?(Field::FieldArray)
250
- value.clear
251
- false
252
- else
253
- true
254
- end
255
- end
256
- self
257
- end
258
-
259
- def dup
260
- copy_to(super, :dup)
261
- end
262
-
263
- def clone
264
- copy_to(super, :clone)
265
- end
266
-
267
- def copy_to(object, method)
268
- duplicate = proc {|obj|
269
- case obj
270
- when Message, String then obj.__send__(method)
271
- else obj
272
- end
273
- }
274
-
275
- object.__send__(:initialize)
276
- @values.each do |name, value|
277
- if value.is_a?(Field::FieldArray)
278
- object.__send__(name).replace(value.map {|v| duplicate.call(v)})
279
- else
280
- object.__send__("#{name}=", duplicate.call(value))
281
- end
282
- end
283
- object
284
- end
285
- private :copy_to
286
-
287
- def inspect(indent=0)
288
- result = []
289
- i = ' ' * indent
290
- field_value_to_string = lambda { |field, value|
291
- result << \
292
- if field.optional? && ! has_field?(field.name)
293
- ''
294
- else
295
- case field
296
- when Field::MessageField then
297
- if value.nil?
298
- "#{i}#{field.name} {}\n"
299
- else
300
- "#{i}#{field.name} {\n#{value.inspect(indent + 1)}#{i}}\n"
301
- end
302
- when Field::EnumField then
303
- if value.is_a?(EnumValue)
304
- "#{i}#{field.name}: #{value.name}\n"
305
- else
306
- "#{i}#{field.name}: #{field.type.name_by_value(value)}\n"
307
- end
308
- else
309
- "#{i}#{field.name}: #{value.inspect}\n"
310
- end
311
- end
312
- }
313
- each_field do |field, value|
314
- if field.repeated?
315
- value.each do |v|
316
- field_value_to_string.call(field, v)
317
- end
318
- else
319
- field_value_to_string.call(field, value)
320
- end
321
- end
322
- result.join
323
- end
324
-
325
- def parse_from_string(string)
326
- parse_from(StringIO.new(string))
327
- end
328
-
329
- def parse_from_file(filename)
330
- if filename.is_a?(File)
331
- parse_from(filename)
332
- else
333
- File.open(filename, 'rb') do |f|
334
- parse_from(f)
335
- end
336
- end
337
- end
338
-
339
- def parse_from(stream)
340
- Decoder.decode(stream, self)
341
- end
342
-
343
- def serialize_to_string(string='')
344
- io = StringIO.new(string)
345
- serialize_to(io)
346
- result = io.string
347
- result.force_encoding('ASCII-8BIT') if result.respond_to?(:force_encoding)
348
- result
349
- end
350
- alias to_s serialize_to_string
351
-
352
- def serialize_to_file(filename)
353
- if filename.is_a?(File)
354
- serialize_to(filename)
355
- else
356
- File.open(filename, 'wb') do |f|
357
- serialize_to(f)
358
- end
359
- end
360
- end
361
-
362
- def serialize_to(stream)
363
- Encoder.encode(stream, self)
364
- end
365
-
366
- def set_field(tag, bytes)
367
- field = (get_field_by_tag(tag) || get_ext_field_by_tag(tag))
368
- field.set(self, bytes) if field
369
- end
370
-
371
- def [](tag_or_name)
372
- if field = get_field(tag_or_name) || get_ext_field(tag_or_name)
373
- __send__(field.name)
374
- else
375
- raise NoMethodError, "No such field: #{tag_or_name.inspect}"
376
- end
377
- end
378
-
379
- def []=(tag_or_name, value)
380
- if field = get_field(tag_or_name) || get_ext_field(tag_or_name)
381
- __send__("#{field.name}=", value)
382
- else
383
- raise NoMethodError, "No such field: #{tag_or_name.inspect}"
384
- end
385
- end
386
-
387
- # Returns a hash; which key is a tag number, and value is a field object.
388
- def all_fields
389
- @_all_fields ||= self.class.all_fields
390
- end
391
-
392
- def fields
393
- @_fields ||= self.class.fields
394
- end
395
-
396
- # Returns field object or +nil+.
397
- def get_field_by_name(name)
398
- self.class.get_field_by_name(name)
399
- end
400
-
401
- # Returns field object or +nil+.
402
- def get_field_by_tag(tag)
403
- self.class.get_field_by_tag(tag)
404
- end
405
-
406
- # Returns field object or +nil+.
407
- def get_field(tag_or_name)
408
- self.class.get_field(tag_or_name)
409
- end
410
-
411
- # Returns extension fields. See Message#fields method.
412
- def extension_fields
413
- self.class.extension_fields
414
- end
415
-
416
- def get_ext_field_by_name(name) # :nodoc:
417
- self.class.get_ext_field_by_name(name)
418
- end
419
-
420
- def get_ext_field_by_tag(tag) # :nodoc:
421
- self.class.get_ext_field_by_tag(tag)
422
- end
423
-
424
- def get_ext_field(tag_or_name) # :nodoc:
425
- self.class.get_ext_field(tag_or_name)
426
- end
427
-
428
- # Iterate over a field collection.
429
- # message.each_field do |field_object, value|
430
- # # do something
431
- # end
432
- def each_field
433
- all_fields.each do |field|
434
- value = __send__(field.name)
435
- yield(field, value)
436
- end
437
- end
438
-
439
- def to_hash
440
- result = {}
441
- build_value = lambda { |field, value|
442
- if !field.optional? || (field.optional? && has_field?(field.name))
443
- case field
444
- when Field::MessageField then
445
- value.to_hash
446
- when Field::EnumField then
447
- if value.is_a?(EnumValue)
448
- value.to_i
449
- elsif value.is_a?(Symbol)
450
- field.type[value].to_i
451
- else
452
- value
453
- end
454
- else
455
- value
456
- end
457
- end
458
- }
459
- each_field do |field, value|
460
- if field.repeated?
461
- result[field.name] = value.map do |v|
462
- build_value.call(field, v)
463
- end
464
- else
465
- result[field.name] = build_value.call(field, value)
466
- end
467
- end
468
- result
469
- end
470
-
471
- def to_json
472
- to_hash.to_json
473
- end
474
- end
475
- end
1
+ require 'protobuf/message' # Need to get rid of this