Skip to content

Instantly share code, notes, and snippets.

@sekia
Created July 4, 2012 11:31
Show Gist options
  • Save sekia/3046888 to your computer and use it in GitHub Desktop.
Save sekia/3046888 to your computer and use it in GitHub Desktop.
A patch for AnyEvent::Groonga 0.07
diff --git a/lib/AnyEvent/Groonga.pm b/lib/AnyEvent/Groonga.pm
index 6d57c95..d068b64 100644
--- a/lib/AnyEvent/Groonga.pm
+++ b/lib/AnyEvent/Groonga.pm
@@ -127,7 +127,7 @@ sub _post_to_http_server {
my $json = shift;
my $result;
try {
- my $data = JSON->new->utf8(0)->decode($json);
+ my $data = JSON->new->utf8->decode($json);
$result = AnyEvent::Groonga::Result->new(
posted_command => $command,
data => $data
@@ -164,9 +164,7 @@ sub _post_to_gqtp_server {
my $json = $stdout;
my $result;
try {
- my $data = JSON->new->latin1->decode($json);
-
- #my $data = JSON->new->utf8(0)->decode($json);
+ my $data = JSON->new->utf8->decode($json);
$result = AnyEvent::Groonga::Result->new(
posted_command => $command,
data => $data
@@ -284,7 +282,11 @@ sub _load_filter {
my $self = shift;
my $data = shift;
my $json = JSON->new->latin1->encode($data);
- $json =~ s/"/\\"/g if $self->protocol ne 'http';
+ if ($self->protocol ne 'http') {
+ $json =~ s/\\/\\\\\\\\/g;
+ $json =~ s/'/\\'/g;
+ $json =~ s/"/\\"/g;
+ }
if ( ref $data ne 'ARRAY' ) {
$json = '[' . $json . ']';
}
diff --git a/t/05_regression.t b/t/05_regression.t
new file mode 100644
index 0000000..8a76b87
--- /dev/null
+++ b/t/05_regression.t
@@ -0,0 +1,84 @@
+use strict;
+use warnings;
+use utf8;
+use lib '../lib';
+use AnyEvent::Groonga;
+use Test::More tests => 2;
+use FindBin;
+use File::Spec;
+
+unlink $_ for glob( File::Spec->catfile( $FindBin::RealBin, "data", "*") );
+
+my $g = AnyEvent::Groonga->new( debug => 0 );
+my $groonga_path = $g->groonga_path;
+my $test_database_path
+ = File::Spec->catfile( $FindBin::RealBin, "data", "test.db" );
+
+unless ( $groonga_path and -e $groonga_path ) {
+ plan skip_all => "groonga is not installed.";
+}
+
+`$groonga_path -n $test_database_path quit`;
+
+$g->protocol("local_db");
+$g->database_path(
+ File::Spec->catfile( $FindBin::RealBin, "data", "test.db" ) );
+
+$g->call(
+ table_create => {
+ name => "Site",
+ flags => "TABLE_HASH_KEY",
+ key_type => "ShortText",
+ }
+)->recv;
+
+$g->call(
+ column_create => {
+ table => "Site",
+ name => "title",
+ flags => "COLUMN_SCALAR",
+ type => "ShortText",
+ }
+)->recv;
+
+{
+ my @data = (
+ { _key => "http://example.com/tzd",
+ title => "test record containing backslash\\ character.",
+ },
+ { _key => "http://example.com/gez",
+ title => "test record containing quote' character.",
+ },
+ { _key => "http://example.com/jpn",
+ title => "日本語を含むレコード",
+ },
+ );
+
+ my $result = $g->call(
+ load => {
+ table => "Site",
+ values => \@data,
+ }
+ )->recv;
+ is( $result->body, 3 );
+
+ $result = $g->call( select => { table => "Site" } )->recv;
+
+ is_deeply(
+ $result->items,
+ [
+ { _id => 1,
+ _key => "http://example.com/tzd",
+ title => "test record containing backslash\\ character.",
+ },
+ { _id => 2,
+ _key => "http://example.com/gez",
+ title => "test record containing quote' character.",
+ },
+ { _id => 3,
+ _key => "http://example.com/jpn",
+ title => "日本語を含むレコード",
+ },
+ ]
+ );
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment