Class: ProtocolBuffers::TextFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol_buffers/runtime/text_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ TextFormatter

Returns a new instance of TextFormatter.



3
4
5
# File 'lib/protocol_buffers/runtime/text_formatter.rb', line 3

def initialize(options = nil)
  @options = options || {}
end

Instance Method Details

#format(io, message, options = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protocol_buffers/runtime/text_formatter.rb', line 7

def format(io, message, options = nil)
  message.validate!
  options ||= {}
  options = options.merge(@options)
  options[:nest] ||= 0

  if options[:short]
    indent = ""
    newline = " "
  else
    indent = "  " * options[:nest]
    newline = "\n"
  end

  sep = ""
  message.fields.each do |tag, field|
    next unless message.value_for_tag?(tag)
    value = message.value_for_tag(tag)
    if field.repeated?
      next if value.size == 0
      value.each do |v|
        io.write sep; sep = newline

        format_field(io, field, v, indent, newline, options)
      end
    else
      io.write sep; sep = newline

      format_field(io, field, value, indent, newline, options)
    end
  end

  message.each_unknown_field do |tag_int, value|
    io.write sep; sep = newline

    wire_type = tag_int & 0x7
    id = tag_int >> 3
    format_unknown_field(io, wire_type, id, value, options)
  end

  io.write sep if !options[:short]

  io
end

#format_field(io, field, value, indent, newline, options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/protocol_buffers/runtime/text_formatter.rb', line 52

def format_field(io, field, value, indent, newline, options)
  if field.kind_of? Field::GroupField
    name = value.class.name.sub(/\A.*::/, '')
  else
    name = field.name
  end

  io.write "#{indent}#{name}"
  if field.kind_of? Field::AggregateField
    io.write " "
  else
    io.write ": "
  end
  field.text_format(io, value, options)
end

#format_unknown_field(io, wire_type, id, value, options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/protocol_buffers/runtime/text_formatter.rb', line 68

def format_unknown_field(io, wire_type, id, value, options)
  options = options.dup
  options[:nest] ||= 0

  if options[:short]
    indent = ""
    newline = " "
  else
    indent = "  " * options[:nest]
    newline = "\n"
  end

  if wire_type == 3
    options[:nest] += 1

    io.write "#{indent}#{id} {#{newline}"
  else
    io.write "#{indent}#{id}: "
  end

  case wire_type
  when 0 # VARINT
    io.write "#{value}"

  when 1 # FIXED64
    lo, hi = value.unpack("V2")
    io.write "0x%016x" % (hi << 32 | lo)

  when 5 # FIXED32
    io.write "0x%08x" % value.unpack("V")

  when 2 # LENGTH_DELIMITED
    value = value.unpack("C*").map { |b| "\\x%02x" % b }.join(nil)
    io.write "\"#{value}\""

  when 3 # START_GROUP
    format(io, value, options)

  when 4 # END_GROUP: never appear
    raise(EncodeError, "Unexpected wire type END_GROUP")
  else
    raise(EncodeError, "unknown wire type: #{wire_type}")
  end
  if wire_type == 3
    io.write "#{indent}}"
  end
end