lib/protocol_buffers/runtime/message.rb in ruby-protocol-buffers-1.5.1 vs lib/protocol_buffers/runtime/message.rb in ruby-protocol-buffers-1.6.0
- old
+ new
@@ -1,9 +1,11 @@
require 'stringio'
require 'protocol_buffers/runtime/field'
require 'protocol_buffers/runtime/encoder'
require 'protocol_buffers/runtime/decoder'
+require 'protocol_buffers/runtime/text_formatter'
+require 'protocol_buffers/runtime/text_parser'
module ProtocolBuffers
# = Generated Code
#
@@ -255,10 +257,23 @@
serialize(sio)
return sio.string
end
alias_method :to_s, :serialize_to_string
+ # Format this message into the given IO stream using the text format of Protocol Buffers.
+ def text_format(io, options = nil)
+ formatter = TextFormatter.new(options)
+ formatter.format(io, self)
+ end
+
+ # Format this message into a text and return it.
+ def text_format_to_string(options = nil)
+ sio = ProtocolBuffers.utf8_sio
+ text_format(sio, options)
+ return sio.string
+ end
+
def to_hash
self.class.to_hash(self)
end
def self.to_hash(message)
@@ -294,10 +309,23 @@
# Shortcut, simply calls self.new.parse(io)
def self.parse(io)
self.new.parse(io)
end
+ # Parse the text as a text representation of this class, and merge the parsed fields
+ # into the current message.
+ def parse_from_text(text)
+ parser = TextParser.new
+ parser.parse_text(text, self)
+ return self
+ end
+
+ # Shortcut, simply calls self.new.parse_from_text(text)
+ def self.parse_from_text(text)
+ self.new.parse_from_text(text)
+ end
+
# Merge the attribute values from +obj+ into this Message, which must be of
# the same class.
#
# Singular fields will be overwritten, except for embedded messages which
# will be merged. Repeated fields will be concatenated.
@@ -422,9 +450,44 @@
# message.value_for_tag?(message.class.field_for_name(:f1).tag)
# # is equivalent to
# message.has_f1?
def value_for_tag?(tag)
@set_fields[tag] || false
+ end
+
+ # Gets the field, returning nil if not set
+ # If a block is given, this block is called and it's
+ # return value returned if the value is not set
+ def get(*nested_field_names, &b)
+ if nested_field_names.size == 1
+ field_name = nested_field_names.first
+ field = self.class.field_for_name(field_name)
+ raise ArgumentError.new unless field
+ unless self.value_for_tag?(field.tag)
+ return b ? b.call : nil
+ end
+ return self.value_for_tag(field.tag)
+ end
+ last_proto = nested_field_names[0..-2].inject(self) do |sub_proto, ifield_name|
+ sub_field = sub_proto.class.field_for_name(ifield_name)
+ raise ArgumentError.new unless sub_field
+ raise ArgumentError.new unless sub_field.is_a?(ProtocolBuffers::Field::MessageField)
+ unless sub_proto.value_for_tag?(sub_field.tag)
+ return b ? b.call : nil
+ end
+ sub_proto.value_for_tag(sub_field.tag)
+ end
+ last_field_name = nested_field_names.last
+ last_field = last_proto.class.field_for_name(last_field_name)
+ unless last_proto.value_for_tag?(last_field.tag)
+ return b ? b.call : nil
+ end
+ last_proto.value_for_tag(last_field.tag)
+ end
+
+ # Gets the field, throwing ArgumentError if not set
+ def get!(*nested_field_names)
+ get(*nested_field_names) { raise ArgumentError.new("#{nested_field_names} is not set") }
end
def inspect
ret = ProtocolBuffers.bin_sio
ret << "#<#{self.class.name}"