Class: Protobug::Field::BytesField

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

Direct Known Subclasses

StringField

Constant Summary

Constants inherited from Protobug::Field

PACKABLE_WIRE_TYPES

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

#adder_method_definition, #binary_decode, #binary_encode, #json_decode, #json_encode, #json_key_encode, #method_definitions, #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.



368
369
370
371
372
# File 'lib/protobug/field.rb', line 368

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



364
365
366
# File 'lib/protobug/field.rb', line 364

def self.type
  :bytes
end

Instance Method Details

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



378
379
380
# File 'lib/protobug/field.rb', line 378

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

#binary_encode_one(value, outbuf) ⇒ Object



374
375
376
# File 'lib/protobug/field.rb', line 374

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

#defaultObject



400
401
402
403
404
# File 'lib/protobug/field.rb', line 400

def default
  return [] if repeated?

  "".b
end

#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/protobug/field.rb', line 382

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



396
397
398
# File 'lib/protobug/field.rb', line 396

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

#wire_typeObject



406
407
408
# File 'lib/protobug/field.rb', line 406

def wire_type
  2
end