Class: Protobuf::Message

Inherits:
Object
  • Object
show all
Extended by:
Fields
Includes:
Serialization
Defined in:
lib/protobuf/message.rb,
lib/protobuf/message/fields.rb,
lib/protobuf/message/serialization.rb

Defined Under Namespace

Modules: Fields, Serialization

Constant Summary

Constants included from Fields

Fields::ACCESSOR_SUFFIXES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialization

#decode, #decode_from, #encode, #encode_to

Constructor Details

#initialize(fields = {}) {|_self| ... } ⇒ Message

Constructor

Yields:

  • (_self)

Yield Parameters:



44
45
46
47
48
49
50
51
# File 'lib/protobuf/message.rb', line 44

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

.from_json(json) ⇒ Object



24
25
26
27
# File 'lib/protobuf/message.rb', line 24

def self.from_json(json)
  fields = normalize_json(JSON.parse(json))
  new(fields)
end

.normalize_json(ob) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/protobuf/message.rb', line 29

def self.normalize_json(ob)
  case ob
  when Array
    ob.map { |value| normalize_json(value) }
  when Hash
    Hash[*ob.flat_map { |key, value| [key.underscore, normalize_json(value)] }]
  else
    ob
  end
end

.to_jsonObject

Class Methods



20
21
22
# File 'lib/protobuf/message.rb', line 20

def self.to_json
  name
end

Instance Method Details

#==(other) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/protobuf/message.rb', line 192

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



200
201
202
203
204
205
206
# File 'lib/protobuf/message.rb', line 200

def [](name)
  field = _protobuf_message_field[name]
  field.value_from_values(@values)
rescue # not having a field should be the exceptional state
  raise if field
  fail ArgumentError, "invalid field name=#{name.inspect}"
end

#[]=(name, value) ⇒ Object



208
209
210
# File 'lib/protobuf/message.rb', line 208

def []=(name, value)
  set_field(name, value, true)
end

#clear!Object

Public Instance Methods



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/protobuf/message.rb', line 57

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

#cloneObject



69
70
71
# File 'lib/protobuf/message.rb', line 69

def clone
  copy_to(super, :clone)
end

#dupObject



73
74
75
# File 'lib/protobuf/message.rb', line 73

def dup
  copy_to(super, :dup)
end

#each_fieldObject

Iterate over every field, invoking the given block



79
80
81
82
83
84
85
86
# File 'lib/protobuf/message.rb', line 79

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_serializationObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/protobuf/message.rb', line 88

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?

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
# File 'lib/protobuf/message.rb', line 99

def field?(name)
  field = _protobuf_message_field[name]

  if field
    field.field?(@values)
  else
    false
  end
end

#inspectObject



111
112
113
114
115
116
117
# File 'lib/protobuf/message.rb', line 111

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?

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
# File 'lib/protobuf/message.rb', line 119

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



212
213
214
215
216
217
218
219
220
# File 'lib/protobuf/message.rb', line 212

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_hashObject Also known as: to_hash_value, to_proto_hash

Return a hash-representation of the given fields for this message type.



130
131
132
133
134
135
136
137
138
139
# File 'lib/protobuf/message.rb', line 130

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_keysObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/protobuf/message.rb', line 141

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



152
153
154
# File 'lib/protobuf/message.rb', line 152

def to_json(options = {})
  to_json_hash(options).to_json(options)
end

#to_json_hash(options = {}) ⇒ 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.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/protobuf/message.rb', line 158

def to_json_hash(options = {})
  result = {}

  proto3 = options[:proto3] || options[:lower_camel_case]

  @values.each_key do |field_name|
    value = self[field_name]
    field = self.class.get_field(field_name, true)

    # NB: to_json_hash_value should come before json_encode so as to handle
    # repeated fields without extra logic.
    hashed_value = if value.respond_to?(:to_json_hash_value) && !field.is_a?(::Protobuf::Field::EnumField)
                     value.to_json_hash_value(options)
                   elsif field.respond_to?(:json_encode)
                     field.json_encode(value, options)
                   else
                     value
                   end

    if proto3 && (hashed_value.nil? || value == field.class.default rescue field.default rescue nil)
      result.delete(field.name)
    else
      key = proto3 ? field.name.to_s.camelize(:lower).to_sym : field.name
      result[key] = hashed_value
    end
  end

  result
end

#to_protoObject



188
189
190
# File 'lib/protobuf/message.rb', line 188

def to_proto
  self
end