Class: Protobug::Field::MessageField

Inherits:
Protobug::Field show all
Defined in:
lib/protobug/field.rb

Direct Known Subclasses

MapField

Instance Attribute Summary collapse

Attributes inherited from Protobug::Field

#adder, #cardinality, #clearer, #haser, #ivar, #json_name, #name, #number, #oneof, #setter

Instance Method Summary collapse

Methods inherited from Protobug::Field

#binary_decode, #binary_encode, #define_adder, #json_decode, #json_encode, #json_key_encode, #optional?, #packed?, #pretty_print, #proto3_optional?, #repeated?, #to_text, #validate!

Constructor Details

#initialize(number, name, cardinality:, message_type:, json_name: name, oneof: nil, proto3_optional: cardinality == :optional) ⇒ MessageField

Returns a new instance of MessageField.



191
192
193
194
195
196
# File 'lib/protobug/field.rb', line 191

def initialize(number, name, cardinality:, message_type:, json_name: name, oneof: nil,
               proto3_optional: cardinality == :optional)
  super(number, name, json_name: json_name, cardinality: cardinality, oneof: oneof,
                      proto3_optional: proto3_optional)
  @message_type = message_type
end

Instance Attribute Details

#message_typeObject (readonly)

Returns the value of attribute message_type.



189
190
191
# File 'lib/protobug/field.rb', line 189

def message_type
  @message_type
end

Instance Method Details

#binary_decode_one(io, message, registry, wire_type) ⇒ Object



202
203
204
205
206
207
# File 'lib/protobug/field.rb', line 202

def binary_decode_one(io, message, registry, wire_type)
  value = BinaryEncoding.read_field_value(io, wire_type)
  kwargs = {}
  kwargs[:object] = message.send(name) if !repeated? && message.send(haser)
  type_lookup(registry).decode(StringIO.new(value), registry: registry, **kwargs)
end

#binary_encode_one(value, outbuf) ⇒ Object



198
199
200
# File 'lib/protobug/field.rb', line 198

def binary_encode_one(value, outbuf)
  BinaryEncoding.encode_length value.class.encode(value), outbuf
end

#defaultObject



222
223
224
225
226
227
# File 'lib/protobug/field.rb', line 222

def default
  return [] if repeated?

  # TODO: message_type.default
  nil
end

#json_decode_one(value, ignore_unknown_fields, registry) ⇒ Object



209
210
211
212
# File 'lib/protobug/field.rb', line 209

def json_decode_one(value, ignore_unknown_fields, registry)
  klass = type_lookup(registry)
  klass.decode_json_hash(value, registry: registry, ignore_unknown_fields: ignore_unknown_fields)
end

#json_encode_one(value, print_unknown_fields:) ⇒ Object



218
219
220
# File 'lib/protobug/field.rb', line 218

def json_encode_one(value, print_unknown_fields:)
  value.as_json(print_unknown_fields: print_unknown_fields)
end

#type_lookup(registry) ⇒ Object



214
215
216
# File 'lib/protobug/field.rb', line 214

def type_lookup(registry)
  registry.fetch(message_type)
end

#wire_typeObject



229
# File 'lib/protobug/field.rb', line 229

def wire_type = 2