File Coverage

blib/lib/Rethinkdb/Util.pm
Criterion Covered Total %
statement 18 87 20.6
branch 0 38 0.0
condition 0 20 0.0
subroutine 6 16 37.5
pod n/a
total 24 161 14.9


line stmt bran cond sub pod time code
1             package Rethinkdb::Util;
2 15     15   59 use Rethinkdb::Base -base;
  15         17  
  15         92  
3              
4 15     15   73 use Scalar::Util 'blessed';
  15         23  
  15         591  
5 15     15   56 use JSON::PP 'encode_json';
  15         22  
  15         673  
6 15     15   49 use Carp 'croak';
  15         25  
  15         482  
7              
8 15     15   4832 use Rethinkdb::Query::Datum;
  15         23  
  15         89  
9 15     15   70 use Rethinkdb::Protocol;
  15         20  
  15         45  
10              
11             my $PROTOCOL = Rethinkdb::Protocol->new;
12             my $COUNTER = 0;
13              
14             sub _token {
15 0     0     return $COUNTER++;
16             }
17              
18             sub _wrap_func_helper {
19 0     0     my $node = shift;
20              
21 0 0 0       if ( !( blessed $node && $node->isa('Rethinkdb::Query') ) ) {
22 0           return;
23             }
24              
25 0 0 0       if ( blessed $node
      0        
26             && $node->_type
27             && $node->_type eq $PROTOCOL->term->termType->implicit_var )
28             {
29 0           return 1;
30             }
31              
32 0 0         if ( $node->args ) {
33 0           foreach ( @{ $node->args } ) {
  0            
34 0 0         if ( _wrap_func_helper($_) ) {
35 0           return 1;
36             }
37             }
38             }
39              
40 0           return;
41             }
42              
43             sub _wrap_func {
44 0     0     my $self = shift;
45 0           my $arg = shift;
46 0           my $force = shift;
47              
48 0           my $val = $self->_expr($arg);
49              
50 0 0         if ( _wrap_func_helper $val ) {
    0          
51 0     0     return $self->_make_func( sub ($) { $val; } );
  0            
52             }
53             elsif( $force ) {
54 0     0     return $self->_make_func( sub ($) { $val; } );
  0            
55             }
56              
57 0           return $val;
58             }
59              
60             sub _expr {
61 0     0     my $self = shift;
62 0           my $value = shift;
63              
64 0 0 0       if ( blessed($value) && $value->can('_build') ) {
    0          
    0          
    0          
65 0           return $value;
66             }
67             elsif ( ref $value eq 'ARRAY' ) {
68 0           return $self->_make_array($value);
69             }
70             elsif ( ref $value eq 'HASH' ) {
71 0           return $self->_make_obj($value);
72             }
73             elsif ( ref $value eq 'CODE' ) {
74 0           return $self->_make_func($value);
75             }
76             else {
77 0           return Rethinkdb::Query::Datum->new( { data => $value } );
78             }
79              
80             # to croak or not?
81 0           return;
82             }
83              
84             # try to make expr mostly JSON
85             sub _expr_json {
86 0     0     my $self = shift;
87 0           my $value = shift;
88              
89 0 0 0       if ( blessed($value) && $value->can('_build') ) {
90 0           return $value;
91             }
92              
93 0           my $retval;
94 0           eval { $retval = encode_json $value; };
  0            
95              
96 0 0 0       if ( !$@ && $retval ) {
    0          
    0          
    0          
97 0           return Rethinkdb::Query->new(
98             _type => $PROTOCOL->term->termType->json,
99             args => $retval
100             );
101             }
102             elsif ( ref $value eq 'ARRAY' ) {
103 0           return $self->_make_array($value);
104             }
105             elsif ( ref $value eq 'HASH' ) {
106 0           return $self->_make_obj($value);
107             }
108             elsif ( ref $value eq 'CODE' ) {
109 0           return $self->_make_func($value);
110             }
111             else {
112 0           return Rethinkdb::Query::Datum->new( { data => $value } );
113             }
114              
115             # to croak or not?
116 0           return;
117             }
118              
119             sub _make_array {
120 0     0     my $self = shift;
121 0 0         my $args = @_ ? @_ > 1 ? [@_] : [ @{ $_[0] } ] : [];
  0 0          
122              
123 0           my $obj = Rethinkdb::Query->new(
124             _type => $PROTOCOL->term->termType->make_array,
125             args => $args,
126             );
127              
128 0           return $obj;
129             }
130              
131             sub _make_obj {
132 0     0     my $self = shift;
133 0 0         my $optargs = @_ ? @_ > 1 ? {@_} : { %{ $_[0] } } : {};
  0 0          
134              
135 0           my $obj = Rethinkdb::Query->new(
136             _type => $PROTOCOL->term->termType->make_obj,
137             optargs => $optargs,
138             );
139              
140 0           return $obj;
141             }
142              
143             sub _make_func {
144 0     0     my $self = shift;
145 0           my $func = shift;
146              
147 0           my $params = [];
148 0           my $prototype = prototype $func;
149 0   0       $prototype ||= '$';
150 0           my $param_length = length $prototype;
151              
152 0           foreach ( 1 .. $param_length ) {
153 0           push @{$params},
  0            
154             Rethinkdb::Query->new(
155             _type => $PROTOCOL->term->termType->var,
156             args => $_,
157             );
158             }
159              
160 0           my $body = $func->( @{$params} );
  0            
161 0           my $args = $self->_make_array( [ 1 .. $param_length ] );
162              
163 0           my $obj = Rethinkdb::Query->new(
164             _type => $PROTOCOL->term->termType->func,
165             args => [ $args, $body ],
166             );
167              
168 0           return $obj;
169             }
170              
171             1;
172              
173             =encoding utf8
174              
175             =head1 NAME
176              
177             Rethinkdb::Util - RethinkDB Utilities
178              
179             =head1 DESCRIPTION
180              
181             This module contains internal utilities used by the RethinkDB perl driver.
182              
183             =head1 SEE ALSO
184              
185             L, L
186              
187             =cut