Class: Protobug::Field::BytesField

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

Direct Known Subclasses

StringField

Class Method Summary collapse

Instance Method Summary collapse

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

.type ⇒ Object



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

#default ⇒ Object



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_type ⇒ Object



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

def wire_type
  2
end