Class: ProtocolBuffers::TextFormatter
- Inherits:
-
Object
- Object
- ProtocolBuffers::TextFormatter
- Defined in:
- lib/protocol_buffers/runtime/text_formatter.rb
Instance Method Summary collapse
- #format(io, message, options = nil) ⇒ Object
- #format_field(io, field, value, indent, newline, options) ⇒ Object
- #format_unknown_field(io, wire_type, id, value, options) ⇒ Object
-
#initialize(options = nil) ⇒ TextFormatter
constructor
A new instance of TextFormatter.
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( = nil) @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, , = nil) .validate! ||= {} = .merge(@options) [:nest] ||= 0 if [:short] indent = "" newline = " " else indent = " " * [:nest] newline = "\n" end sep = "" .fields.each do |tag, field| next unless .value_for_tag?(tag) value = .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, ) end else io.write sep; sep = newline format_field(io, field, value, indent, newline, ) end end .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, ) end io.write sep if ![: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, ) 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, ) 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, ) = .dup [:nest] ||= 0 if [:short] indent = "" newline = " " else indent = " " * [:nest] newline = "\n" end if wire_type == 3 [: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, ) 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 |