Class: Protobug::Field::BytesField

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

Direct Known Subclasses

StringField

Instance Attribute Summary

Attributes inherited from Protobug::Field

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

Class Method Summary collapse

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:, json_name: name, oneof: nil, proto3_optional: cardinality == :optional) ⇒ BytesField

Returns a new instance of BytesField.



317
318
319
320
321
# File 'lib/protobug/field.rb', line 317

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

Class Method Details

.typeObject



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

def self.type = :bytes

Instance Method Details

#binary_decode_one(io, _message, _registry, wire_type) ⇒ Object



327
328
329
# File 'lib/protobug/field.rb', line 327

def binary_decode_one(io, _message, _registry, wire_type)
  BinaryEncoding.read_field_value(io, wire_type)
end

#binary_encode_one(value, outbuf) ⇒ Object



323
324
325
# File 'lib/protobug/field.rb', line 323

def binary_encode_one(value, outbuf)
  BinaryEncoding.encode_length value.b, outbuf
end

#defaultObject



349
350
351
352
353
# File 'lib/protobug/field.rb', line 349

def default
  return [] if repeated?

  "".b
end

#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/protobug/field.rb', line 331

def json_decode_one(value, _ignore_unknown_fields, _registry)
  return UNSET if value.nil?

  # url decode 64
  value.tr!("-_", "+/")
  begin
    value = value.unpack1("m").force_encoding(Encoding::BINARY)
  rescue ArgumentError => e
    raise DecodeError, "Invalid URL-encoded base64 #{value.inspect} for #{inspect}: #{e}"
  end

  value
end

#json_encode_one(value, print_unknown_fields:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



345
346
347
# File 'lib/protobug/field.rb', line 345

def json_encode_one(value, print_unknown_fields:) # rubocop:disable Lint/UnusedMethodArgument
  [value].pack("m0")
end

#wire_typeObject



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

def wire_type = 2