TOON/Encoder.pm

filter: Statementstmt 90.5% 173 / 191 Branchbran 73.4% 72 / 98 Conditioncond 63.6% 14 / 22 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 2 totaltotal 82.0% 278 / 339 CC 69 SCAR 26.3
CC
69
Cov
83%
_encode_object_with_array
23.5
_needs_quoting
19.8
_encode_array
16.8
CRAP 13.9
« TOON/Decoder.pm ↑ Summary TOON/Validator.pm »
1 package Data::TOON::Encoder;
2 10 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 ▶sub new {
Subroutine new: called CC=6 SCAR=18.2
Pod undocumented
7 33 my ($class, %opts) = @_;
8 return bless {
9 indent => $opts{indent} || 2,
10 delimiter => $opts{delimiter} || ',',
11 strict => $opts{strict} // 1,
12 depth => 0,
13 max_depth => $opts{max_depth} // 100,
14 seen => {}, # Track object references for circular detection
15 33 ▶ column_priority => $opts{column_priority} || [], # Column names to prioritize
Condition80%8/10
$opts{'indent'} || 2
l!l
033
$opts{'delimiter'} || ','
l!l
429
$opts{"strict"} // 1
l!l
033
$opts{"max_depth"} // 100
l!l
231
$opts{'column_priority'} || []
l!l
726
MC/DC0%0/1
$opts{'indent'} || 2
$opts{'indent'}
MC/DC100%1/1
$opts{'delimiter'} || ','
$opts{'delimiter'}
MC/DC0%0/1
$opts{"strict"} // 1
$opts{"strict"}
MC/DC100%1/1
$opts{"max_depth"} // 100
$opts{"max_depth"}
MC/DC100%1/1
$opts{'column_priority'} || []
$opts{'column_priority'}
Truth table50%1/2
A
A= $opts{'indent'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'delimiter'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"strict"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{"max_depth"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'column_priority'}
Aresult
11
00
16 }, $class;
17 }
18  
19 33 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
20 33 my ($self, $data) = @_;
21
22 33 $self->{depth} = 0;
23 33 $self->{seen} = {}; # Reset seen references
24 33 my $result = $self->_encode_value($data);
25
26 31 return $result;
27 }
28  
29 48 ▶sub _encode_value {
Subroutine _encode_value: called CC=8 SCAR=22.3
30 48 my ($self, $value) = @_;
31
32 48 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 48 48
33
34 48 my $ref = ref $value;
35
36 48 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 27 21 48
elsif ($ref eq 'ARRAY') { } 2 19 21
37 # Check for circular reference
38 27 my $ref_addr = "$value"; # Stringify reference to get address
39 27 ▶ if ($self->{seen}->{$ref_addr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"seen"}{$ref_addr}) 0 27 27
40 0 die "Circular reference detected\n";
41 }
42 27 $self->{seen}->{$ref_addr} = 1;
43
44 # Check max depth
45 27 ▶ if ($self->{depth} > $self->{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"depth"} > $self->{"max_depth"}) 0 27 27
46 0 die "Maximum nesting depth exceeded (max: $self->{max_depth})\n";
47 }
48
49 27 return $self->_encode_object($value);
50 } elsif ($ref eq 'ARRAY') {
51 # Check for circular reference
52 2 my $ref_addr = "$value";
53 2 ▶ if ($self->{seen}->{$ref_addr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"seen"}{$ref_addr}) 0 2 2
54 0 die "Circular reference detected\n";
55 }
56 2 $self->{seen}->{$ref_addr} = 1;
57
58 # Check max depth
59 2 ▶ if ($self->{depth} > $self->{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"depth"} > $self->{"max_depth"}) 0 2 2
60 0 die "Maximum nesting depth exceeded (max: $self->{max_depth})\n";
61 }
62
63 2 return $self->_encode_array($value);
64 } else {
65 19 return $self->_encode_primitive($value);
66 }
67 }
68  
69 53 ▶sub _encode_object {
Subroutine _encode_object: called CC=5 SCAR=16.1
70 53 my ($self, $obj) = @_;
71
72 # Check max depth - allow one level deep
73 53 ▶ if ($self->{depth} >= $self->{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"depth"} >= $self->{"max_depth"}) 2 51 53
74 2 die "Maximum nesting depth exceeded (max: $self->{max_depth})\n";
75 }
76
77 51 my @lines;
78 51 foreach my $key ($self->_sort_fields(keys %$obj)) {
79 75 my $value = $obj->{$key};
80 75 my $indent = ' ' x ($self->{depth} * $self->{indent});
81
82 75 my $ref = ref $value;
83
84 75 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 16 59 75
elsif ($ref eq 'HASH') { } 26 33 59
85 16 push @lines, $self->_encode_object_with_array($indent, $key, $value);
86 } elsif ($ref eq 'HASH') {
87 26 push @lines, $indent . "$key:";
88 26 local $self->{depth} = $self->{depth} + 1;
89 26 push @lines, $self->_encode_object($value);
90 } else {
91 33 my $encoded = $self->_encode_primitive($value);
92 33 push @lines, "$indent$key: $encoded";
93 }
94 }
95
96 39 return join "\n", @lines;
97 }
98  
99 16 ▶sub _encode_object_with_array {
Subroutine _encode_object_with_array: called CC=23 SCAR=31.6
100 16 my ($self, $indent, $key, $array) = @_;
101
102 16 ▶ return undef unless ref $array eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $array eq "ARRAY" 0 16 16
103
104 16 my @lines;
105 16 my $array_len = scalar(@$array);
106
107 # Check if array contains only objects (for potential tabular format)
108 16 my $all_objects = 1;
109 16 foreach my $item (@$array) {
110 26 ▶ if (ref $item ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item ne "HASH") 1 25 26
111 1 $all_objects = 0;
112 1 last;
113 }
114 }
115
116 16 ▶ if ($all_objects && $array_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_objects and $array_len > 0) { } 15 1 16
Condition66%2/3
$all_objects and $array_len > 0
!ll&&!rl&&r
1015
MC/DC50%1/2
$all_objects and $array_len > 0
$all_objects $array_len > 0
Truth table66%2/3
A and B
A= $all_objectsB= $array_len > 0
ABresult
0X0
100
111
117 # Check if tabular format is possible
118 # Requires: all objects have same keys and all values are primitives
119 15 my $first = $array->[0];
120 15 my @first_keys = sort keys %$first;
121 15 my $can_tabular = 1;
122
123 # Check all objects have same keys and all values are primitives
124 15 foreach my $obj (@$array) {
125 25 my @obj_keys = sort keys %$obj;
126
127 # Different key set = can't use tabular
128 25 ▶ if (@obj_keys != @first_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@obj_keys != @first_keys) 1 24 25
129 1 $can_tabular = 0;
130 1 last;
131 }
132
133 24 ▶ for (my $i = 0; $i < @first_keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @first_keys) 60 24 84
134 60 ▶ if ($first_keys[$i] ne $obj_keys[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_keys[$i] ne $obj_keys[$i]) 0 60 60
135 0 $can_tabular = 0;
136 0 last;
137 }
138 }
139
140 24 ▶ if (!$can_tabular) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($can_tabular) 0 24 24
141 0 last;
142 }
143
144 # Check all values are primitives
145 24 foreach my $val (values %$obj) {
146 58 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 4 54 58
147 4 $can_tabular = 0;
148 4 last;
149 }
150 }
151
152 24 ▶ if (!$can_tabular) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($can_tabular) 4 20 24
153 4 last;
154 }
155 }
156
157 15 ▶ if ($can_tabular) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_tabular) { } 10 5 15
158 # Tabular format: extract field names from first object
159 10 my @fields = $self->_sort_fields(keys %$first);
160
161 # Add delimiter indicator in bracket
162 10 my $delim_indicator = '';
163 10 ▶ if ($self->{delimiter} eq "\t") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'delimiter'} eq "\t") { } 1 9 10
elsif ($self->{'delimiter'} eq '|') { } 3 6 9
164 1 $delim_indicator = "\t";
165 } elsif ($self->{delimiter} eq '|') {
166 3 $delim_indicator = '|';
167 }
168
169 10 my $field_list = join($self->{delimiter}, @fields);
170 10 my $header = $indent . $key . '[' . $array_len . $delim_indicator . ']{' . $field_list . '}:';
171 10 push @lines, $header;
172
173 10 local $self->{depth} = $self->{depth} + 1;
174 10 my $row_indent = ' ' x ($self->{depth} * $self->{indent});
175
176 10 foreach my $obj (@$array) {
177 19 my @values = map { $self->_encode_primitive($obj->{$_}) } @fields;
178 19 push @lines, $row_indent . join($self->{delimiter}, @values);
179 }
180 } else {
181 # List format: use - items
182 5 my $header = $indent . $key . '[' . $array_len . ']:';
183 5 push @lines, $header;
184
185 5 local $self->{depth} = $self->{depth} + 1;
186 5 my $item_indent = ' ' x ($self->{depth} * $self->{indent});
187 5 my $field_indent = ' ' x (($self->{depth} + 1) * $self->{indent});
188
189 5 foreach my $obj (@$array) {
190 # First field on hyphen line, remaining fields at depth+2
191 6 my @keys = $self->_sort_fields(keys %$obj);
192 6 ▶ if (@keys > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys > 0) { } 6 0 6
193 # First key with hyphen at current depth
194 6 my $first_key = $keys[0];
195 6 local $self->{depth} = $self->{depth} + 1;
196 6 my $first_val = $self->_encode_value($obj->{$first_key});
197 6 push @lines, $item_indent . "- $first_key: $first_val";
198
199 # Remaining keys at depth+2 (one deeper)
200 6 ▶ for (my $i = 1; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 7 6 13
201 7 my $k = $keys[$i];
202 7 my $v = $self->_encode_value($obj->{$k});
203 7 push @lines, $field_indent . "$k: $v";
204 }
205 } else {
206 # Empty object
207 0 push @lines, $item_indent . "-";
208 }
209 }
210 }
211 } else {
212 # List format: mixed types or not objects
213 1 my $header = $indent . $key . '[' . $array_len . ']:';
214 1 push @lines, $header;
215
216 1 local $self->{depth} = $self->{depth} + 1;
217 1 my $item_indent = ' ' x ($self->{depth} * $self->{indent});
218
219 1 foreach my $item (@$array) {
220 2 my $encoded = $self->_encode_value($item);
221 2 ▶ push @lines, $item_indent . "- $encoded" if defined $encoded;
Branch: 1 branch
BranchTrueFalseTotal
if defined $encoded 2 0 2
222 }
223 }
224
225 16 return join "\n", @lines;
226 }
227  
228 2 ▶sub _encode_array {
Subroutine _encode_array: called CC=8 SCAR=28.2
229 2 my ($self, $array) = @_;
230
231 # Check if all primitives
232 2 my $all_primitives = 1;
233 2 foreach my $item (@$array) {
234 6 ▶ if (ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) 0 6 6
235 0 $all_primitives = 0;
236 0 last;
237 }
238 }
239
240 2 my $array_len = scalar(@$array);
241
242 2 ▶ if ($all_primitives) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_primitives) { } 2 0 2
243 # Primitive array: inline format with delimiter indicator
244 2 my @values = map { $self->_encode_primitive($_) } @$array;
245 2 my $delim_indicator = '';
246 2 ▶ if ($self->{delimiter} eq "\t") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'delimiter'} eq "\t") { } 0 2 2
elsif ($self->{'delimiter'} eq '|') { } 0 2 2
247 0 $delim_indicator = "\t";
248 } elsif ($self->{delimiter} eq '|') {
249 0 $delim_indicator = '|';
250 }
251 2 return '[' . $array_len . $delim_indicator . ']: ' . join($self->{delimiter}, @values);
252 } else {
253 # Mixed/object array: list format
254 0 my @lines = ('[' . $array_len . ']:');
255
256 0 local $self->{depth} = $self->{depth} + 1;
257 0 foreach my $item (@$array) {
258 0 my $encoded = $self->_encode_value($item);
259 0 ▶ push @lines, " - $encoded" if defined $encoded;
Branch: 1 branch
BranchTrueFalseTotal
if defined $encoded 0 0 0
260 }
261
262 0 return join "\n", @lines;
263 }
264 }
265  
266 107 ▶sub _encode_primitive {
Subroutine _encode_primitive: called CC=5 SCAR=16.2
267 107 my ($self, $value) = @_;
268
269 107 ▶ return 'null' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 107 107
270
271 # Check if it's numeric (but not a boolean-like string)
272 107 ▶ if ($value =~ /^[+-]?\d+(?:\.\d+)?$/ && $value !~ /^(true|false|null)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[+-]?\d+(?:\.\d+)?$/u and not $value =~ /^(true|false|null)$/iu) 53 54 107
Condition66%2/3
$value =~ /^[+-]?\d+(?:\.\d+)?$/u and not $value =~ /^(true|false|null)$/iu
!ll&&!rl&&r
54053
MC/DC50%1/2
$value =~ /^[+-]?\d+(?:\.\d+)?$/u and not $value =~ /^(true|false|null)$/iu
$value =~ /^[+-]?\d+(?:\.\d+)?$/u not $value =~ /^(true|false|null)$/iu
Truth table66%2/3
A and not(B)
A= $value =~ /^[+-]?\d+(?:\.\d+)?$/uB= not $value =~ /^(true|false|null)$/iu
ABresult
0X0
100
111
273 # Canonical form: normalize numbers
274 53 my $normalized = $self->_canonicalize_number($value);
275 53 return $normalized;
276 }
277
278 # Check if needs quoting
279 54 ▶ if ($self->_needs_quoting($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_needs_quoting($value)) 10 44 54
280 10 return '"' . $self->_escape_string($value) . '"';
281 }
282
283 44 return $value;
284 }
285  
286 53 ▶sub _canonicalize_number {
Subroutine _canonicalize_number: called CC=3 SCAR=11.0
287 53 my ($self, $num) = @_;
288
289 # -0 becomes 0
290 53 ▶ if ($num == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 0) 1 52 53
291 1 return '0';
292 }
293
294 # Convert to numeric form to normalize
295 52 my $n = 0 + $num;
296
297 # Remove trailing zeros from decimals
298 52 ▶ if ($n =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /\./u) 3 49 52
299 3 $n =~ s/0+$//;
300 3 $n =~ s/\.$//;
301 }
302
303 52 return "$n";
304 }
305  
306 54 ▶sub _needs_quoting {
Subroutine _needs_quoting: called CC=13 SCAR=29.8
307 54 my ($self, $value) = @_;
308
309 54 ▶ return 1 if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 54 54
310 54 ▶ return 1 if $value =~ /^\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\s/u 0 54 54
311 54 ▶ return 1 if $value =~ /\s$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\s$/u 0 54 54
312 54 ▶ return 1 if $value eq 'true' || $value eq 'false' || $value eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "true" or $value eq "false" or $value eq "null" 0 54 54
Condition33%2/6
$value eq "true" or $value eq "false" or $value eq "null"
l!l&&r!l&&!r
0054
$value eq "true" or $value eq "false"
l!l&&r!l&&!r
0054
MC/DC0%0/3
$value eq "true" or $value eq "false" or $value eq "null"
$value eq "true" $value eq "false" $value eq "null"
Truth table25%1/4
A or B or C
A= $value eq "true"B= $value eq "false"C= $value eq "null"
ABCresult
1XX1
01X1
0011
0000
313 54 ▶ return 1 if $value =~ /^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/iu 0 54 54
314 54 ▶ return 1 if $value =~ /^0\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^0\d+$/u 0 54 54
315 54 ▶ return 1 if $value =~ /[:"\\\[\]{}-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[:"\\\[\]{}-]/u 0 54 54
316 54 ▶ return 1 if $value =~ /[\r\n\t]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\r\n\t]/u 0 54 54
317 54 ▶ return 1 if $value =~ /$self->{delimiter}/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$self->{"delimiter"}/u 10 44 54
318 44 ▶ return 1 if $value =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^-/u 0 44 44
319
320 44 return 0;
321 }
322  
323 10 ▶sub _escape_string {
Subroutine _escape_string: called CC=1 SCAR=0.0
324 10 my ($self, $str) = @_;
325
326 10 $str =~ s/\\/\\\\/g;
327 10 $str =~ s/"/\\"/g;
328 10 $str =~ s/\n/\\n/g;
329 10 $str =~ s/\r/\\r/g;
330 10 $str =~ s/\t/\\t/g;
331
332 10 return $str;
333 }
334  
335 67 ▶sub _sort_fields {
Subroutine _sort_fields: called CC=6 SCAR=17.9
336 67 my ($self, @fields) = @_;
337
338 # If no priority specified, use alphabetical sort (backward compatibility)
339 67 ▶ return sort @fields unless @{$self->{column_priority}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"column_priority"};} 54 13 67
340
341 13 my %priority;
342 13 my $index = 0;
343 13 foreach my $col (@{$self->{column_priority}}) {
344 28 $priority{$col} = $index++;
345 }
346
347 # Sort: priority columns first (by priority order), then remaining columns alphabetically
348 return sort {
349 13 ▶ my $a_priority = exists $priority{$a} ? $priority{$a} : 999999;
Branch: 1 branch
BranchTrueFalseTotal
exists $priority{$a} ? : 13 6 19
350 19 ▶ my $b_priority = exists $priority{$b} ? $priority{$b} : 999999;
Branch: 1 branch
BranchTrueFalseTotal
exists $priority{$b} ? : 11 8 19
351
352 19 ▶ if ($a_priority != $b_priority) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_priority != $b_priority) 16 3 19
353 16 return $a_priority <=> $b_priority;
354 }
355 3 return $a cmp $b;
356 } @fields;
357 }
358  
359 10 1;
360  
361  
« TOON/Decoder.pm ↑ Summary TOON/Validator.pm »