protobuf-core 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +18 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +23 -0
  8. data/Rakefile +5 -0
  9. data/bin/protoc-gen-ruby +16 -0
  10. data/lib/protobuf.rb +27 -0
  11. data/lib/protobuf/code_generator.rb +44 -0
  12. data/lib/protobuf/core.rb +2 -0
  13. data/lib/protobuf/core/version.rb +5 -0
  14. data/lib/protobuf/decoder.rb +73 -0
  15. data/lib/protobuf/deprecation.rb +112 -0
  16. data/lib/protobuf/descriptors.rb +3 -0
  17. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +54 -0
  18. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +251 -0
  19. data/lib/protobuf/encoder.rb +67 -0
  20. data/lib/protobuf/enum.rb +303 -0
  21. data/lib/protobuf/exceptions.rb +9 -0
  22. data/lib/protobuf/field.rb +74 -0
  23. data/lib/protobuf/field/base_field.rb +267 -0
  24. data/lib/protobuf/field/bool_field.rb +59 -0
  25. data/lib/protobuf/field/bytes_field.rb +82 -0
  26. data/lib/protobuf/field/double_field.rb +25 -0
  27. data/lib/protobuf/field/enum_field.rb +68 -0
  28. data/lib/protobuf/field/field_array.rb +87 -0
  29. data/lib/protobuf/field/fixed32_field.rb +25 -0
  30. data/lib/protobuf/field/fixed64_field.rb +28 -0
  31. data/lib/protobuf/field/float_field.rb +41 -0
  32. data/lib/protobuf/field/int32_field.rb +21 -0
  33. data/lib/protobuf/field/int64_field.rb +21 -0
  34. data/lib/protobuf/field/integer_field.rb +23 -0
  35. data/lib/protobuf/field/message_field.rb +65 -0
  36. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  37. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  38. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  39. data/lib/protobuf/field/sint32_field.rb +21 -0
  40. data/lib/protobuf/field/sint64_field.rb +21 -0
  41. data/lib/protobuf/field/string_field.rb +34 -0
  42. data/lib/protobuf/field/uint32_field.rb +21 -0
  43. data/lib/protobuf/field/uint64_field.rb +21 -0
  44. data/lib/protobuf/field/varint_field.rb +73 -0
  45. data/lib/protobuf/generators/base.rb +70 -0
  46. data/lib/protobuf/generators/enum_generator.rb +41 -0
  47. data/lib/protobuf/generators/extension_generator.rb +27 -0
  48. data/lib/protobuf/generators/field_generator.rb +131 -0
  49. data/lib/protobuf/generators/file_generator.rb +132 -0
  50. data/lib/protobuf/generators/group_generator.rb +105 -0
  51. data/lib/protobuf/generators/message_generator.rb +98 -0
  52. data/lib/protobuf/generators/printable.rb +160 -0
  53. data/lib/protobuf/logging.rb +39 -0
  54. data/lib/protobuf/message.rb +193 -0
  55. data/lib/protobuf/message/fields.rb +133 -0
  56. data/lib/protobuf/message/serialization.rb +89 -0
  57. data/lib/protobuf/optionable.rb +23 -0
  58. data/lib/protobuf/wire_type.rb +10 -0
  59. data/proto/dynamic_discovery.proto +44 -0
  60. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  61. data/proto/google/protobuf/descriptor.proto +620 -0
  62. data/proto/rpc.proto +62 -0
  63. data/protobuf-core.gemspec +31 -0
  64. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  65. data/spec/data/data.bin +3 -0
  66. data/spec/data/types.bin +0 -0
  67. data/spec/encoding/all_types_spec.rb +105 -0
  68. data/spec/encoding/extreme_values_spec.rb +0 -0
  69. data/spec/functional/class_inheritance_spec.rb +52 -0
  70. data/spec/functional/compile_and_require_spec.rb +29 -0
  71. data/spec/lib/protobuf/base_spec.rb +84 -0
  72. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  73. data/spec/lib/protobuf/enum_generator_spec.rb +73 -0
  74. data/spec/lib/protobuf/enum_spec.rb +265 -0
  75. data/spec/lib/protobuf/extension_generator_spec.rb +42 -0
  76. data/spec/lib/protobuf/field/bool_field_spec.rb +51 -0
  77. data/spec/lib/protobuf/field/field_array_spec.rb +69 -0
  78. data/spec/lib/protobuf/field/float_field_spec.rb +55 -0
  79. data/spec/lib/protobuf/field/int32_field_spec.rb +90 -0
  80. data/spec/lib/protobuf/field/string_field_spec.rb +45 -0
  81. data/spec/lib/protobuf/field_generator_spec.rb +102 -0
  82. data/spec/lib/protobuf/field_spec.rb +191 -0
  83. data/spec/lib/protobuf/file_generator_spec.rb +32 -0
  84. data/spec/lib/protobuf/message_generator_spec.rb +0 -0
  85. data/spec/lib/protobuf/message_spec.rb +526 -0
  86. data/spec/lib/protobuf/optionable_spec.rb +46 -0
  87. data/spec/lib/protobuf_spec.rb +45 -0
  88. data/spec/spec_helper.rb +9 -0
  89. data/spec/support/packed_field.rb +22 -0
  90. data/spec/support/test/all_types.data.bin +0 -0
  91. data/spec/support/test/all_types.data.txt +119 -0
  92. data/spec/support/test/bacon.proto +14 -0
  93. data/spec/support/test/defaults.pb.rb +27 -0
  94. data/spec/support/test/defaults.proto +9 -0
  95. data/spec/support/test/enum.pb.rb +61 -0
  96. data/spec/support/test/enum.proto +34 -0
  97. data/spec/support/test/extended.pb.rb +24 -0
  98. data/spec/support/test/extended.proto +10 -0
  99. data/spec/support/test/extreme_values.data.bin +0 -0
  100. data/spec/support/test/google_unittest.pb.rb +530 -0
  101. data/spec/support/test/google_unittest.proto +713 -0
  102. data/spec/support/test/google_unittest_import.pb.rb +39 -0
  103. data/spec/support/test/google_unittest_import.proto +64 -0
  104. data/spec/support/test/google_unittest_import_public.pb.rb +10 -0
  105. data/spec/support/test/google_unittest_import_public.proto +38 -0
  106. data/spec/support/test/multi_field_extensions.pb.rb +58 -0
  107. data/spec/support/test/multi_field_extensions.proto +33 -0
  108. data/spec/support/test/resource.pb.rb +106 -0
  109. data/spec/support/test/resource.proto +94 -0
  110. metadata +306 -0
