Module: ProtocolBuffers::Encoder

Defined in:
lib/protocol_buffers/runtime/encoder.rb

Overview

:nodoc: all

Class Method Summary collapse

Class Method Details

.encode(io, message) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/protocol_buffers/runtime/encoder.rb', line 6

def self.encode(io, message)
  unless message.valid?
    raise(EncodeError, "invalid message")
  end

  message.fields.each do |tag, field|
    next unless message.value_for_tag?(tag)

    value = message.value_for_tag(tag)
    wire_type = field.wire_type
    tag = (field.tag << 3) | wire_type

    if field.repeated?
      value.each { |i| serialize_field(io, tag, wire_type,
                                       field.serialize(i)) }
    else
      serialize_field(io, tag, wire_type, field.serialize(value))
    end
  end

  message.each_unknown_field do |tag_int, value|
    wire_type = tag_int & 0b111
    serialize_field(io, tag_int, wire_type, value)
  end
end

.serialize_field(io, tag, wire_type, serialized) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protocol_buffers/runtime/encoder.rb', line 32

def self.serialize_field(io, tag, wire_type, serialized)
  # write the tag
  Varint.encode(io, tag)

  # see comment in decoder.rb about magic numbers
  case wire_type
  when 0 # VARINT
    Varint.encode(io, serialized)
  when 1, 5 # FIXED64, FIXED32
    io.write(serialized)
  when 2 # LENGTH_DELIMITED
    Varint.encode(io, serialized.length)
    io.write(serialized)
  when 3, 4 # deprecated START_GROUP/END_GROUP types
    raise(EncodeError, "groups are deprecated and unsupported")
  else
    raise(EncodeError, "unknown wire type: #{wire_type}")
  end
end