Skip to content

Commit

Permalink
Add support for Neo4j::Types
Browse files Browse the repository at this point in the history
Neo4j::Driver 1.02 no longer reblesses values received via Neo4j::Bolt. Like before, the returned values are still Neo4j::Types implementations, so their interface doesn't actually change. But the package name of such values changes from Neo4j::Driver::Type::* to Neo4j::Bolt::*, which trips up some checks in REST::Neo4p that expect to see "Driver" in the package name.

Original discussion: majensen/perlbolt#37
  • Loading branch information
johannessen committed Dec 11, 2024
1 parent fef50b8 commit e7ca25a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ my $build = $class->new
'experimental' => 0,
'MIME::Base64' => 0,
perl => 5.010001,
'Neo4j::Driver' => '0.19',
'Neo4j::Driver' => '0.21', # Neo4j::Types
},
recommends => {
'Mojo::UserAgent' => 0,
Expand Down
3 changes: 2 additions & 1 deletion lib/REST/Neo4p/Entity.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package REST::Neo4p::Entity;
use REST::Neo4p::Exceptions;
use Carp qw(croak carp);
use JSON;
use Scalar::Util qw(blessed);
use URI::Escape;
use strict;
use warnings;
Expand Down Expand Up @@ -65,7 +66,7 @@ sub new_from_json_response {
unless (defined $decoded_resp) {
REST::Neo4p::LocalException->throw("new_from_json_response() called with undef argument\n");
}
my $is_json = !(ref($decoded_resp) =~ /Neo4j::Driver/);
my $is_json = ! blessed $decoded_resp; # blessed via Neo4j::Driver
unless ($ENTITY_TABLE->{$entity_type}{_actions} || !$is_json) {
# capture the url suffix patterns for the entity actions:
for (keys %$decoded_resp) {
Expand Down
6 changes: 3 additions & 3 deletions lib/REST/Neo4p/Node.pm
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ sub simple_from_json_response {
my $class = shift;
my ($decoded_resp) = @_;
my $ret;
for (ref $decoded_resp) {
/HASH/ && do {
for ($decoded_resp) {
ref eq 'HASH' && do {
# node id
($ret->{_node}) = $decoded_resp->{self} =~ m{.*/([0-9]+)$};
# node properties
Expand All @@ -235,7 +235,7 @@ sub simple_from_json_response {
}
last;
};
/Driver/ && do {
$_->isa('Neo4j::Types::Node') && do { # via Neo4j::Driver
$ret->{_node} = $decoded_resp->id;
$ret->{$_} = $decoded_resp->properties->{$_} for keys %{$decoded_resp->properties};
last;
Expand Down
3 changes: 2 additions & 1 deletion lib/REST/Neo4p/Path.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package REST::Neo4p::Path;
use REST::Neo4p::Exceptions;
use Carp qw(croak carp);
use Scalar::Util qw(blessed);
use strict;
use warnings;
BEGIN {
Expand All @@ -16,7 +17,7 @@ sub new {
sub new_from_json_response {
my $class = shift;
my ($decoded_resp) = @_;
return $class->new_from_driver_obj(@_) if (ref($decoded_resp) =~ /Neo4j::Driver/);
return $class->new_from_driver_obj(@_) if blessed $decoded_resp;
REST::Neo4p::LocalException->throw("Arg does not describe a Neo4j path response\n") unless $decoded_resp->{start} && $decoded_resp->{end} && $decoded_resp->{relationships} && $decoded_resp->{nodes};
my $obj = bless {}, $class;
$obj->{_length} = $decoded_resp->{length};
Expand Down
17 changes: 10 additions & 7 deletions lib/REST/Neo4p/Query.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use REST::Neo4p::Exceptions;
use JSON::XS;
use REST::Neo4p::ParseStream;
use HOP::Stream qw/drop/;
use Scalar::Util qw(blessed);
use Tie::IxHash;
use File::Temp qw(:seekable);
use Carp qw(croak carp);
Expand Down Expand Up @@ -177,22 +178,24 @@ sub _wrap_statement_result {
for (my $i=0;$i<$n;$i++) {
my $elt = $rec->get($i);
my $cvt = sub {
return $_[0] unless ref($_[0]) =~ /Driver/;
my ($type) = ref($_[0]) =~ /::([^:]+)$/;
my $cls = "REST::Neo4p::$type";
return $_[0] unless blessed $_[0];
my $cls = $_[0]->isa('Neo4j::Types::Node') ? 'REST::Neo4p::Node'
: $_[0]->isa('Neo4j::Types::Relationship') ? 'REST::Neo4p::Relationship'
: $_[0]->isa('Neo4j::Types::Path') ? 'REST::Neo4p::Path'
: undef or return $_[0]; # spatial/temporal values
return $as_object ? $cls->new_from_json_response($_[0]) :
$cls->simple_from_json_response($_[0]);
};
for (ref($elt)) {
/Driver/ && do {
for ($elt) {
blessed $_ && do { # Neo4j::Types::*, via Neo4j::Driver
$elt = $cvt->($elt);
};
/HASH/ && do {
ref eq 'HASH' && do {
for (keys %$elt) {
$elt->{$_} = $cvt->($elt->{$_})
}
};
/ARRAY/ && do {
ref eq 'ARRAY' && do {
for (@$elt) {
$_ = $cvt->($_);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/REST/Neo4p/Relationship.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ sub simple_from_json_response {
my $class = shift;
my ($decoded_resp) = @_;
my $ret;
for (ref $decoded_resp) {
/HASH/ && do {
for ($decoded_resp) {
ref eq 'HASH' && do {
# reln id
($ret->{_relationship}) = $decoded_resp->{self} =~ m{.*/([0-9]+)$};
# reln type
Expand All @@ -62,7 +62,7 @@ sub simple_from_json_response {
($ret->{_end}) = $decoded_resp->{end} =~ m{.*/([0-9]+)$};
last;
};
/Driver/ && do {
$_->isa('Neo4j::Types::Relationship') && do { # via Neo4j::Driver
$ret->{_relationship} = $decoded_resp->id;
$ret->{_type} = $decoded_resp->type;
$ret->{$_} = $decoded_resp->properties->{$_} for keys %{$decoded_resp->properties};
Expand Down

0 comments on commit e7ca25a

Please sign in to comment.