class Avro::Protocol
Constants
- VALID_TYPE_SCHEMA_TYPES
- VALID_TYPE_SCHEMA_TYPES_SYM
Attributes
doc[R]
md5[R]
messages[R]
name[R]
namespace[R]
types[R]
Public Class Methods
new(name, namespace=nil, types=nil, messages=nil, doc=nil)
click to toggle source
# File lib/avro/protocol.rb 40 def initialize(name, namespace=nil, types=nil, messages=nil, doc=nil) 41 # Ensure valid ctor args 42 if !name 43 raise ProtocolParseError, 'Protocols must have a non-empty name.' 44 elsif !name.is_a?(String) 45 raise ProtocolParseError, 'The name property must be a string.' 46 elsif !namespace.is_a?(String) 47 raise ProtocolParseError, 'The namespace property must be a string.' 48 elsif !types.is_a?(Array) 49 raise ProtocolParseError, 'The types property must be a list.' 50 elsif !messages.is_a?(Hash) 51 raise ProtocolParseError, 'The messages property must be a JSON object.' 52 end 53 54 @name = name 55 @namespace = namespace 56 type_names = {} 57 @types = parse_types(types, type_names) 58 @messages = parse_messages(messages, type_names) 59 @md5 = Digest::MD5.digest(to_s) 60 @doc = doc 61 end
parse(protocol_string)
click to toggle source
# File lib/avro/protocol.rb 25 def self.parse(protocol_string) 26 json_data = MultiJson.load(protocol_string) 27 28 if json_data.is_a? Hash 29 name = json_data['protocol'] 30 namespace = json_data['namespace'] 31 types = json_data['types'] 32 messages = json_data['messages'] 33 doc = json_data['doc'] 34 Protocol.new(name, namespace, types, messages, doc) 35 else 36 raise ProtocolParseError, "Not a JSON object: #{json_data}" 37 end 38 end
Public Instance Methods
==(other)
click to toggle source
# File lib/avro/protocol.rb 67 def ==(other) 68 to_avro == other.to_avro 69 end
to_s()
click to toggle source
# File lib/avro/protocol.rb 63 def to_s 64 MultiJson.dump to_avro 65 end
Protected Instance Methods
to_avro(names=Set.new)
click to toggle source
# File lib/avro/protocol.rb 104 def to_avro(names=Set.new) 105 hsh = {'protocol' => name} 106 hsh['namespace'] = namespace if namespace 107 hsh['types'] = types.map{|t| t.to_avro(names) } if types 108 109 if messages 110 hsh['messages'] = messages.inject({}) {|h, (k,t)| h[k] = t.to_avro(names); h } 111 end 112 113 hsh 114 end
Private Instance Methods
parse_messages(messages, names)
click to toggle source
# File lib/avro/protocol.rb 85 def parse_messages(messages, names) 86 message_objects = {} 87 messages.each do |name, body| 88 if message_objects.has_key?(name) 89 raise ProtocolParseError, "Message name \"#{name}\" repeated." 90 elsif !body.is_a?(Hash) 91 raise ProtocolParseError, "Message name \"#{name}\" has non-object body #{body.inspect}" 92 end 93 94 request = body['request'] 95 response = body['response'] 96 errors = body['errors'] 97 doc = body['doc'] 98 message_objects[name] = Message.new(name, request, response, errors, names, namespace, doc) 99 end 100 message_objects 101 end
parse_types(types, type_names)
click to toggle source
# File lib/avro/protocol.rb 72 def parse_types(types, type_names) 73 types.collect do |type| 74 # FIXME adding type.name to type_names is not defined in the 75 # spec. Possible bug in the python impl and the spec. 76 type_object = Schema.real_parse(type, type_names, namespace) 77 unless VALID_TYPE_SCHEMA_TYPES_SYM.include?(type_object.type_sym) 78 msg = "Type #{type} not an enum, record, fixed or error." 79 raise ProtocolParseError, msg 80 end 81 type_object 82 end 83 end