Yoji Shidara
null+****@clear*****
Mon Dec 16 14:53:39 JST 2013
Yoji Shidara 2013-12-16 14:53:39 +0900 (Mon, 16 Dec 2013) New Revision: aded71b275b908c25f2bf4b7d58ea5932727f49c https://github.com/droonga/fluent-plugin-droonga/commit/aded71b275b908c25f2bf4b7d58ea5932727f49c Message: Add Droonga::MessagePackPacker Added files: lib/droonga/message_pack_packer.rb test/unit/test_message_pack_packer.rb Added: lib/droonga/message_pack_packer.rb (+48 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/message_pack_packer.rb 2013-12-16 14:53:39 +0900 (227e49a) @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2013 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +module Droonga + module MessagePackPacker + class << self + def to_msgpack(object) + packer = MessagePack::Packer.new + to_msgpack_internal(packer, object) + packer.to_s + end + + def to_msgpack_internal(packer, object) + case object + when Array + packer.write_array_header(object.size) + object.each do |element| + to_msgpack_internal(packer, element) + end + when Hash + packer.write_map_header(object.size) + object.each do |key, value| + to_msgpack_internal(packer, key) + to_msgpack_internal(packer, value) + end + when Time + packer.write(object.utc.iso8601) + else + packer.write(object) + end + end + end + end +end Added: test/unit/test_message_pack_packer.rb (+32 -0) 100644 =================================================================== --- /dev/null +++ test/unit/test_message_pack_packer.rb 2013-12-16 14:53:39 +0900 (495e432) @@ -0,0 +1,32 @@ +# Copyright (C) 2013 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +require "droonga/message_pack_packer" + +class MessagePackPackerTest < Test::Unit::TestCase + def test_to_msgpack + src = [ + 11, + 29, + Time.parse("2013-11-29T08:00:00Z"), + "Groonga", + {"key" => "value"} + ] + actual = Droonga::MessagePackPacker.to_msgpack(src) + expected = "\x95\v\x1D\xB42013-11-29T08:00:00Z\xA7Groonga\x81\xA3key\xA5value".force_encoding("ASCII-8BIT") + + assert_equal(expected, actual) + end +end -------------- next part -------------- HTML����������������������������... 下载