@@ -0,0 +1,251 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # This file is auto-generated. DO NOT EDIT!
5
+ #
6
+ require 'protobuf/message'
7
+
8
+ module Google
9
+ module Protobuf
10
+
11
+ ##
12
+ # Message Classes
13
+ #
14
+ class FileDescriptorSet < ::Protobuf::Message; end
15
+ class FileDescriptorProto < ::Protobuf::Message; end
16
+ class DescriptorProto < ::Protobuf::Message
17
+ class ExtensionRange < ::Protobuf::Message; end
18
+
19
+ end
20
+
21
+ class FieldDescriptorProto < ::Protobuf::Message
22
+ class Type < ::Protobuf::Enum
23
+ define :TYPE_DOUBLE, 1
24
+ define :TYPE_FLOAT, 2
25
+ define :TYPE_INT64, 3
26
+ define :TYPE_UINT64, 4
27
+ define :TYPE_INT32, 5
28
+ define :TYPE_FIXED64, 6
29
+ define :TYPE_FIXED32, 7
30
+ define :TYPE_BOOL, 8
31
+ define :TYPE_STRING, 9
32
+ define :TYPE_GROUP, 10
33
+ define :TYPE_MESSAGE, 11
34
+ define :TYPE_BYTES, 12
35
+ define :TYPE_UINT32, 13
36
+ define :TYPE_ENUM, 14
37
+ define :TYPE_SFIXED32, 15
38
+ define :TYPE_SFIXED64, 16
39
+ define :TYPE_SINT32, 17
40
+ define :TYPE_SINT64, 18
41
+ end
42
+
43
+ class Label < ::Protobuf::Enum
44
+ define :LABEL_OPTIONAL, 1
45
+ define :LABEL_REQUIRED, 2
46
+ define :LABEL_REPEATED, 3
47
+ end
48
+
49
+ end
50
+
51
+ class EnumDescriptorProto < ::Protobuf::Message; end
52
+ class EnumValueDescriptorProto < ::Protobuf::Message; end
53
+ class ServiceDescriptorProto < ::Protobuf::Message; end
54
+ class MethodDescriptorProto < ::Protobuf::Message; end
55
+ class FileOptions < ::Protobuf::Message
56
+ class OptimizeMode < ::Protobuf::Enum
57
+ define :SPEED, 1
58
+ define :CODE_SIZE, 2
59
+ define :LITE_RUNTIME, 3
60
+ end
61
+
62
+ end
63
+
64
+ class MessageOptions < ::Protobuf::Message; end
65
+ class FieldOptions < ::Protobuf::Message
66
+ class CType < ::Protobuf::Enum
67
+ define :STRING, 0
68
+ define :CORD, 1
69
+ define :STRING_PIECE, 2
70
+ end
71
+
72
+ end
73
+
74
+ class EnumOptions < ::Protobuf::Message; end
75
+ class EnumValueOptions < ::Protobuf::Message; end
76
+ class ServiceOptions < ::Protobuf::Message; end
77
+ class MethodOptions < ::Protobuf::Message; end
78
+ class UninterpretedOption < ::Protobuf::Message
79
+ class NamePart < ::Protobuf::Message; end
80
+
81
+ end
82
+
83
+ class SourceCodeInfo < ::Protobuf::Message
84
+ class Location < ::Protobuf::Message; end
85
+
86
+ end
87
+
88
+
89
+
90
+ ##
91
+ # Message Fields
92
+ #
93
+ class FileDescriptorSet
94
+ repeated ::Google::Protobuf::FileDescriptorProto, :file, 1
95
+ end
96
+
97
+ class FileDescriptorProto
98
+ optional :string, :name, 1
99
+ optional :string, :package, 2
100
+ repeated :string, :dependency, 3
101
+ repeated :int32, :public_dependency, 10
102
+ repeated :int32, :weak_dependency, 11
103
+ repeated ::Google::Protobuf::DescriptorProto, :message_type, 4
104
+ repeated ::Google::Protobuf::EnumDescriptorProto, :enum_type, 5
105
+ repeated ::Google::Protobuf::ServiceDescriptorProto, :service, 6
106
+ repeated ::Google::Protobuf::FieldDescriptorProto, :extension, 7
107
+ optional ::Google::Protobuf::FileOptions, :options, 8
108
+ optional ::Google::Protobuf::SourceCodeInfo, :source_code_info, 9
109
+ end
110
+
111
+ class DescriptorProto
112
+ class ExtensionRange
113
+ optional :int32, :start, 1
114
+ optional :int32, :end, 2
115
+ end
116
+
117
+ optional :string, :name, 1
118
+ repeated ::Google::Protobuf::FieldDescriptorProto, :field, 2
119
+ repeated ::Google::Protobuf::FieldDescriptorProto, :extension, 6
120
+ repeated ::Google::Protobuf::DescriptorProto, :nested_type, 3
121
+ repeated ::Google::Protobuf::EnumDescriptorProto, :enum_type, 4
122
+ repeated ::Google::Protobuf::DescriptorProto::ExtensionRange, :extension_range, 5
123
+ optional ::Google::Protobuf::MessageOptions, :options, 7
124
+ end
125
+
126
+ class FieldDescriptorProto
127
+ optional :string, :name, 1
128
+ optional :int32, :number, 3
129
+ optional ::Google::Protobuf::FieldDescriptorProto::Label, :label, 4
130
+ optional ::Google::Protobuf::FieldDescriptorProto::Type, :type, 5
131
+ optional :string, :type_name, 6
132
+ optional :string, :extendee, 2
133
+ optional :string, :default_value, 7
134
+ optional ::Google::Protobuf::FieldOptions, :options, 8
135
+ end
136
+
137
+ class EnumDescriptorProto
138
+ optional :string, :name, 1
139
+ repeated ::Google::Protobuf::EnumValueDescriptorProto, :value, 2
140
+ optional ::Google::Protobuf::EnumOptions, :options, 3
141
+ end
142
+
143
+ class EnumValueDescriptorProto
144
+ optional :string, :name, 1
145
+ optional :int32, :number, 2
146
+ optional ::Google::Protobuf::EnumValueOptions, :options, 3
147
+ end
148
+
149
+ class ServiceDescriptorProto
150
+ optional :string, :name, 1
151
+ repeated ::Google::Protobuf::MethodDescriptorProto, :method, 2
152
+ optional ::Google::Protobuf::ServiceOptions, :options, 3
153
+ end
154
+
155
+ class MethodDescriptorProto
156
+ optional :string, :name, 1
157
+ optional :string, :input_type, 2
158
+ optional :string, :output_type, 3
159
+ optional ::Google::Protobuf::MethodOptions, :options, 4
160
+ end
161
+
162
+ class FileOptions
163
+ optional :string, :java_package, 1
164
+ optional :string, :java_outer_classname, 8
165
+ optional :bool, :java_multiple_files, 10, :default => false
166
+ optional :bool, :java_generate_equals_and_hash, 20, :default => false
167
+ optional ::Google::Protobuf::FileOptions::OptimizeMode, :optimize_for, 9, :default => ::Google::Protobuf::FileOptions::OptimizeMode::SPEED
168
+ optional :string, :go_package, 11
169
+ optional :bool, :cc_generic_services, 16, :default => false
170
+ optional :bool, :java_generic_services, 17, :default => false
171
+ optional :bool, :py_generic_services, 18, :default => false
172
+ repeated ::Google::Protobuf::UninterpretedOption, :uninterpreted_option, 999
173
+ # Extension Fields
174
+ extensions 1000...536870912
175
+ end
176
+
177
+ class MessageOptions
178
+ optional :bool, :message_set_wire_format, 1, :default => false
179
+ optional :bool, :no_standard_descriptor_accessor, 2, :default => false
180
+ repeated ::Google::Protobuf::UninterpretedOption, :uninterpreted_option, 999
181
+ # Extension Fields
182
+ extensions 1000...536870912
183
+ end
184
+
185
+ class FieldOptions
186
+ optional ::Google::Protobuf::FieldOptions::CType, :ctype, 1, :default => ::Google::Protobuf::FieldOptions::CType::STRING
187
+ optional :bool, :packed, 2
188
+ optional :bool, :lazy, 5, :default => false
189
+ optional :bool, :deprecated, 3, :default => false
190
+ optional :string, :experimental_map_key, 9
191
+ optional :bool, :weak, 10, :default => false
192
+ repeated ::Google::Protobuf::UninterpretedOption, :uninterpreted_option, 999
193
+ # Extension Fields
194
+ extensions 1000...536870912
195
+ end
196
+
197
+ class EnumOptions
198
+ optional :bool, :allow_alias, 2, :default => true
199
+ repeated ::Google::Protobuf::UninterpretedOption, :uninterpreted_option, 999
200
+ # Extension Fields
201
+ extensions 1000...536870912
202
+ end
203
+
204
+ class EnumValueOptions
205
+ repeated ::Google::Protobuf::UninterpretedOption, :uninterpreted_option, 999
206
+ # Extension Fields
207
+ extensions 1000...536870912
208
+ end
209
+
210
+ class ServiceOptions
211
+ repeated ::Google::Protobuf::UninterpretedOption, :uninterpreted_option, 999
212
+ # Extension Fields
213
+ extensions 1000...536870912
214
+ end
215
+
216
+ class MethodOptions
217
+ repeated ::Google::Protobuf::UninterpretedOption, :uninterpreted_option, 999
218
+ # Extension Fields
219
+ extensions 1000...536870912
220
+ end
221
+
222
+ class UninterpretedOption
223
+ class NamePart
224
+ required :string, :name_part, 1
225
+ required :bool, :is_extension, 2
226
+ end
227
+
228
+ repeated ::Google::Protobuf::UninterpretedOption::NamePart, :name, 2
229
+ optional :string, :identifier_value, 3
230
+ optional :uint64, :positive_int_value, 4
231
+ optional :int64, :negative_int_value, 5
232
+ optional :double, :double_value, 6
233
+ optional :bytes, :string_value, 7
234
+ optional :string, :aggregate_value, 8
235
+ end
236
+
237
+ class SourceCodeInfo
238
+ class Location
239
+ repeated :int32, :path, 1, :packed => true
240
+ repeated :int32, :span, 2, :packed => true
241
+ optional :string, :leading_comments, 3
242
+ optional :string, :trailing_comments, 4
243
+ end
244
+
245
+ repeated ::Google::Protobuf::SourceCodeInfo::Location, :location, 1
246
+ end
247
+
248
+ end
249
+
250
+ end
251
+
@@ -0,0 +1,67 @@
1
+ module Protobuf
2
+ class Encoder
3
+
4
+ def self.encode(stream, message)
5
+ new(stream, message).encode
6
+ end
7
+
8
+ private
9
+
10
+ attr_writer :message, :stream
11
+
12
+ public
13
+
14
+ attr_reader :message, :stream
15
+
16
+ def initialize(message, stream)
17
+ unless message.respond_to?(:each_field_for_serialization)
18
+ fail ArgumentError, "Message instance must respond to :each_field_for_serialization"
19
+ end
20
+
21
+ self.message = message
22
+ self.stream = stream
23
+ end
24
+
25
+ def encode
26
+ message.each_field_for_serialization do |field, value|
27
+ encode_field(field, value)
28
+ end
29
+
30
+ stream
31
+ end
32
+
33
+ private
34
+
35
+ def encode_field(field, value)
36
+ if field.repeated?
37
+ encode_repeated_field(field, value)
38
+ else
39
+ write_pair(field, value)
40
+ end
41
+ end
42
+
43
+ def encode_packed_field(field, value)
44
+ key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
45
+ packed_value = value.map { |val| field.encode(val) }.join
46
+ stream << ::Protobuf::Field::VarintField.encode(key)
47
+ stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
48
+ stream << packed_value
49
+ end
50
+
51
+ def encode_repeated_field(field, value)
52
+ if field.packed?
53
+ encode_packed_field(field, value)
54
+ else
55
+ value.each { |val| write_pair(field, val) }
56
+ end
57
+ end
58
+
59
+ # Encode key and value, and write to +stream+.
60
+ def write_pair(field, value)
61
+ key = (field.tag << 3) | field.wire_type
62
+ stream << ::Protobuf::Field::VarintField.encode(key)
63
+ stream << field.encode(value)
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,303 @@
1
+ require 'delegate'
2
+ require 'protobuf/optionable'
3
+
4
+ ##
5
+ # Adding extension to Numeric until
6
+ # we can get people to stop calling #value
7
+ # on Enum instances.
8
+ #
9
+ ::Protobuf.deprecator.define_deprecated_methods(Numeric, :value => :to_int)
10
+
11
+ module Protobuf
12
+ class Enum < SimpleDelegator
13
+ # Public: Allows setting Options on the Enum class.
14
+ include ::Protobuf::Optionable
15
+
16
+ def self.aliases_allowed?
17
+ get_option(:allow_alias)
18
+ end
19
+
20
+ # Public: Get all integer tags defined by this enum.
21
+ #
22
+ # Examples
23
+ #
24
+ # class StateMachine < ::Protobuf::Enum
25
+ # set_option :allow_alias
26
+ # define :ON, 1
27
+ # define :STARTED, 1
28
+ # define :OFF, 2
29
+ # end
30
+ #
31
+ # StateMachine.all_tags #=> [ 1, 2 ]
32
+ #
33
+ # Returns an array of unique integers.
34
+ #
35
+ def self.all_tags
36
+ @all_tags ||= enums.map(&:to_i).uniq
37
+ end
38
+
39
+ # Internal: DSL method to create a new Enum. The given name will
40
+ # become a constant for this Enum pointing to the new instance.
41
+ #
42
+ # Examples
43
+ #
44
+ # class StateMachine < ::Protobuf::Enum
45
+ # define :ON, 1
46
+ # define :OFF, 2
47
+ # end
48
+ #
49
+ # StateMachine::ON #=> #<StateMachine::ON=1>
50
+ # StateMachine::OFF #=> #<StateMachine::OFF=2>
51
+ #
52
+ # Returns nothing.
53
+ #
54
+ def self.define(name, tag)
55
+ enum = new(self, name, tag)
56
+ @enums ||= []
57
+ @enums << enum
58
+ const_set(name, enum)
59
+ end
60
+
61
+ # Public: All defined enums.
62
+ #
63
+ class << self
64
+ attr_reader :enums
65
+ end
66
+
67
+ # Public: Get an array of Enum objects with the given tag.
68
+ #
69
+ # tag - An object that responds to to_i.
70
+ #
71
+ # Examples
72
+ #
73
+ # class StateMachine < ::Protobuf::Enum
74
+ # set_option :allow_alias
75
+ # define :ON, 1
76
+ # define :STARTED, 1
77
+ # define :OFF, 2
78
+ # end
79
+ #
80
+ # StateMachine.enums_for_tag(1) #=> [ #<StateMachine::ON=1>, #<StateMachine::STARTED=1> ]
81
+ # StateMachine.enums_for_tag(2) #=> [ #<StateMachine::OFF=2> ]
82
+ #
83
+ # Returns an array with zero or more Enum objects or nil.
84
+ #
85
+ def self.enums_for_tag(tag)
86
+ enums.select do |enum|
87
+ enum.to_i == tag.to_i
88
+ end
89
+ end
90
+
91
+ # Public: Get the Enum associated with the given name.
92
+ #
93
+ # name - A string-like object. Case-sensitive.
94
+ #
95
+ # Example
96
+ #
97
+ # class StateMachine < ::Protobuf::Enum
98
+ # define :ON, 1
99
+ # define :OFF, 2
100
+ # end
101
+ #
102
+ # StateMachine.enum_for_name(:ON) # => #<StateMachine::ON=1>
103
+ # StateMachine.enum_for_name("ON") # => #<StateMachine::ON=1>
104
+ # StateMachine.enum_for_name(:on) # => nil
105
+ # StateMachine.enum_for_name(:FOO) # => nil
106
+ #
107
+ # Returns the Enum object with the given name or nil.
108
+ #
109
+ def self.enum_for_name(name)
110
+ const_get(name)
111
+ rescue ::NameError
112
+ nil
113
+ end
114
+
115
+ # Public: Get the Enum object corresponding to the given tag.
116
+ #
117
+ # tag - An object that responds to to_i.
118
+ #
119
+ # Returns an Enum object or nil. If the tag corresponds to multiple
120
+ # Enums, the first enum defined will be returned.
121
+ #
122
+ def self.enum_for_tag(tag)
123
+ enums_for_tag(tag).first
124
+ end
125
+
126
+ # Public: Get an Enum by a variety of type-checking mechanisms.
127
+ #
128
+ # candidate - An Enum, Numeric, String, or Symbol object.
129
+ #
130
+ # Example
131
+ #
132
+ # class StateMachine < ::Protobuf::Enum
133
+ # set_option :allow_alias
134
+ # define :ON, 1
135
+ # define :STARTED, 1
136
+ # define :OFF, 2
137
+ # end
138
+ #
139
+ # StateMachine.fetch(StateMachine::ON) # => #<StateMachine::ON=1>
140
+ # StateMachine.fetch(:ON) # => #<StateMachine::ON=1>
141
+ # StateMachine.fetch("STARTED") # => #<StateMachine::STARTED=1>
142
+ # StateMachine.fetch(1) # => [ #<StateMachine::ON=1>, #<StateMachine::STARTED=1> ]
143
+ #
144
+ # Returns an Enum object or nil.
145
+ #
146
+ def self.fetch(candidate)
147
+ case candidate
148
+ when self then
149
+ candidate
150
+ when ::Numeric then
151
+ enum_for_tag(candidate.to_i)
152
+ when ::String, ::Symbol then
153
+ enum_for_name(candidate)
154
+ else
155
+ nil
156
+ end
157
+ end
158
+
159
+ # Public: Get the Symbol name associated with the given number.
160
+ #
161
+ # number - An object that responds to to_i.
162
+ #
163
+ # Examples
164
+ #
165
+ # # Without aliases
166
+ # class StateMachine < ::Protobuf::Enum
167
+ # define :ON, 1
168
+ # define :OFF, 2
169
+ # end
170
+ #
171
+ # StateMachine.name_for_tag(1) # => :ON
172
+ # StateMachine.name_for_tag(2) # => :OFF
173
+ # StateMachine.name_for_tag(3) # => nil
174
+ #
175
+ # # With aliases
176
+ # class StateMachine < ::Protobuf::Enum
177
+ # set_option :allow_alias
178
+ # define :STARTED, 1
179
+ # define :ON, 1
180
+ # define :OFF, 2
181
+ # end
182
+ #
183
+ # StateMachine.name_for_tag(1) # => :STARTED
184
+ # StateMachine.name_for_tag(2) # => :OFF
185
+ #
186
+ # Returns the symbol name of the enum constant given it's associated tag or nil.
187
+ # If the given tag has multiple names associated (due to allow_alias)
188
+ # the first defined name will be returned.
189
+ #
190
+ def self.name_for_tag(tag)
191
+ enum_for_tag(tag).try(:name)
192
+ end
193
+
194
+ # Public: Check if the given tag is defined by this Enum.
195
+ #
196
+ # number - An object that responds to to_i.
197
+ #
198
+ # Returns a boolean.
199
+ #
200
+ def self.valid_tag?(tag)
201
+ tag.respond_to?(:to_i) && all_tags.include?(tag.to_i)
202
+ end
203
+
204
+ # Public: [DEPRECATED] Return a hash of Enum objects keyed
205
+ # by their :name.
206
+ #
207
+ def self.values
208
+ @values ||= enums.each_with_object({}) do |enum, hash|
209
+ hash[enum.name] = enum
210
+ end
211
+ end
212
+
213
+ ##
214
+ # Class Deprecations
215
+ #
216
+ class << self
217
+ ::Protobuf.deprecator.define_deprecated_methods(
218
+ self,
219
+ :enum_by_value => :enum_for_tag,
220
+ :name_by_value => :name_for_tag,
221
+ :get_name_by_tag => :name_for_tag,
222
+ :value_by_name => :enum_for_name,
223
+ )
224
+
225
+ ::Protobuf.deprecator.deprecate_methods(self, :values => :enums)
226
+ end
227
+
228
+ ##
229
+ # Attributes
230
+ #
231
+
232
+ private
233
+
234
+ attr_writer :parent_class, :name, :tag
235
+
236
+ public
237
+
238
+ attr_reader :parent_class, :name, :tag
239
+
240
+ ##
241
+ # Instance Methods
242
+ #
243
+
244
+ def initialize(parent_class, name, tag)
245
+ self.parent_class = parent_class
246
+ self.name = name
247
+ self.tag = tag
248
+ super(tag)
249
+ end
250
+
251
+ # Overriding the class so ActiveRecord/Arel visitor will visit the enum as a Fixnum
252
+ #
253
+ def class
254
+ Fixnum
255
+ end
256
+
257
+ def inspect
258
+ "\#<Protobuf::Enum(#{parent_class})::#{name}=#{tag}>"
259
+ end
260
+
261
+ def to_i
262
+ tag
263
+ end
264
+
265
+ def to_int
266
+ tag.to_int
267
+ end
268
+
269
+ def to_s(format = :tag)
270
+ case format
271
+ when :tag then
272
+ to_i.to_s
273
+ when :name then
274
+ name.to_s
275
+ else
276
+ to_i.to_s
277
+ end
278
+ end
279
+
280
+ # Re-implement `try` in order to fix the problem where
281
+ # the underlying fixnum doesn't respond to all methods (e.g. name or tag).
282
+ # If we respond to the first argument, `__send__` the args. Otherwise,
283
+ # delegate the `try` call to the underlying vlaue fixnum.
284
+ #
285
+ def try(*args, &block)
286
+ case
287
+ when args.empty? && block_given?
288
+ yield self
289
+ when respond_to?(args.first)
290
+ __send__(*args, &block)
291
+ else
292
+ @tag.try(*args, &block)
293
+ end
294
+ end
295
+
296
+ ::Protobuf.deprecator.define_deprecated_methods(self, :value => :to_i)
297
+
298
+ ##
299
+ # Instance Aliases
300
+ #
301
+ alias_method :to_hash_value, :to_i
302
+ end
303
+ end