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::DescriptorProto, Google::Protobuf::DescriptorProto::ExtensionRange, Google::Protobuf::DescriptorProto::ReservedRange, Google::Protobuf::EnumDescriptorProto, Google::Protobuf::EnumOptions, Google::Protobuf::EnumValueDescriptorProto, Google::Protobuf::EnumValueOptions, Google::Protobuf::FieldDescriptorProto, Google::Protobuf::FieldOptions, Google::Protobuf::FileDescriptorProto, Google::Protobuf::FileDescriptorSet, Google::Protobuf::FileOptions, Google::Protobuf::MessageOptions, Google::Protobuf::MethodDescriptorProto, Google::Protobuf::MethodOptions, Google::Protobuf::OneofDescriptorProto, 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::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
-
#initialize(fields = {}) {|_self| ... } ⇒ Message
constructor
-
#inspect ⇒ Object
-
#respond_to_has?(key) ⇒ Boolean
(also: #responds_to_has?, #respond_to_and_has?, #responds_to_and_has?)
-
#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?)
-
#to_hash ⇒ Object
(also: #to_hash_value, #to_proto_hash)
Return a hash-representation of the given fields for this message type.
-
#to_json(options = {}) ⇒ Object
-
#to_proto ⇒ Object
#decode, #decode_from, #encode, #encode_to
Constructor Details
#initialize(fields = {}) {|_self| ... } ⇒ Message
38
39
40
41
42
43
44
45
|
# File 'lib/protobuf/message.rb', line 38
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
30
31
32
|
# File 'lib/protobuf/message.rb', line 30
def self.to_json
name
end
|
Instance Method Details
#==(other) ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/protobuf/message.rb', line 144
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
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/protobuf/message.rb', line 152
def [](name)
field = self.class.get_field(name, true)
fail ArgumentError, "invalid field name=#{name.inspect}" unless field
if field.repeated?
@values[field.fully_qualified_name] ||= ::Protobuf::Field::FieldArray.new(field)
elsif @values.key?(field.fully_qualified_name)
@values[field.fully_qualified_name]
else
field.default_value
end
end
|
#[]=(name, value) ⇒ Object
166
167
168
|
# File 'lib/protobuf/message.rb', line 166
def []=(name, value)
set_field(name, value, true)
end
|
#clear! ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/protobuf/message.rb', line 51
def clear!
@values.delete_if do |_, value|
if value.is_a?(::Protobuf::Field::FieldArray)
value.clear
false
else
true
end
end
self
end
|
#clone ⇒ Object
63
64
65
|
# File 'lib/protobuf/message.rb', line 63
def clone
copy_to(super, :clone)
end
|
#dup ⇒ Object
67
68
69
|
# File 'lib/protobuf/message.rb', line 67
def dup
copy_to(super, :dup)
end
|
#each_field ⇒ Object
Iterate over every field, invoking the given block
73
74
75
76
77
78
79
80
|
# File 'lib/protobuf/message.rb', line 73
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
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/protobuf/message.rb', line 82
def each_field_for_serialization
self.class.all_fields.each do |field|
value = @values[field.fully_qualified_name]
if value.nil?
fail ::Protobuf::SerializationError, "Required field #{self.class.name}##{field.name} does not have a value." if field.required?
next
end
yield(field, value)
end
end
|
#field?(name) ⇒ Boolean
94
95
96
97
98
99
100
101
102
|
# File 'lib/protobuf/message.rb', line 94
def field?(name)
field = self.class.get_field(name, true)
return false if field.nil?
if field.repeated?
@values.key?(field.fully_qualified_name) && @values[field.fully_qualified_name].present?
else
@values.key?(field.fully_qualified_name)
end
end
|
#inspect ⇒ Object
105
106
107
108
109
110
111
|
# File 'lib/protobuf/message.rb', line 105
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?(key) ⇒ Boolean
Also known as:
responds_to_has?, respond_to_and_has?, responds_to_and_has?
113
114
115
|
# File 'lib/protobuf/message.rb', line 113
def respond_to_has?(key)
respond_to?(key) && field?(key)
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?
117
118
119
120
|
# File 'lib/protobuf/message.rb', line 117
def respond_to_has_and_present?(key)
respond_to_has?(key) &&
(self[key].present? || [true, false].include?(self[key]))
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.
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/protobuf/message.rb', line 123
def to_hash
result = {}
@values.each_key do |field_name|
value = self[field_name]
field = self.class.get_field(field_name, true)
hashed_value = value.respond_to?(:to_hash_value) ? value.to_hash_value : value
result[field.name] = hashed_value
end
result
end
|
#to_json(options = {}) ⇒ Object
136
137
138
|
# File 'lib/protobuf/message.rb', line 136
def to_json(options = {})
to_hash.to_json(options)
end
|
#to_proto ⇒ Object
140
141
142
|
# File 'lib/protobuf/message.rb', line 140
def to_proto
self
end
|