lib/avro/protocol.rb in avro-1.8.2 vs lib/avro/protocol.rb in avro-1.9.0
- old
+ new
@@ -18,26 +18,27 @@
class Protocol
VALID_TYPE_SCHEMA_TYPES = Set.new(%w[enum record error fixed])
VALID_TYPE_SCHEMA_TYPES_SYM = Set.new(VALID_TYPE_SCHEMA_TYPES.map(&:to_sym))
class ProtocolParseError < Avro::AvroError; end
- attr_reader :name, :namespace, :types, :messages, :md5
+ attr_reader :name, :namespace, :types, :messages, :md5, :doc
def self.parse(protocol_string)
json_data = MultiJson.load(protocol_string)
if json_data.is_a? Hash
name = json_data['protocol']
namespace = json_data['namespace']
types = json_data['types']
messages = json_data['messages']
- Protocol.new(name, namespace, types, messages)
+ doc = json_data['doc']
+ Protocol.new(name, namespace, types, messages, doc)
else
raise ProtocolParseError, "Not a JSON object: #{json_data}"
end
end
- def initialize(name, namespace=nil, types=nil, messages=nil)
+ def initialize(name, namespace=nil, types=nil, messages=nil, doc=nil)
# Ensure valid ctor args
if !name
raise ProtocolParseError, 'Protocols must have a non-empty name.'
elsif !name.is_a?(String)
raise ProtocolParseError, 'The name property must be a string.'
@@ -53,10 +54,11 @@
@namespace = namespace
type_names = {}
@types = parse_types(types, type_names)
@messages = parse_messages(messages, type_names)
@md5 = Digest::MD5.digest(to_s)
+ @doc = doc
end
def to_s
MultiJson.dump to_avro
end
@@ -65,11 +67,10 @@
to_avro == other.to_avro
end
private
def parse_types(types, type_names)
- type_objects = []
types.collect do |type|
# FIXME adding type.name to type_names is not defined in the
# spec. Possible bug in the python impl and the spec.
type_object = Schema.real_parse(type, type_names, namespace)
unless VALID_TYPE_SCHEMA_TYPES_SYM.include?(type_object.type_sym)
@@ -90,11 +91,12 @@
end
request = body['request']
response = body['response']
errors = body['errors']
- message_objects[name] = Message.new(name, request, response, errors, names, namespace)
+ doc = body['doc']
+ message_objects[name] = Message.new(name, request, response, errors, names, namespace, doc)
end
message_objects
end
protected
@@ -109,25 +111,27 @@
hsh
end
class Message
- attr_reader :name, :request, :response, :errors, :default_namespace
+ attr_reader :name, :request, :response, :errors, :default_namespace, :doc
- def initialize(name, request, response, errors=nil, names=nil, default_namespace=nil)
+ def initialize(name, request, response, errors=nil, names=nil, default_namespace=nil, doc=nil)
@name = name
@default_namespace = default_namespace
@request = parse_request(request, names)
@response = parse_response(response, names)
@errors = parse_errors(errors, names) if errors
+ @doc = doc
end
def to_avro(names=Set.new)
{
'request' => request.to_avro(names),
'response' => response.to_avro(names)
}.tap do |hash|
hash['errors'] = errors.to_avro(names) if errors
+ hash['doc'] = @doc if @doc
end
end
def to_s
Yajl.dump to_avro