Serialize/Role/Base.pm

filter: Statementstmt 99.5% 206 / 207 Branchbran 87.5% 77 / 88 Conditioncond 71.4% 10 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 95.4% 42 / 44 Podpod 87.5% 7 / 8 totaltotal 93.5% 346 / 370 CC 61 SCAR 16.2
CC
61
Cov
95%
__ANON__
11.2
_build_output_types
11.1
BUILD
10.1
CRAP 5.0
« Serialize/Util.pm ↑ Summary Serialize/Encode/json.pm »
1 package Data::Record::Serialize::Role::Base;
2  
3 # ABSTRACT: Base Role for Data::Record::Serialize
4  
5 21 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 our $VERSION = '2.05';
9  
10 21 ▶use Data::Record::Serialize::Error { errors => [ 'fields', 'types' ] }, -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 21 ▶use Data::Record::Serialize::Util -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 21 ▶use Types::Standard qw[ ArrayRef CodeRef CycleTuple HashRef Enum Str Bool is_HashRef Maybe ];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use Data::Record::Serialize::Types qw( SerializeType );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 21 ▶use Ref::Util qw( is_coderef is_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 21 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 21 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39 has types => (
40 is => 'rwp',
41 isa => ( HashRef [SerializeType] | CycleTuple [ Str, SerializeType ] )
42 , # need parens for perl <= 5.12.5
43 predicate => 1,
44 119 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 119 $_[0]->clear_type_index;
46 119 $_[0]->clear_output_types;
47 },
48 21 );
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62 21 has default_type => (
63 is => 'ro',
64 isa => SerializeType,
65 predicate => 1,
66 );
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78 has fields => (
79 is => 'rwp',
80 isa => ( ArrayRef [Str] | Enum ['all'] ), # need parens for perl <= 5.12.5
81 predicate => 1,
82 clearer => 1,
83 92 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 92 $_[0]->_clear_fieldh;
85 92 $_[0]->clear_output_types;
86 92 $_[0]->clear_output_fields;
87 },
88 21 );
89  
90  
91 # for quick lookup of field names
92 has _fieldh => (
93 is => 'lazy',
94 init_arg => undef,
95 clearer => 1,
96 41 ▶ builder => sub {
Subroutine _build__fieldh: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
97 41 my $self = shift;
98 41 my %fieldh;
99 41 @fieldh{ @{ $self->fields } } = ();
100 41 return \%fieldh;
101 },
102 21 );
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114 has output_fields => (
115 is => 'lazy',
116 trigger => 1,
117 clearer => 1,
118 1 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.5
119 1 my $self = shift;
120 1 ▶ [ map { $self->rename_fields->{$_} // $_ } @{ $self->fields } ];
Condition50%3/6
$self->rename_fields->{$_} // $_
l!l&&r!l&&!r
050
$self->rename_fields->{$_} // $_
l!l&&r!l&&!r
1440
MC/DC0%0/2
$self->rename_fields->{$_} // $_
$self->rename_fields->{$_} $_
MC/DC0%0/2
$self->rename_fields->{$_} // $_
$self->rename_fields->{$_} $_
Truth table33%1/3
A // B
A= $self->rename_fields->{$_}B= $_
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $self->rename_fields->{$_}B= $_
ABresult
1X1
011
000
121 },
122 21 init_arg => undef,
123 );
124  
125 # something for other roles to wrap.
126 0 ▶sub _trigger_output_fields { }
Subroutine _trigger_output_fields: not called CC=1 SCAR=0.0
127  
128 21 has _run_setup => (
129 is => 'rwp',
130 isa => Bool,
131 init_args => undef,
132 default => 1,
133 );
134  
135  
136 # have we initialized types? can't simply use $self->has_types, as
137 # the caller may have provided some.
138 21 has _have_initialized_types => (
139 is => 'rwp',
140 init_arg => undef,
141 isa => Bool,
142 default => 0,
143 );
144  
145 has _boolify => (
146 is => 'lazy',
147 isa => Bool,
148 init_arg => undef,
149 78 ▶ builder => sub { $_[0]->_can_bool || $_[0]->_convert_boolean_to_int },
Subroutine _build__boolify: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_can_bool or $_[0]->_convert_boolean_to_int 25 53 78
150 21 );
151  
152 21 has _convert_boolean_to_int => (
153 is => 'rwp',
154 default => 0,
155 );
156  
157 has _can_bool => (
158 is => 'lazy',
159 isa => Bool,
160 init_arg => undef,
161 78 ▶ builder => sub { !!$_[0]->can( 'to_bool' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
162 21 );
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179 79 ▶sub _build_field_list_with_type {
Subroutine _build_field_list_with_type: called CC=5 SCAR=16.1
180 79 my ( $self, $list_spec, $type, $error_label ) = @_;
181  
182 79 my $fieldh = $self->_fieldh;
183 79 my $list = do {
184 79 ▶ if ( is_coderef( $list_spec ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_coderef($list_spec)) { } 3 76 79
elsif (Ref::Util::XS::is_arrayref($list_spec)) { } 15 61 76
185 3 ( ArrayRef [Str] )->assert_return( $list_spec->( $self ) );
186 }
187 elsif ( is_arrayref( $list_spec ) ) {
188 15 populate_set( $self->type_index->[$type], $list_spec );
189 }
190 else {
191 # want all of the fields. actually just want the ones that will be output,
192 # otherwise the check below will fail.
193 61 ▶ [ grep { exists $fieldh->{$_} } $list_spec ? @{ $self->type_index->[$type] } : () ];
Branch: 1 branch
BranchTrueFalseTotal
$list_spec ? : 48 13 61
194 }
195 };
196  
197 # this check is to help catch typos by users
198 79 my @not_field = grep { !exists $fieldh->{$_} } @{$list};
199 79 ▶ error( 'fields', "unknown $error_label fields: " . join( ', ', @not_field ) )
Branch: 1 branch
BranchTrueFalseTotal
if @not_field 6 73 79
200 if @not_field;
201  
202 73 return $list;
203 }
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239 21 has [ 'nullify', 'numify', 'stringify' ] => (
240 is => 'rw',
241 isa => ( ArrayRef [Str] | CodeRef | Bool ), # need parens for perl <= 5.12.5
242 predicate => 1,
243 trigger => 1,
244 );
245  
246 12 ▶sub _trigger_nullify { $_[0]->_clear_nullified }
Subroutine _trigger_nullify: called CC=1 SCAR=0.0
247 17 ▶sub _trigger_numify { $_[0]->_clear_numified }
Subroutine _trigger_numify: called CC=1 SCAR=0.0
248 19 ▶sub _trigger_stringify { $_[0]->_clear_stringified }
Subroutine _trigger_stringify: called CC=1 SCAR=0.0
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267 11 ▶sub nullified {
Subroutine nullified: called CC=2 SCAR=6.9
Pod documented
268 11 my $self = shift;
269 11 ▶ return [ $self->has_fields ? @{ $self->_nullified } : () ];
Branch: 1 branch
BranchTrueFalseTotal
$self->has_fields ? : 9 2 11
270 }
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290 11 ▶sub numified {
Subroutine numified: called CC=2 SCAR=6.9
Pod documented
291 11 my $self = shift;
292 11 ▶ return [ $self->has_fields ? @{ $self->_numified } : () ];
Branch: 1 branch
BranchTrueFalseTotal
$self->has_fields ? : 9 2 11
293 }
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312 11 ▶sub stringified {
Subroutine stringified: called CC=2 SCAR=6.9
Pod documented
313 11 my $self = shift;
314 11 ▶ return [ $self->has_fields ? @{ $self->_stringified } : () ];
Branch: 1 branch
BranchTrueFalseTotal
$self->has_fields ? : 9 2 11
315 }
316  
317  
318 21 has [ '_nullified', '_numified', '_stringified' ] => (
319 is => 'lazy',
320 isa => ArrayRef [Str],
321 clearer => 1,
322 predicate => 1,
323 init_arg => undef,
324 builder => 1,
325 );
326  
327 84 ▶sub _build__nullified {
Subroutine _build__nullified: called CC=2 SCAR=6.9
328 84 my $self = shift;
329 84 ▶ return $self->has_nullify
Branch: 1 branch
BranchTrueFalseTotal
$self->has_nullify ? : 13 71 84
330 ? $self->_build_field_list_with_type( $self->nullify, ANY, 'nullify' )
331 : [];
332 }
333  
334 83 ▶sub _build__numified {
Subroutine _build__numified: called CC=2 SCAR=6.9
335 83 my $self = shift;
336 83 ▶ return $self->has_numify
Branch: 1 branch
BranchTrueFalseTotal
$self->has_numify ? : 33 50 83
337 ? $self->_build_field_list_with_type( $self->numify, NUMBER, 'numify' )
338 : [];
339 }
340  
341 82 ▶sub _build__stringified {
Subroutine _build__stringified: called CC=2 SCAR=6.9
342 82 my $self = shift;
343 82 ▶ return $self->has_stringify
Branch: 1 branch
BranchTrueFalseTotal
$self->has_stringify ? : 33 49 82
344 ? $self->_build_field_list_with_type( $self->stringify, STRING, 'stringify' )
345 : [];
346 }
347  
348  
349  
350  
351  
352  
353  
354  
355  
356  
357 1 ▶sub string_fields { $_[0]->type_index->[STRING] }
Subroutine string_fields: called CC=1 SCAR=0.0
Pod documented
358  
359  
360  
361  
362  
363  
364  
365  
366  
367  
368 70 ▶sub numeric_fields { $_[0]->type_index->[NUMBER] }
Subroutine numeric_fields: called CC=1 SCAR=0.0
Pod documented
369  
370  
371  
372  
373  
374  
375  
376  
377  
378  
379 188 ▶sub boolean_fields { $_[0]->type_index->[BOOLEAN] }
Subroutine boolean_fields: called CC=1 SCAR=0.0
Pod documented
380  
381  
382  
383  
384  
385  
386  
387  
388  
389  
390  
391  
392  
393  
394  
395  
396  
397  
398  
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412  
413  
414  
415 has type_index => (
416 is => 'lazy',
417 init_arg => undef,
418 clearer => 1,
419 89 ▶ builder => sub {
Subroutine _build_type_index: called CC=2 SCAR=7.1
420 89 my $self = shift;
421 89 ▶ error( 'types', 'no types for fields are available' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_types 0 89 89
422 unless $self->has_types;
423 89 index_types( $self->types );
424 },
425 21 );
426  
427  
428  
429  
430  
431  
432  
433  
434  
435  
436  
437 21 has output_types => (
438 is => 'lazy',
439 init_arg => undef,
440 clearer => 1,
441 trigger => 1,
442 );
443  
444 85 ▶sub _build_output_types {
Subroutine _build_output_types: called CC=11 SCAR=24.0
445 85 my $self = shift;
446 85 my %types;
447  
448 return
449 85 ▶ unless $self->has_types;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_types 0 85 85
450  
451 85 my @int_fields = grep { defined $self->types->{$_} } @{ $self->fields };
452 85 @types{@int_fields} = @{ $self->types }{@int_fields};
453  
454 85 ▶ unless ( $self->_encoder_has_type( BOOLEAN ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_encoder_has_type(6)) 85 0 85
455 85 $types{$_} = T_INTEGER for @{ $self->boolean_fields };
456 85 $self->_set__convert_boolean_to_int( 1 );
457 }
458  
459 85 ▶ unless ( $self->_encoder_has_type( INTEGER ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_encoder_has_type(1)) 68 17 85
460 68 $types{$_} = T_NUMBER for @{ $self->numeric_fields };
461 }
462  
463 85 ▶ if ( my $map_types = $self->_map_types ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $map_types = $self->_map_types) 19 66 85
464 19 for my $field ( keys %types ) {
465 46 my $type = $types{$field};
466 46 ▶ next unless exists $map_types->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map_types->{$type} 0 46 46
467 46 $types{$field} = $map_types->{$type};
468 }
469 }
470  
471 85 for my $key ( keys %types ) {
472 177 ▶ my $rename = $self->rename_fields->{$key}
Branch: 1 branch
BranchTrueFalseTotal
my $rename = $self->rename_fields->{$key} or next 2 175 177
473 or next;
474  
475 2 $types{$rename} = delete $types{$key};
476 }
477  
478 85 \%types;
479 }
480  
481 # something for other roles to wrap.
482 0 ▶sub _trigger_output_types { }
Subroutine _trigger_output_types: not called CC=1 SCAR=0.0
483  
484  
485 170 ▶sub _encoder_has_type {
Subroutine _encoder_has_type: called CC=2 SCAR=6.9
486 170 my ( $self, $type ) = @_;
487 170 ▶ any { is_type( $_, $type ) } keys %{ $self->_map_types // {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
$self->_map_types // {}
l!l
38132
MC/DC100%1/1
$self->_map_types // {}
$self->_map_types
Truth table100%2/2
A
A= $self->_map_types
Aresult
11
00
488 }
489  
490  
491  
492  
493  
494  
495  
496  
497 21 has format_fields => (
498 is => 'ro',
499 isa => HashRef [ Str | CodeRef ],
500 );
501  
502  
503  
504  
505  
506  
507  
508 21 has format_types => (
509 is => 'ro',
510 isa => HashRef [ Str | CodeRef ],
511 );
512  
513  
514  
515  
516  
517  
518  
519  
520 has rename_fields => (
521 is => 'ro',
522 isa => HashRef [Str],
523 93 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
524 93 ▶ return $_[0] unless is_HashRef( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless is_HashRef($_[0]) 0 93 93
525  
526 # remove renames which do nothing
527 93 my %rename = %{ $_[0] };
528 93 delete @rename{ grep { $rename{$_} eq $_ } keys %rename };
529 93 return \%rename;
530 },
531 90 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
532 3 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
533 3 $_[0]->clear_output_types;
534 },
535 21 );
536  
537  
538  
539  
540  
541  
542  
543  
544  
545 21 has format => (
546 is => 'ro',
547 isa => Bool,
548 default => 1,
549 );
550  
551 has _format => (
552 is => 'rwp',
553 lazy => 1,
554 78 ▶ default => sub {
Subroutine __ANON__: called CC=11 SCAR=24.2
555 78 my $self = shift;
556  
557 78 ▶ if ( $self->format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format) 78 0 78
558 78 my %format;
559  
560 # first consider types; they'll be overridden by per field
561 # formats in the next step.
562 78 ▶ if ( $self->format_types && $self->types ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format_types and $self->types) 3 75 78
Condition66%2/3
$self->format_types and $self->types
!ll&&!rl&&r
7503
MC/DC50%1/2
$self->format_types and $self->types
$self->format_types $self->types
Truth table66%2/3
A and B
A= $self->format_typesB= $self->types
ABresult
0X0
100
111
563  
564 3 for my $field ( @{ $self->fields } ) {
565  
566 10 ▶ my $type = $self->types->{$field}
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->types->{$field} or next 10 0 10
567 or next;
568  
569 10 ▶ my $format = $self->format_types->{$type}
Branch: 1 branch
BranchTrueFalseTotal
my $format = $self->format_types->{$type} or next 10 0 10
570 or next;
571  
572 10 $format{$field} = $format;
573 }
574 }
575  
576 78 ▶ if ( $self->format_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format_fields) 2 76 78
577 2 for my $field ( @{ $self->fields } ) {
578 8 ▶ my $format = $self->format_fields->{$field}
Branch: 1 branch
BranchTrueFalseTotal
my $format = $self->format_fields->{$field} or next 6 2 8
579 or next;
580  
581 6 $format{$field} = $format;
582 }
583 }
584  
585 78 ▶ return \%format
Branch: 1 branch
BranchTrueFalseTotal
if keys %format 4 74 78
586 if keys %format;
587 }
588  
589 74 return;
590 },
591 21 init_arg => undef,
592 );
593  
594  
595  
596  
597  
598  
599 89 ▶sub BUILD {
Subroutine BUILD: called CC=10 SCAR=23.1
Pod undocumented
600 89 my $self = shift;
601  
602 # if types is passed, set fields if it's not set.
603 # convert types to hash if it's an array
604 89 ▶ if ( $self->has_types ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_types) 35 54 89
605 35 my $types = $self->types;
606  
607 35 ▶ if ( 'HASH' eq ref $types ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq ref $types) { } 34 1 35
elsif ('ARRAY' eq ref $types) { } 1 0 1
608 34 ▶ $self->_set_fields( [ keys %{$types} ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_fields 9 25 34
609 unless $self->has_fields;
610 }
611 elsif ( 'ARRAY' eq ref $types ) {
612 1 $self->_set_types( { @{$types} } );
613  
614 1 ▶ if ( !$self->has_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_fields) 1 0 1
615 1 my @fields;
616 # pull off "keys"
617 1 ▶ push @fields, ( shift @$types, shift @$types )[0] while @$types;
Branch: 1 branch
BranchTrueFalseTotal
@$types and do { push @fields, (shift @$types, shift @$types)[0] } 3 1 4
618 1 $self->_set_fields( \@fields );
619 }
620 }
621 else {
622 0 error( '::attribute::value', 'internal error' );
623 }
624 }
625  
626 89 ▶ if ( $self->has_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_fields) 56 33 89
627  
628 56 ▶ if ( ref $self->fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->fields) { } 51 5 56
629 # in this specific case everything can be done before the first
630 # record is read. this is kind of overkill, but at least one
631 # test depended upon being able to determine types prior
632 # to sending the first record, so need to do this here rather
633 # than in Default::setup
634 51 ▶ $self->_set_types_from_default
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_default_type 4 47 51
635 if $self->has_default_type;
636 }
637  
638 # if fields eq 'all', clear out the attribute so that it will get
639 # filled in when the first record is sent.
640 else {
641 5 $self->clear_fields;
642 }
643 }
644  
645 89 return;
646 }
647  
648 76 ▶sub _set_types_from_record {
Subroutine _set_types_from_record: called CC=7 SCAR=19.5
649 76 my ( $self, $data ) = @_;
650  
651 76 ▶ return if $self->_have_initialized_types;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_have_initialized_types 0 76 76
652  
653 76 ▶ my $types = $self->has_types ? $self->types : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->has_types ? : 27 49 76
654  
655 76 for my $field ( grep !defined $types->{$_}, @{ $self->fields } ) {
656 113 my $value = $data->{$field};
657 113 ▶ my $def = Scalar::Util::looks_like_number( $value ) ? T_NUMBER : T_STRING;
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($value) ? : 63 50 113
658  
659 113 ▶ $def = T_INTEGER
Branch: 1 branch
BranchTrueFalseTotal
if $def eq "N" and POSIX::floor($value) == POSIX::ceil($value) 45 68 113
Condition100%3/3
$def eq "N" and POSIX::floor($value) == POSIX::ceil($value)
!ll&&!rl&&r
501845
MC/DC100%2/2
$def eq "N" and POSIX::floor($value) == POSIX::ceil($value)
$def eq "N" POSIX::floor($value) == POSIX::ceil($value)
Truth table100%3/3
A and B
A= $def eq "N"B= POSIX::floor($value) == POSIX::ceil($value)
ABresult
0X0
100
111
660 if $def eq T_NUMBER
661 && POSIX::floor( $value ) == POSIX::ceil( $value );
662  
663 113 $types->{$field} = $def;
664 }
665  
666 76 $self->_set_types( $types );
667 76 $self->_set__have_initialized_types( 1 );
668 }
669  
670 9 ▶sub _set_types_from_default {
Subroutine _set_types_from_default: called CC=4 SCAR=13.9
671 9 my $self = shift;
672  
673 9 ▶ return if $self->_have_initialized_types;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_have_initialized_types 2 7 9
674  
675 7 ▶ my $types = $self->has_types ? $self->types : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->has_types ? : 5 2 7
676  
677 7 $types->{$_} = $self->default_type for grep { !defined $types->{$_} } @{ $self->fields };
678  
679 7 $self->_set_types( $types );
680 7 $self->_set__have_initialized_types( 1 );
681 }
682  
683  
684  
685  
686  
687  
688  
689  
690  
691  
692  
693  
694  
695  
696  
697 82 ▶sub setup_from_record {
Subroutine setup_from_record: called CC=5 SCAR=16.1
Pod documented
698 82 my ( $self, $data ) = @_;
699  
700 # if fields has not been set yet, set it to the names in the data
701 82 ▶ $self->_set_fields( [ keys %$data ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_fields 36 46 82
702 unless $self->has_fields;
703  
704 # make sure there are no duplicate output fields
705 82 my %dups;
706 82 ▶ $dups{$_}++ && error( fields => "duplicate output field: $_" ) for @{ $self->fields };
Branch: 1 branch
BranchTrueFalseTotal
$dups{$_}++ and error("fields", "duplicate output field: $_") 1 170 171
707  
708 81 ▶ if ( $self->has_default_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_default_type) { } 5 76 81
709 5 $self->_set_types_from_default;
710 }
711 else {
712 76 $self->_set_types_from_record( $data );
713 }
714  
715 # trigger building of output_types, which also remaps types. ick.
716 81 $self->output_types;
717  
718 }
719  
720 21 1;
721  
722 #
723 # This file is part of Data-Record-Serialize
724 #
725 # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
726 #
727 # This is free software, licensed under:
728 #
729 # The GNU General Public License, Version 3, June 2007
730 #
731  
732 __END__
« Serialize/Util.pm ↑ Summary Serialize/Encode/json.pm »