lib/Jubatus/Common/Types.pm untested

filter: Statementstmt 0.0% 0 / 810 Branchbran 0.0% 0 / 146 Conditioncond 0.0% 0 / 84 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 0.0% 0 / 164 Podpod 0.0% 0 / 50 totaltotal 0.0% 0 / 1305 CC 74 SCAR 28.3
CC
74
Cov
0%
check_type
110.0
estimate_type
90.0
check_value
56.0
CRAP 17.0
1 package Jubatus::Common::Exception;
2  
3 use strict;
4 use warnings;
5 use utf8;
6 use autodie;
7  
8 use parent 'Exception::Tiny';
9  
10 use constant JUBATUS_DEBUG => $ENV{JUBATUS_DEBUG};
11 use Log::Minimal qw/debugf infof warnf critf/; # $ENV{LM_DEBUG}
12 local $Log::Minimal::AUTODUMP = 1;
13 local $Log::Minimal::COLOR = 1;
14 local $Log::Minimal::LOG_LEVEL = "DEBUG";
15  
16 # It's used to show 'OtherException'
17 sub show {
18 my ($self, $e) = @_;
19 warnf ($e) if (JUBATUS_DEBUG);
20 return;
21 }
22  
23 1;
24  
25 package Jubatus::Common::TypeException;
26  
27 use strict;
28 use warnings;
29 use utf8;
30 use autodie;
31  
32 use parent -norequire, 'Jubatus::Common::Exception';
33  
34 use constant JUBATUS_DEBUG => $ENV{JUBATUS_DEBUG};
35 use Log::Minimal qw/debugf infof warnf critf/; # $ENV{LM_DEBUG}
36 local $Log::Minimal::AUTODUMP = 1;
37 local $Log::Minimal::COLOR = 1;
38 local $Log::Minimal::LOG_LEVEL = "DEBUG";
39  
40 # It's used to show 'TypeException'
41 sub show {
42 my ($self, $arr_ref) = @_;
43 my ($value_str, $type_str) = @{$arr_ref};
44 warnf ("Type %s is expected, but %s is given", $type_str, $value_str) if (JUBATUS_DEBUG);
45 return;
46 }
47  
48 1;
49  
50 package Jubatus::Common::ValueException;
51  
52 use strict;
53 use warnings;
54 use utf8;
55 use autodie;
56  
57 use parent -norequire, 'Jubatus::Common::Exception';
58  
59 use constant JUBATUS_DEBUG => $ENV{JUBATUS_DEBUG};
60 use Log::Minimal qw/debugf infof warnf critf/; # $ENV{LM_DEBUG}
61 local $Log::Minimal::AUTODUMP = 1;
62 local $Log::Minimal::COLOR = 1;
63 local $Log::Minimal::LOG_LEVEL = "DEBUG";
64  
65 # It's used to show 'ValueException'
66 sub show {
67 my ($self, $arr_ref) = @_;
68 my ($value_str, $min, $max, $type_str) = @{$arr_ref};
69 if ($type_str) {
70 warnf ("%s value must be in (%d, %d), but %s is given", $value_str, $min, $max, $type_str) if (JUBATUS_DEBUG);
71 } else {
72 warnf ("Value %s is expected, but %s is given", $value_str, $type_str) if (JUBATUS_DEBUG);
73 }
74 return;
75 }
76  
77 1;
78  
79 package Jubatus::Common::ValuePairException;
80  
81 use strict;
82 use warnings;
83 use utf8;
84 use autodie;
85  
86 use parent -norequire, 'Jubatus::Common::Exception';
87  
88 use constant JUBATUS_DEBUG => $ENV{JUBATUS_DEBUG};
89 use Log::Minimal qw/debugf infof warnf critf/; # $ENV{LM_DEBUG}
90 local $Log::Minimal::AUTODUMP = 1;
91 local $Log::Minimal::COLOR = 1;
92 local $Log::Minimal::LOG_LEVEL = "DEBUG";
93  
94 # It's used to show 'ValuePairException'
95 sub show {
96 my ($self, $arr_ref) = @_;
97 my ($num1, $num2, $type_str) = @{$arr_ref};
98 if (($type_str) && ($num1) && ($num2)) {
99 warnf ("Two of %s values should have same number elements ,but (%d, %d) are given", $type_str, $num1, $num2) if (JUBATUS_DEBUG);
100 }
101 return;
102 }
103  
104 1;
105  
106 package Jubatus::Common::NotFoundException;
107  
108 use strict;
109 use warnings;
110 use utf8;
111 use autodie;
112  
113 use parent -norequire, 'Jubatus::Common::Exception';
114  
115 use constant JUBATUS_DEBUG => $ENV{JUBATUS_DEBUG};
116 use Log::Minimal qw/debugf infof warnf critf/; # $ENV{LM_DEBUG}
117 local $Log::Minimal::AUTODUMP = 1;
118 local $Log::Minimal::COLOR = 1;
119 local $Log::Minimal::LOG_LEVEL = "DEBUG";
120  
121 # It's used to show 'ValuePairException'
122 sub show {
123 my ($self, $arr_ref) = @_;
124 my ($data, $value_str, $type_str) = @{$arr_ref};
125 if (($data) && ($value_str) && ($type_str)) {
126 if (JUBATUS_DEBUG) {
127 warnf ("%s object should contain %s in data", $type_str, $value_str);
128 warnf ("data : $data");
129 }
130 }
131 return;
132 }
133  
134 1;
135  
136 package Jubatus::Common::Types;
137  
138 use strict;
139 use warnings;
140 use utf8;
141 use autodie;
142  
143 use B;
144 use Try::Lite;
145  
146 use constant JUBATUS_DEBUG => $ENV{JUBATUS_DEBUG};
147 use Log::Minimal qw/debugf infof warnf critf/; # $ENV{LM_DEBUG}
148 local $Log::Minimal::AUTODUMP = 1;
149 local $Log::Minimal::COLOR = 1;
150 local $Log::Minimal::LOG_LEVEL = "DEBUG";
151  
152 # Make check the matching of a label of $value object and a string of $type
153 sub estimate_type {
154 my ($value, $caller) = @_;
155 my $type; # a label of $value object and string of $type is matching
156 eval {
157 infof $caller if (JUBATUS_DEBUG);
158 try {
159 # Throw a exception when a label of $value object and a string value of $type aren't matching
160 my $flags = B::svref_2object( \$value )->FLAGS;
161 if (ref $value eq "ARRAY") {
162 $type = "Array";
163 } elsif (ref $value eq "HASH") {
164 $type = "Hash";
165 } elsif ($flags & B::SVf_NOK || $flags & B::SVp_NOK) {
166 $type = "Float";
167 } elsif ($flags & B::SVf_IOK || $flags & B::SVp_IOK) {
168 $type = "Integer";
169 } elsif ($flags & B::SVf_POK) {
170 $type = "String";
171 } elsif (ref $value ne "") {
172 $type = ref $value;
173 } else {
174 Jubatus::Common::TypeException->throw("data" => [ref $value, "Something type"]);
175 }
176 } (
177 # Catch the thrown error in the above lines
178 'Jubatus::Common::TypeException' => sub {
179 my $data = ${$@}->{data};
180 Jubatus::Common::TypeException->show($data);
181 },
182 ),
183 };
184 if ($@) { Jubatus::Common::Exception->show($@); } # Catch the re-thrown exception
185 return $type;
186 }
187  
188 # Make check the matching of a label of $value object and a string of $type
189 sub check_type {
190 my ($value, $type, $caller) = @_;
191 my $is_valid = 1; # a label of $value object and string of $type is matching
192 eval {
193 infof "$caller" if (JUBATUS_DEBUG);
194 try {
195 # Throw a exception when a label of $value object and a string value of $type aren't matching
196 if (ref $value eq $type) {
197 } else {
198 my $flags = B::svref_2object( \$value )->FLAGS;
199 if (($type eq "Integer") && ($flags & B::SVf_IOK || $flags & B::SVp_IOK)) {
200 } elsif (($type eq "Float") && ($flags & B::SVf_NOK || $flags & B::SVp_NOK)) {
201 } elsif (($type eq "String") && ($flags & B::SVf_POK)) {
202 } elsif (($type eq "Bool") && ((($flags & B::SVf_POK) && ("1" eq $value) || ("0" eq $value)) || (($flags & B::SVf_IOK || $flags & B::SVp_IOK) && (1 == $value) || (0 == $value)))) {
203 } elsif (($type eq "Array") && (ref $value eq "ARRAY")) {
204 } elsif (($type eq "Hash") && (ref $value eq "HASH")) {
205 } else {
206 $is_valid = 0;
207 Jubatus::Common::TypeException->throw("data" => [ref $value, $type],);
208 }
209 }
210 } (
211 # Catch the thrown error in the above lines
212 'Jubatus::Common::TypeException' => sub {
213 my $data = ${$@}->{data};
214 Jubatus::Common::TypeException->show($data);
215 },
216 );
217 };
218 if ($@) { $is_valid = 0; Jubatus::Common::Exception->show($@); } # Catch the re-thrown exception
219 return $is_valid;
220 }
221  
222 # Make check the matching of a label of $value object and each string value of type in $types
223 sub check_types {
224 my ($value, $types, $caller) = @_;
225 my $is_valid = 0;
226 infof $caller if (JUBATUS_DEBUG);
227 foreach my $type (@{$types}) {
228 # Call check_type() to compare $value and $type
229 $is_valid = check_type($value, $type, $caller);
230 if ($is_valid) {
231 last; # a label of $value object and string of $type is matching
232 }
233 }
234 return $is_valid;
235 }
236  
237 # Make check the matching of $value and $query_value
238 sub check_value {
239 my ($value, $query_value, $type, $caller) = @_;
240 my $is_valid = 1; # a label of $value object and string of $type is matching
241 eval {
242 infof $caller if (JUBATUS_DEBUG);
243 try {
244 # Throw a exception when a label of $value object and a string value of $type aren't matching
245 if (($type eq "Integer") && ($value == $query_value)){
246 } elsif (($type eq "Float") && ($value == $query_value)) {
247 } elsif (($type eq "String") && ($value eq $query_value)){
248 } elsif (($type eq "Bool") && ($value eq $query_value)){
249 } else {
250 $is_valid = 0;
251 Jubatus::Common::TypeException->throw("data" => [ref $value, $type],);
252 }
253 } (
254 # Catch the thrown error in the above lines
255 'Jubatus::Common::TypeException' => sub {
256 my $data = ${$@}->{data};
257 Jubatus::Common::TypeException->show($data);
258 },
259 );
260 };
261 if ($@) { $is_valid = 0; Jubatus::Common::Exception->show($@); } # Catch the re-thrown exception
262 return $is_valid;
263 }
264  
265 # Make check the matching one of value in a $values object and value of the target object
266 sub check_values {
267 my ($values, $query_value, $type, $caller) = @_;
268 my $is_valid = 1;
269 eval {
270 infof $caller if (JUBATUS_DEBUG);
271 try {
272 if (ref $values eq "ARRAY") {
273 foreach my $value (@{$values}) {
274 # Call check_type() to compare $value and $type
275 $is_valid = check_value($value, $query_value, $type, $caller);
276 last if ($is_valid); # a label of $value object and string of $type is matching
277 }
278 } elsif (ref $values eq "HASH") {
279 foreach my $key (keys @{$values}) {
280 my $value = $values->{$key};
281 # Call check_type() to compare $value and $type
282 $is_valid = check_value($value, $query_value, $type, $caller);
283 last if ($is_valid); # a label of $value object and string of $type is matching
284 }
285 } else {
286 $is_valid = 0;
287 Jubatus::Common::TypeException->throw("data" => [ref $values, "ARRAY or HASH"]);
288 }
289 } (
290 "Jubatus::Common::TypeException" => sub {
291 my $data = ${$@}->{data};
292 Jubatus::Common::TypeException->show($data);
293 },
294 );
295 };
296 if ($@) { $is_valid = 0; Jubatus::Common::Exception->show($@); } # Catch the re-thrown exception
297 return $is_valid;
298 }
299  
300 sub check_bound {
301 my ($value, $max, $min, $type, $caller) = @_;
302 my $is_valid = 1; # a label of $value object and string of $type is matching
303 eval {
304 infof $caller if (JUBATUS_DEBUG);
305 try {
306 # Throw a exception when a label of $value object and a string value of $type aren't matching
307 if (($type eq "Integer") && ($min <= $value) && ($value <= $max)) {
308 } elsif (($type eq "Bool") && (("1" eq $value) || ("0" eq $value))) {
309 } else {
310 $is_valid = 0;
311 Jubatus::Common::ValueException->throw("data" => [ref $value, $max, $min, $type]);
312 }
313 } (
314 # Catch the thrown error in the above lines
315 'Jubatus::Common::ValueException' => sub {
316 my $data = ${$@}->{data};
317 Jubatus::Common::ValueException->show($data);
318 },
319 );
320 };
321 if ($@) { $is_valid = 0; Jubatus::Common::Exception->show($@); } # Catch the re-thrown exception
322 return $is_valid;
323 }
324  
325 sub compare_element_num {
326 my ($value1, $value2, $type, $caller) = @_;
327 my $is_valid = 1; # a label of $value object and string of $type is matching
328 eval {
329 infof $caller if (JUBATUS_DEBUG);
330 try {
331 # Throw a exception when a label of $value object and a string value of $type aren't matching
332 if ($type eq "Array") {
333 unless ($#{$value1} == $#{$value2}) {
334 $is_valid = 0;
335 Jubatus::Common::ValuePairException->throw("data" => [$#{$value1}, $#{$value2}, $type]);
336 }
337 } elsif ($type eq "Hash") {
338 unless ( scalar(keys %{$value1}) == scalar(keys %{$value2})) {
339 $is_valid = 0;
340 Jubatus::Common::ValuePairException->throw("data" => [scalar(keys %{$value1}), scalar(keys %{$value2}), $type]);
341 }
342 } else {
343 $is_valid = 0;
344 Jubatus::Common::TypeException->throw("data" => [$type, "ARRAY or HASH"]);
345 }
346 } (
347 # Catch the thrown error in the above lines
348 'Jubatus::Common::ValuePairException' => sub {
349 my $data = ${$@}->{data};
350 Jubatus::Common::ValuePairException->show($data);
351 },
352 );
353 };
354 if ($@) { $is_valid = 0; Jubatus::Common::Exception->show($@); } # Catch the re-thrown exception
355 return $is_valid;
356 }
357  
358 1;
359  
360 package Jubatus::Common::TPrimitive;
361 # A parent class of the T* classes
362  
363 use strict;
364 use warnings;
365 use utf8;
366 use autodie;
367  
368 # Constructor of J::C::TPrimitive
369 # Second argument $types should be an array reference
370 sub new {
371 my ($class, $types) = @_;
372 my $hash = {
373 "types" => $types, # Use to check a type label of given object
374 };
375 bless $hash, $class;
376 }
377  
378 # Only check the matching of a label of $m object and one of string values in $self->{types} array reference
379 sub from_msgpack {
380 my ($self, $m) = @_;
381 my $is_valid = Jubatus::Common::Types::check_types($m, $self->{types}, (caller 0));
382 return $m;
383 }
384  
385 # Only check the matching of a label of $m object and one of string values in $self->{types} array reference
386 sub to_msgpack {
387 my ($self, $m) = @_;
388 my $is_valid = Jubatus::Common::Types::check_types($m, $self->{types}, (caller 0)."_".(caller 0)[3]);
389 return $m;
390 }
391  
392 1;
393  
394 package Jubatus::Common::TInt;
395 # Integer value classes
396  
397 use strict;
398 use warnings;
399 use utf8;
400 use autodie;
401  
402 use parent -norequire, 'Jubatus::Common::TPrimitive';
403  
404 # Constructor of J::C::TInt
405 sub new {
406 my ($class, $signed, $bytes) = @_;
407 my $hash = {};
408 $hash->{type} = "Integer";
409 if ($signed) { # signed integer
410 $hash->{max} = (1 << (8 * $bytes - 1)) - 1;
411 $hash->{min} = - (1 << (8 * $bytes - 1));
412 } else { # unsigned integer
413 $hash->{max} = (1 << (8 * $bytes)) - 1;
414 $hash->{min} = 0;
415 }
416 bless $hash, $class;
417 }
418  
419 sub from_msgpack {
420 my ($self, $m) = @_;
421 my $type = $self->{type};
422 # Check the matching of IV flags of $m object and the string value of $type
423 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
424 if ($is_valid_type) { # Check of the lower bound and the upper bound
425 my $is_valid_bound = Jubatus::Common::Types::check_bound($m, $self->{max}, $self->{min}, $type, (caller 0)."_".(caller 0)[3]);
426 }
427 return $m;
428 }
429  
430 sub to_msgpack {
431 my ($self, $m) = @_;
432 my $type = $self->{type};
433 # Check the matching of IV flags of $m object and the string value of $type
434 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
435 if ($is_valid_type) { # Check of the lower bound and the upper bound
436 my $is_valid_bound = Jubatus::Common::Types::check_bound($m, $self->{max}, $self->{min}, $type, (caller 0)."_".(caller 0)[3]);
437 }
438 return $m;
439 }
440  
441 1;
442  
443 package Jubatus::Common::TFloat;
444 # Float value classes
445  
446 use strict;
447 use warnings;
448 use utf8;
449 use autodie;
450  
451 use parent -norequire, 'Jubatus::Common::TPrimitive';
452  
453 # Constructor of J::C::TFloat
454 sub new {
455 my ($class) = @_;
456 my $hash = {};
457 $hash->{type} = "Float";
458 bless $hash, $class;
459 }
460  
461 # Only check the matching of NV flags of $m object and the string value of $type
462 sub from_msgpack {
463 my ($self, $m) = @_;
464 my $type = $self->{type};
465 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
466 return $m;
467 }
468  
469 # Only check the matching of NV flags of $m object and the string value of $type
470 sub to_msgpack {
471 my ($self, $m) = @_;
472 my $type = $self->{type};
473 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
474 return $m;
475 }
476  
477 1;
478  
479 package Jubatus::Common::TString;
480 # String value classes
481  
482 use strict;
483 use warnings;
484 use utf8;
485 use autodie;
486  
487 use parent -norequire, 'Jubatus::Common::TPrimitive';
488  
489 # Constructor of J::C::TString
490 sub new {
491 my ($class) = @_;
492 my $hash = {};
493 $hash->{type} = "String";
494 bless $hash, $class;
495 }
496  
497 # Only check the matching of PV flags of $m object and the string value of $type
498 sub from_msgpack {
499 my ($self, $m) = @_;
500 my $type = $self->{type};
501 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
502 return $m;
503 }
504  
505 # Only check the matching of PV flag of $m object and the string value of $type
506 sub to_msgpack {
507 my ($self, $m) = @_;
508 my $type = $self->{type};
509 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
510 return $m;
511 }
512  
513 1;
514  
515 package Jubatus::Common::TRaw;
516 # Raw value classes
517  
518 use strict;
519 use warnings;
520 use utf8;
521 use autodie;
522  
523 use parent -norequire, 'Jubatus::Common::TString';
524  
525 1;
526  
527 package Jubatus::Common::TBool;
528 # Boolean value classes
529  
530 use strict;
531 use warnings;
532 use utf8;
533 use autodie;
534  
535 use parent -norequire, 'Jubatus::Common::TPrimitive';
536  
537 # Constructor of J::C::TBool
538 sub new {
539 my ($class) = @_;
540 my $hash = {};
541 $hash->{type} = "Bool";
542 bless $hash, $class;
543 }
544  
545 sub from_msgpack {
546 my ($self, $m) = @_;
547 my $type = $self->{type};
548 # Check the matching of PV flags of $m object and the string value of $type
549 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
550 if ($is_valid_type) { # Check of $m is "0" as the false value or "1" as the true value
551 my $is_valid_bound = Jubatus::Common::Types::check_bound($m, 1, 0, $type, (caller 0)."_".(caller 0)[3]);
552 }
553 return $m;
554 }
555  
556 sub to_msgpack {
557 my ($self, $m) = @_;
558 my $type = $self->{type};
559 # Check the matching of PV flags of $m object and the string value of $type
560 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
561 if ($is_valid_type) { # Check of $m is "0" as the false value or "1" as the true value
562 my $is_valid_bound = Jubatus::Common::Types::check_bound($m, 1, 0, $type, (caller 0)."_".(caller 0)[3]);
563 }
564 return $m;
565 }
566  
567 1;
568  
569 package Jubatus::Common::TDatum;
570 # Datum value classes
571  
572 use strict;
573 use warnings;
574 use utf8;
575 use autodie;
576  
577 use parent -norequire, 'Jubatus::Common::Datum';
578  
579 # sub new {}
580 # sub from_msgpack() {}
581  
582 sub from_msgpack {
583 my ($self, $m) = @_;
584 my $type = $self->{type}; # = "Jubatus::Common::Datum" which set on new()
585 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Array", (caller 0)."_".(caller 0)[3]);
586 # Return an packed value of $m using message pack protocol
587 return "$type"->from_msgpack($m);
588 }
589  
590 # Check the matching of a label of $m object and the string value of $type
591 sub to_msgpack {
592 my ($self, $m) = @_;
593 my $type = $self->{type}; # = "Jubatus::Common::Datum" which set on new()
594 my $is_valid_type = Jubatus::Common::Types::check_type($m, $type, (caller 0)."_".(caller 0)[3]);
595 # Return an packed value of $m using message pack protocol
596 my $data = $m->to_msgpack();
597 return $data;
598 }
599  
600 1;
601  
602 package Jubatus::Common::TNullable;
603 # Nullable value classes
604  
605 use strict;
606 use warnings;
607 use utf8;
608 use autodie;
609  
610 use parent -norequire, 'Jubatus::Common::TPrimitive';
611  
612 # Constructor of J::C::TNullable
613 # Second argument must be a type string of an object which will use to call the methods of this class
614 sub new {
615 my ($class, $type) = @_;
616 my $hash = {};
617 $hash->{type} = $type;
618 bless $hash, $class;
619 }
620  
621 # Call from_msgpack() which belong to Jubatus::Common::$type
622 sub from_msgpack {
623 my ($self, $m) = @_;
624 my $type = $self->{type};
625 my $result = undef;
626 if (!(defined $m)) {
627 } else {
628 $result = $type->from_msgpack($m);
629 }
630 return $result;
631 }
632 # Call to_msgpack() which belong to Jubatus::Common::$type
633 sub to_msgpack {
634 my ($self, $m) = @_;
635 my $type = $self->{type};
636 my $result = undef;
637 if (!(defined $m)) {
638 } else {
639 $result = $type->to_msgpack($m);
640 }
641 return $result;
642 }
643  
644 1;
645  
646 package Jubatus::Common::TList;
647 # List value classes
648  
649 use strict;
650 use warnings;
651 use utf8;
652 use autodie;
653  
654 use parent -norequire, 'Jubatus::Common::TPrimitive';
655  
656 # Constructor of J::C::TList
657 # Second argument must be a type string of an object which will use to call the methods of this class
658 sub new {
659 my ($class, $type) = @_;
660 my $hash = {};
661 $hash->{type} = $type;
662 bless $hash, $class;
663 }
664  
665 # Call from_msgpack() which belong to Jubatus::Common::$type
666 sub from_msgpack {
667 my ($self, $m) = @_;
668 my $type = $self->{type};
669 my $result = [];
670 # Check a data type of $m to push the data to an array reference
671 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Array", (caller 0)."_".(caller 0)[3]);
672 if ($is_valid_type) { # If $m is Array reference value
673 foreach my $v (@{$m}) {
674 my $tmp = $type->from_msgpack($v);
675 push @{$result}, $tmp;
676 }
677 }
678 return $result; # Return an array reference
679 }
680  
681 # Call to_msgpack() which belong to Jubatus::Common::$type
682 sub to_msgpack {
683 my ($self, $m) = @_;
684 my $type = $self->{type};
685 my $result = [];
686 # Check a data type of $m to push the data to an array reference
687 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Array", (caller 0)."_".(caller 0)[3]);
688 if ($is_valid_type) { # If $m is Array reference value
689 foreach my $v (@{$m}) {
690 my $tmp = $type->to_msgpack($v);
691 push @{$result}, $tmp;
692 }
693 }
694 return $result; # Return an array reference
695 }
696  
697 1;
698  
699 package Jubatus::Common::TMap;
700 # Map value classes
701  
702 use strict;
703 use warnings;
704 use utf8;
705 use autodie;
706  
707 use parent -norequire, 'Jubatus::Common::TPrimitive';
708  
709 # Constructor of J::C::TMap
710 # Second argument must be a type string of an object which will use to call the methods of this class
711 sub new {
712 my ($class, $key_type, $value_type) = @_;
713 my $hash = {};
714 $hash->{key_type} = $key_type;
715 $hash->{value_type} = $value_type;
716 bless $hash, $class;
717 }
718  
719 # Call from_msgpack() which belong to Jubatus::Common::$type
720 sub from_msgpack {
721 my ($self, $m) = @_;
722 my $key_type = $self->{key_type};
723 my $value_type = $self->{value_type};
724 my $result = {};
725 # Check a data type of $m to push the data to an array reference
726 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Hash", (caller 0)."_".(caller 0)[3]);
727 if ($is_valid_type) { # If $m is hash reference value
728 foreach my $key (keys %{$m}) {
729 $result->{$key_type->from_msgpack($key)} = $value_type->from_msgpack($m->{$key});
730 }
731 }
732 return $result; # Return an hash reference
733 }
734  
735 # Call to_msgpack() which belong to Jubatus::Common::$type
736 sub to_msgpack {
737 my ($self, $m) = @_;
738 my $key_type = $self->{key_type};
739 my $value_type = $self->{value_type};
740 my $result = {};
741 # Check a data type of $m to push the data to an array reference
742 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Hash", (caller 0)."_".(caller 0)[3]);
743 if ($is_valid_type) { # If $m is hash reference value
744 foreach my $key (keys %{$m}) {
745 $result->{$key_type->to_msgpack($key)} = $value_type->to_msgpack($m->{$key});
746 }
747 }
748 return $result; # Return an hash reference
749 }
750  
751 1;
752  
753 package Jubatus::Common::TTuple;
754 # Map value classes
755  
756 use strict;
757 use warnings;
758 use utf8;
759 use autodie;
760  
761 use parent -norequire, 'Jubatus::Common::TPrimitive';
762  
763 # Constructor of J::C::TTuple
764 # Second argument must be an array reference of the type strings
765 sub new {
766 my ($class, $types) = @_;
767 my $hash = {};
768 $hash->{types} = $types if Jubatus::Common::Types::check_type($types, "Array", (caller 0)."_".(caller 0)[3]);
769 bless $hash, $class;
770 }
771  
772 sub check_tuple {
773 my ($self, $m) = @_;
774 my $is_valid = 0;
775 my $types = $self->{types};
776 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Array", (caller 0)."_".(caller 0)[3]);
777 if ($is_valid_type) {
778 $is_valid = Jubatus::Common::Types::compare_element_num($m, $types, "Array", (caller 0)."_".(caller 0)[3]);
779 }
780 return $is_valid;
781 }
782  
783 # Call from_msgpack() which belong to Jubatus::Common::$type
784 sub from_msgpack {
785 my ($self, $m) = @_;
786 my $types = $self->{types};
787 # Check a data type and matching of the elements numbers of $m and $type
788 my $is_valid_tuple = $self->check_tuple($m);
789 my $result = [];
790 if ($is_valid_tuple) {
791 for (my $i = 0; $i <= $#$m; $i++) {
792 my $type = $types->[$i];
793 my $value = $m->[$i];
794 push @{$result}, $type->from_msgpack($value);
795 }
796 }
797 return $result; # Return an hash reference
798 }
799  
800 # Call to_msgpack() which belong to Jubatus::Common::$type
801 sub to_msgpack {
802 my ($self, $m) = @_;
803 my $types = $self->{types};
804 # Check a data type and matching of the elements numbers of $m and $type
805 my $is_valid_tuple = $self->check_tuple($m);
806 my $result = [];
807 if ($is_valid_tuple) {
808 for (my $i = 0; $i <= $#$m; $i++) {
809 my $type = $types->[$i];
810 my $value = $m->[$i];
811 push @{$result}, $type->to_msgpack($value);
812 }
813 }
814 return $result; # Return an hash reference
815 }
816  
817 1;
818  
819 package Jubatus::Common::TObject;
820  
821 use strict;
822 use warnings;
823 use utf8;
824 use autodie;
825  
826 use parent -norequire, 'Jubatus::Common::TPrimitive';
827  
828 # Non check returning
829 sub from_msgpack {
830 my ($self, $m) = @_;
831 return $m;
832 }
833  
834 # Non check returning
835 sub to_msgpack {
836 my ($self, $m) = @_;
837 return $m;
838 }
839  
840 1;
841  
842  
843 package Jubatus::Common::TEnum;
844 # Enum value classes
845  
846 use strict;
847 use warnings;
848 use utf8;
849 use autodie;
850  
851 use parent -norequire, 'Jubatus::Common::TPrimitive';
852  
853 use Try::Lite;
854  
855 # Constructor of J::C::TEnum
856 sub new {
857 my ($class, $values) = @_;
858 my $hash = {};
859 my $is_valid_type = Jubatus::Common::Types::check_type($values, "Array", (caller 0)."_".(caller 0)[3]);
860 if ($is_valid_type) {
861 foreach my $value (@{$values}) {
862 my $is_valid_value = Jubatus::Common::Types::check_type($value, "Integer", (caller 0)."_".(caller 0)[3][3]);
863 last unless ($is_valid_value);
864 }
865 $hash->{values} = $values;
866 }
867 bless $hash, $class;
868 }
869  
870 sub from_msgpack {
871 my ($self, $m) = @_;
872 my $values = $self->{values};
873 # Check the matching of IV flags of $m object and the string value of $type
874 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Integer", (caller 0)."_".(caller 0)[3]);
875 my $is_found = 0;
876 if ($is_valid_type) {
877 $is_found = Jubatus::Common::Types::check_values($values, $m ,"Integer", (caller 0)."_".(caller 0)[3]);
878 }
879 return $m;
880 }
881  
882 sub to_msgpack {
883 my ($self, $m) = @_;
884 my $values = $self->{values};
885 # Check the matching of IV flags of $m object and the string value of $type
886 my $is_valid_type = Jubatus::Common::Types::check_type($m, "Integer", (caller 0)."_".(caller 0)[3]);
887 my $is_found = 0;
888 if ($is_valid_type) {
889 $is_found = Jubatus::Common::Types::check_values($values, $m ,"Integer", (caller 0)."_".(caller 0)[3]);
890 }
891 return $m;
892 }
893  
894 1;
895  
896 package Jubatus::Common::TUserDef;
897 # TUserDef value classes
898  
899 use strict;
900 use warnings;
901 use utf8;
902 use autodie;
903  
904 use parent -norequire, 'Jubatus::Common::TPrimitive';
905  
906 use Try::Lite;
907  
908 # Constructor of J::C::TUserDef
909 sub new {
910 my ($class, $type) = @_;
911 my $hash = {};
912 $hash->{type} = $type;
913 bless $hash, $class;
914 }
915  
916 sub from_msgpack {
917 my ($self, $m) = @_;
918 my $type = $self->{type};
919 my $data = $m;
920 my $sub_name = (caller 0)."_".(caller 0)[3];
921 try {
922 if (Jubatus::Common::Types::check_type($m, "Array", $sub_name)) {
923 $data = $type->from_msgpack($m);
924 } elsif (Jubatus::Common::Types::check_type($m, ref $type, $sub_name)) {
925 $data = $type->from_msgpack($m);
926 } else {
927 Jubatus::Common::NotFoundException->throw("data" => [ref $m, $type]);
928 }
929 } (
930 # Catch the thrown error in the above lines
931 'Jubatus::Common::NotFoundException' => sub {
932 my $data = ${$@}->{data};
933 Jubatus::Common::NotFoundException->show($data);
934 },
935 );
936 return $data;
937 }
938  
939 sub to_msgpack {
940 my ($self, $m) = @_;
941 my $type = $self->{type};
942 my $data = $m;
943 my $sub_name = (caller 0)."_".(caller 0)[3];
944 try {
945 if (Jubatus::Common::Types::check_type($m, "Array", $sub_name)) {
946 $data = $type->get_type()->to_msgpack($m);
947 } elsif (Jubatus::Common::Types::check_type($m, ref $type, $sub_name)) {
948 $data = $m->to_msgpack();
949 } else {
950 Jubatus::Common::NotFoundException->throw("data" => [ref $m, $type]);
951 }
952 } (
953 # Catch the thrown error in the above lines
954 'Jubatus::Common::NotFoundException' => sub {
955 my $data = ${$@}->{data};
956 Jubatus::Common::NotFoundException->show($data);
957 },
958 );
959 return $data;
960 }
961  
962 1;
963  
964 __END__