You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/06/18 03:18:28 UTC
svn commit: r669018 - in /incubator/thrift/trunk/lib/rb:
lib/thrift/serializer.rb spec/serializer_spec.rb
Author: kclark
Date: Tue Jun 17 18:18:28 2008
New Revision: 669018
URL: http://svn.apache.org/viewvc?rev=669018&view=rev
Log:
rb: Implement Thrift::Serializer and Thrift::Deserializer
Added:
incubator/thrift/trunk/lib/rb/lib/thrift/serializer.rb
incubator/thrift/trunk/lib/rb/spec/serializer_spec.rb
Added: incubator/thrift/trunk/lib/rb/lib/thrift/serializer.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/serializer.rb?rev=669018&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/serializer.rb (added)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/serializer.rb Tue Jun 17 18:18:28 2008
@@ -0,0 +1,27 @@
+module Thrift
+ class Serializer
+ def initialize(protocolFactory = BinaryProtocolFactory.new)
+ @transport = MemoryBuffer.new
+ @protocol = protocolFactory.get_protocol(@transport)
+ end
+
+ def serialize(base)
+ @transport.reset_buffer
+ base.write(@protocol)
+ @transport.read(@transport.available)
+ end
+ end
+
+ class Deserializer
+ def initialize(protocolFactory = BinaryProtocolFactory.new)
+ @transport = MemoryBuffer.new
+ @protocol = protocolFactory.get_protocol(@transport)
+ end
+
+ def deserialize(base, buffer)
+ @transport.reset_buffer(buffer)
+ base.read(@protocol)
+ base
+ end
+ end
+end
Added: incubator/thrift/trunk/lib/rb/spec/serializer_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/serializer_spec.rb?rev=669018&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/serializer_spec.rb (added)
+++ incubator/thrift/trunk/lib/rb/spec/serializer_spec.rb Tue Jun 17 18:18:28 2008
@@ -0,0 +1,50 @@
+require File.dirname(__FILE__) + '/spec_helper'
+require 'thrift/serializer'
+require 'ThriftSpec_types'
+
+class ThriftSerializerSpec < Spec::ExampleGroup
+ include Thrift
+ include SpecNamespace
+
+ describe Serializer do
+ it "should serialize structs to binary by default" do
+ serializer = Serializer.new
+ data = serializer.serialize(Hello.new(:greeting => "'Ello guv'nor!"))
+ data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
+ end
+
+ it "should serialize structs to the given protocol" do
+ protocol = mock("Protocol")
+ protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
+ protocol.should_receive(:write_field).with("greeting", Types::STRING, 1, "Good day")
+ protocol.should_receive(:write_field_stop)
+ protocol.should_receive(:write_struct_end)
+ protocolFactory = mock("ProtocolFactory")
+ protocolFactory.stub!(:get_protocol).and_return(protocol)
+ serializer = Serializer.new(protocolFactory)
+ serializer.serialize(Hello.new(:greeting => "Good day"))
+ end
+ end
+
+ describe Deserializer do
+ it "should deserialize structs from binary by default" do
+ deserializer = Deserializer.new
+ data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
+ deserializer.deserialize(Hello.new, data).should == Hello.new(:greeting => "'Ello guv'nor!")
+ end
+
+ it "should deserialize structs from the given protocol" do
+ protocol = mock("Protocol")
+ protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
+ protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1],
+ [nil, Types::STOP, 0])
+ protocol.should_receive(:read_type).with(Types::STRING).and_return("Good day")
+ protocol.should_receive(:read_field_end)
+ protocol.should_receive(:read_struct_end)
+ protocolFactory = mock("ProtocolFactory")
+ protocolFactory.stub!(:get_protocol).and_return(protocol)
+ deserializer = Deserializer.new(protocolFactory)
+ deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day")
+ end
+ end
+end