Class: Protobuf::Message
- Inherits:
-
Object
show all
- Extended by:
- Fields
- Includes:
- Serialization
- Defined in:
- lib/protobuf/message.rb,
lib/protobuf/message/fields.rb,
lib/protobuf/message/serialization.rb
Direct Known Subclasses
Google::Protobuf::Compiler::CodeGeneratorRequest, Google::Protobuf::Compiler::CodeGeneratorResponse, Google::Protobuf::Compiler::CodeGeneratorResponse::File, Google::Protobuf::Compiler::Version, Google::Protobuf::DescriptorProto, Google::Protobuf::DescriptorProto::ExtensionRange, Google::Protobuf::DescriptorProto::ReservedRange, Google::Protobuf::EnumDescriptorProto, Google::Protobuf::EnumDescriptorProto::EnumReservedRange, Google::Protobuf::EnumOptions, Google::Protobuf::EnumValueDescriptorProto, Google::Protobuf::EnumValueOptions, Google::Protobuf::ExtensionRangeOptions, Google::Protobuf::FieldDescriptorProto, Google::Protobuf::FieldOptions, Google::Protobuf::FileDescriptorProto, Google::Protobuf::FileDescriptorSet, Google::Protobuf::FileOptions, Google::Protobuf::GeneratedCodeInfo, Google::Protobuf::GeneratedCodeInfo::Annotation, Google::Protobuf::MessageOptions, Google::Protobuf::MethodDescriptorProto, Google::Protobuf::MethodOptions, Google::Protobuf::OneofDescriptorProto, Google::Protobuf::OneofOptions, Google::Protobuf::ServiceDescriptorProto, Google::Protobuf::ServiceOptions, Google::Protobuf::SourceCodeInfo, Google::Protobuf::SourceCodeInfo::Location, Google::Protobuf::UninterpretedOption, Google::Protobuf::UninterpretedOption::NamePart, Rpc::DynamicDiscovery::Beacon, Rpc::DynamicDiscovery::Server, Socketrpc::Header, Socketrpc::Request, Socketrpc::Response
Defined Under Namespace
Modules: Fields, Serialization
Constant Summary
Constants included
from Fields
Fields::ACCESSOR_SUFFIXES
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#[](name) ⇒ Object
-
#[]=(name, value) ⇒ Object
-
#clear! ⇒ Object
-
#clone ⇒ Object
-
#dup ⇒ Object
-
#each_field ⇒ Object
Iterate over every field, invoking the given block.
-
#each_field_for_serialization ⇒ Object
-
#field?(name) ⇒ Boolean
(also: #respond_to_has?)
-
#initialize(fields = {}) {|_self| ... } ⇒ Message
constructor
-
#inspect ⇒ Object
-
#respond_to_has_and_present?(key) ⇒ Boolean
(also: #respond_to_has_present?, #respond_to_and_has_present?, #respond_to_and_has_and_present?, #responds_to_has_present?, #responds_to_and_has_present?, #responds_to_and_has_and_present?)
-
#set_field(name, value, ignore_nil_for_repeated, field = nil) ⇒ Object
-
#to_hash ⇒ Object
(also: #to_hash_value, #to_proto_hash)
Return a hash-representation of the given fields for this message type.
-
#to_hash_with_string_keys ⇒ Object
-
#to_json(options = {}) ⇒ Object
-
#to_json_hash ⇒ Object
(also: #to_json_hash_value)
Return a hash-representation of the given fields for this message type that is safe to convert to JSON.
-
#to_proto ⇒ Object
#decode, #decode_from, #encode, #encode_to
Constructor Details
#initialize(fields = {}) {|_self| ... } ⇒ Message
28
29
30
31
32
33
34
35
|
# File 'lib/protobuf/message.rb', line 28
def initialize(fields = {})
@values = {}
fields.to_hash.each do |name, value|
set_field(name, value, true)
end
yield self if block_given?
end
|
Class Method Details
.to_json ⇒ Object
20
21
22
|
# File 'lib/protobuf/message.rb', line 20
def self.to_json
name
end
|
Instance Method Details
#==(other) ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/protobuf/message.rb', line 169
def ==(other)
return false unless other.is_a?(self.class)
each_field do |field, value|
return false unless value == other[field.name]
end
true
end
|
#[](name) ⇒ Object
177
178
179
180
181
182
183
|
# File 'lib/protobuf/message.rb', line 177
def [](name)
field = _protobuf_message_field[name]
field.value_from_values(@values)
rescue raise if field
fail ArgumentError, "invalid field name=#{name.inspect}"
end
|
#[]=(name, value) ⇒ Object
185
186
187
|
# File 'lib/protobuf/message.rb', line 185
def []=(name, value)
set_field(name, value, true)
end
|
#clear! ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/protobuf/message.rb', line 41
def clear!
@values.delete_if do |_, value|
if value.is_a?(::Protobuf::Field::FieldArray) || value.is_a?(::Protobuf::Field::FieldHash)
value.clear
false
else
true
end
end
self
end
|
#clone ⇒ Object
53
54
55
|
# File 'lib/protobuf/message.rb', line 53
def clone
copy_to(super, :clone)
end
|
#dup ⇒ Object
57
58
59
|
# File 'lib/protobuf/message.rb', line 57
def dup
copy_to(super, :dup)
end
|
#each_field ⇒ Object
Iterate over every field, invoking the given block
63
64
65
66
67
68
69
70
|
# File 'lib/protobuf/message.rb', line 63
def each_field
return to_enum(:each_field) unless block_given?
self.class.all_fields.each do |field|
value = self[field.name]
yield(field, value)
end
end
|
#each_field_for_serialization ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/protobuf/message.rb', line 72
def each_field_for_serialization
_protobuf_message_unset_required_field_tags.each do |tag|
fail ::Protobuf::SerializationError, "Required field #{self.class.name}##{_protobuf_message_field[tag].name} does not have a value."
end
@values.each_key do |fully_qualified_name|
field = _protobuf_message_field[fully_qualified_name]
yield(field, field.value_from_values_for_serialization(@values))
end
end
|
#field?(name) ⇒ Boolean
Also known as:
respond_to_has?
83
84
85
86
87
88
89
90
91
|
# File 'lib/protobuf/message.rb', line 83
def field?(name)
field = _protobuf_message_field[name]
if field
field.field?(@values)
else
false
end
end
|
#inspect ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/protobuf/message.rb', line 95
def inspect
attrs = self.class.fields.map do |field|
[field.name, self[field.name].inspect].join('=')
end.join(' ')
"#<#{self.class} #{attrs}>"
end
|
#respond_to_has_and_present?(key) ⇒ Boolean
Also known as:
respond_to_has_present?, respond_to_and_has_present?, respond_to_and_has_and_present?, responds_to_has_present?, responds_to_and_has_present?, responds_to_and_has_and_present?
103
104
105
106
107
108
109
110
111
|
# File 'lib/protobuf/message.rb', line 103
def respond_to_has_and_present?(key)
field = _protobuf_message_field[key]
if field
field.field_and_present?(@values)
else
false
end
end
|
#set_field(name, value, ignore_nil_for_repeated, field = nil) ⇒ Object
189
190
191
192
193
194
195
196
197
|
# File 'lib/protobuf/message.rb', line 189
def set_field(name, value, ignore_nil_for_repeated, field = nil)
field ||= _protobuf_message_field[name]
if field
field.set_field(@values, value, ignore_nil_for_repeated, self)
else
fail(::Protobuf::FieldNotDefinedError, name) unless ::Protobuf.ignore_unknown_fields?
end
end
|
#to_hash ⇒ Object
Also known as:
to_hash_value, to_proto_hash
Return a hash-representation of the given fields for this message type.
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/protobuf/message.rb', line 114
def to_hash
result = {}
@values.each_key do |field_name|
field = _protobuf_message_field[field_name]
field.to_message_hash(@values, result)
end
result
end
|
#to_hash_with_string_keys ⇒ Object
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/protobuf/message.rb', line 125
def to_hash_with_string_keys
result = {}
@values.each_key do |field_name|
field = _protobuf_message_field[field_name]
field.to_message_hash_with_string_key(@values, result)
end
result
end
|
#to_json(options = {}) ⇒ Object
136
137
138
|
# File 'lib/protobuf/message.rb', line 136
def to_json(options = {})
to_json_hash.to_json(options)
end
|
#to_json_hash ⇒ Object
Also known as:
to_json_hash_value
Return a hash-representation of the given fields for this message type that
is safe to convert to JSON.
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/protobuf/message.rb', line 142
def to_json_hash
result = {}
@values.each_key do |field_name|
value = self[field_name]
field = self.class.get_field(field_name, true)
hashed_value = if value.respond_to?(:to_json_hash_value)
value.to_json_hash_value
elsif field.respond_to?(:json_encode)
field.json_encode(value)
else
value
end
result[field.name] = hashed_value
end
result
end
|
#to_proto ⇒ Object
165
166
167
|
# File 'lib/protobuf/message.rb', line 165
def to_proto
self
end
|