[SCM] libmessage-passing-perl Debian packaging branch, master, updated. debian/0.111-3-14-g44f6e88

Tomas Doran bobtfish at bobtfish.net
Mon May 6 11:57:09 UTC 2013


The following commit has been merged in the master branch:
commit 00ff76f472336f6fe9e34484cc48693417efd155
Author: Tomas Doran <bobtfish at bobtfish.net>
Date:   Sat Jun 9 18:02:07 2012 +0100

    Make the encoder and decoders pass through.
    
    When appropriate - this avoids the fact you need to explicitly set the decoder to Null
    for any input which directly produces a hash (e.g. ::Input::Freeswitch)

diff --git a/Changes b/Changes
index 8f464c5..5378d76 100644
--- a/Changes
+++ b/Changes
@@ -1,3 +1,11 @@
+  - Make JSON encoder pass non refs straight through,
+    so that if a previous filter generates a scalar,
+    then this gets sent as-is.
+
+  - Make JSON decoder pass refs straight through, to
+    act as a no-op if the input has already decoded
+    it's data into a hash.
+
   - Remove spurious warnings from reconnect code.
 
 0.006
diff --git a/lib/Message/Passing/Filter/Decoder/JSON.pm b/lib/Message/Passing/Filter/Decoder/JSON.pm
index 4596735..918c243 100644
--- a/lib/Message/Passing/Filter/Decoder/JSON.pm
+++ b/lib/Message/Passing/Filter/Decoder/JSON.pm
@@ -5,7 +5,7 @@ use namespace::autoclean;
 
 with 'Message::Passing::Role::Filter';
 
-sub filter { from_json( $_[1], { utf8  => 1 } ) }
+sub filter { ref($_[1]) ? $_[1] : from_json( $_[1], { utf8  => 1 } ) }
 
 __PACKAGE__->meta->make_immutable;
 1;
diff --git a/lib/Message/Passing/Filter/Encoder/JSON.pm b/lib/Message/Passing/Filter/Encoder/JSON.pm
index f4cfead..2e3def5 100644
--- a/lib/Message/Passing/Filter/Encoder/JSON.pm
+++ b/lib/Message/Passing/Filter/Encoder/JSON.pm
@@ -14,6 +14,7 @@ has pretty => (
 
 sub filter {
     my ($self, $message) = @_;
+    return $message unless ref($message);
     if (blessed $message) { # FIXME - This should be moved out of here!
         if ($message->can('pack')) {
             $message = $message->pack;
diff --git a/t/input_decode.t b/t/input_decode.t
new file mode 100644
index 0000000..7617b67
--- /dev/null
+++ b/t/input_decode.t
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use Test::More;
+use Try::Tiny;
+
+use Message::Passing::Output::Test;
+use Message::Passing::Filter::Decoder::JSON;
+
+my $unpacked;
+my $test = Message::Passing::Output::Test->new(cb => sub { $unpacked = shift });
+my $decoder = Message::Passing::Filter::Decoder::JSON->new(output_to => $test);
+
+my $h = {};
+$decoder->consume($h);;
+is_deeply $unpacked, $h;
+
+$decoder->consume('{"baz":"bar"}');
+is_deeply $unpacked, {"baz" => "bar"};
+
+done_testing;
+
diff --git a/t/output_encode.t b/t/output_encode.t
index 85e0eff..4875d39 100644
--- a/t/output_encode.t
+++ b/t/output_encode.t
@@ -4,13 +4,21 @@ use Test::More;
 use Try::Tiny;
 
 {
-    package Message;
+    package Message::MXS;
     use strict;
     use warnings;
 
     sub pack { { foo => "bar" } }
 }
 
+{
+    package Message::LMS;
+    use strict;
+    use warnings;
+
+    sub to_hash { { baz => "bar" } }
+}
+
 use Message::Passing::Output::Test;
 use Message::Passing::Filter::Encoder::JSON;
 
@@ -18,9 +26,14 @@ my $packed;
 my $test = Message::Passing::Output::Test->new(cb => sub { $packed = shift });
 my $encoder = Message::Passing::Filter::Encoder::JSON->new(output_to => $test);
 
-$encoder->consume(bless {}, 'Message');
-
+$encoder->consume(bless {}, 'Message::MXS');
 is $packed, '{"foo":"bar"}';
 
+$encoder->consume(bless {}, 'Message::LMS');
+is $packed, '{"baz":"bar"}';
+
+$encoder->consume('{}');
+is $packed, '{}';
+
 done_testing;
 

-- 
libmessage-passing-perl Debian packaging



More information about the Pkg-perl-cvs-commits mailing list