blib/lib/Text/KDL/XS/Emitter.pm

filter: Statementstmt 99.3% 146 / 147 Branchbran 90.9% 60 / 66 Conditioncond 84.7% 39 / 46 MC/DCmcdc 77.7% 21 / 27 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 93.5% 289 / 309 CC 63 SCAR 19.4
CC
63
Cov
95%
_write_top_level
12.1
_payload_for
11.0
_write_node
11.0
CRAP 6.9
↑ Summary blib/lib/Text/KDL/XS/Value.pm »
1 package Text::KDL::XS::Emitter;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Text::KDL::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 our @CARP_NOT = qw(
12 Text::KDL::XS Text::KDL::XS::Document Text::KDL::XS::Node
13 Text::KDL::XS::Parser Text::KDL::XS::Value
14 );
15  
16 24 my %IS_OPTION = map { $_ => 1 } qw(version indent escape_mode identifier_mode);
17 24 my %VERSION_CODE = (detect => 0, 1 => 1, 2 => 2);
18 24 my $MAX_INDENT = 64;
19 24 my $ESCAPE_MODE_BITS = 0x170;
20 24 my $QUOTE_ALL = 1;
21  
22 24 my @BOOLEAN_CLASSES = qw(JSON::PP::Boolean Types::Serialiser::Boolean JSON::Boolean boolean Mojo::JSON::_Bool);
23  
24 24 my $NULL_PAYLOAD = { type => 'null' };
25 24 my $TRUE_PAYLOAD = { type => 'bool', value => 1 };
26 24 my $FALSE_PAYLOAD = { type => 'bool', value => 0 };
27  
28 # emit_kdl. The document is written once through the XS emitter, which notes
29 # any name, key, annotation or string value that it would write bare although
30 # it reads back as a keyword or a number. Unless the caller chose an
31 # identifier_mode, such a document is written again with every identifier
32 # quoted.
33 166 ▶sub _emit_tree {
Subroutine _emit_tree: called CC=3 SCAR=11.0
34 166 my ($class, $tree, @options) = @_;
35 166 my $settings = _parse_settings(@options);
36  
37 149 my $emission = _write_document($class, $tree, $settings);
38 $emission = _write_document($class, $tree, { %$settings, identifier_mode => $QUOTE_ALL })
39 119 ▶ if $emission->{needs_quoting} && !defined $settings->{identifier_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $emission->{"needs_quoting"} and not defined $settings->{"identifier_mode"} 35 84 119
Condition100%3/3
$emission->{"needs_quoting"} and not defined $settings->{"identifier_mode"}
!ll&&!rl&&r
83135
MC/DC100%2/2
$emission->{"needs_quoting"} and not defined $settings->{"identifier_mode"}
$emission->{"needs_quoting"} not defined $settings->{"identifier_mode"}
Truth table100%3/3
A and not(B)
A= $emission->{"needs_quoting"}B= not defined $settings->{"identifier_mode"}
ABresult
0X0
100
111
40 119 return $emission->{text};
41 }
42  
43 166 ▶sub _parse_settings {
Subroutine _parse_settings: called CC=1 SCAR=0.0
44 166 my (@options) = @_;
45 166 my $options = Text::KDL::XS::_parse_named_arguments('emit_kdl', 'option', \%IS_OPTION, @options);
46 164 my $version = Text::KDL::XS::_normalize_version('emit_kdl', $options->{version});
47  
48 return {
49 version => $VERSION_CODE{$version},
50 indent => _integer_option('indent', $options->{indent}, $MAX_INDENT),
51 escape_mode => _escape_mode_option($options->{escape_mode}),
52 161 identifier_mode => _integer_option('identifier_mode', $options->{identifier_mode}, 2),
53 };
54 }
55  
56 313 ▶sub _integer_option {
Subroutine _integer_option: called CC=4 SCAR=13.9
57 313 my ($name, $value, $maximum) = @_;
58 313 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 300 13 313
59 13 ▶ Carp::croak("emit_kdl: $name must be an integer from 0 to $maximum, got '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[0-9]+\z/ and $value <= $maximum 8 5 13
Condition100%3/3
$value =~ /\A[0-9]+\z/ and $value <= $maximum
!ll&&!rl&&r
625
MC/DC100%2/2
$value =~ /\A[0-9]+\z/ and $value <= $maximum
$value =~ /\A[0-9]+\z/ $value <= $maximum
Truth table100%3/3
A and B
A= $value =~ /\A[0-9]+\z/B= $value <= $maximum
ABresult
0X0
100
111
60 unless $value =~ /\A[0-9]+\z/ && $value <= $maximum;
61 5 return $value;
62 }
63  
64 156 ▶sub _escape_mode_option {
Subroutine _escape_mode_option: called CC=5 SCAR=16.1
65 156 my ($value) = @_;
66 156 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 149 7 156
67 7 ▶ Carp::croak("emit_kdl: escape_mode must be a combination of 0x10, 0x20, 0x40 and 0x170, got '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[0-9]+\z/ and $value <= $ESCAPE_MODE_BITS and ($value & ~$ESCAPE_MODE_BITS) == 0 4 3 7
Condition100%6/6
$value =~ /\A[0-9]+\z/ and $value <= $ESCAPE_MODE_BITS and ($value & ~$ESCAPE_MODE_BITS) == 0
!ll&&!rl&&r
313
$value =~ /\A[0-9]+\z/ and $value <= $ESCAPE_MODE_BITS
!ll&&!rl&&r
214
MC/DC100%3/3
$value =~ /\A[0-9]+\z/ and $value <= $ESCAPE_MODE_BITS and ($value & ~$ESCAPE_MODE_BITS) == 0
$value =~ /\A[0-9]+\z/ $value <= $ESCAPE_MODE_BITS ($value & ~$ESCAPE_MODE_BITS) == 0
Truth table100%4/4
A and B and C
A= $value =~ /\A[0-9]+\z/B= $value <= $ESCAPE_MODE_BITSC= ($value & ~$ESCAPE_MODE_BITS) == 0
ABCresult
0XX0
10X0
1100
1111
68 unless $value =~ /\A[0-9]+\z/ && $value <= $ESCAPE_MODE_BITS && ($value & ~$ESCAPE_MODE_BITS) == 0;
69 3 return $value;
70 }
71  
72 # Writes the whole document with one XS emitter. Returns the text and
73 # whether it has to be written in quote-all mode to read back as written.
74 184 ▶sub _write_document {
Subroutine _write_document: called CC=6 SCAR=17.9
75 184 my ($class, $tree, $settings) = @_;
76 184 my $walk = { emitter => undef, on_path => {} };
77  
78 184 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $walk->{"emitter"} = $class->_new($settings->{"version"}, $settings->{"indent"} // -1, $settings->{"escape_mode"} // -1, $settings->{"identifier_mode"} // -1); _write_top_level($walk, $tree); $walk->{"emitter"}->_emit_end; 1 } } or Text::KDL::XS::_rethrow($@, __FILE__) 154 30 184
79 $walk->{emitter} = $class->_new(
80 $settings->{version},
81 $settings->{indent} // -1,
82 $settings->{escape_mode} // -1,
83 184 ▶ $settings->{identifier_mode} // -1,
Condition100%6/6
$settings->{"indent"} // -1
l!l
2182
$settings->{"escape_mode"} // -1
l!l
3181
$settings->{"identifier_mode"} // -1
l!l
38146
MC/DC100%1/1
$settings->{"indent"} // -1
$settings->{"indent"}
MC/DC100%1/1
$settings->{"escape_mode"} // -1
$settings->{"escape_mode"}
MC/DC100%1/1
$settings->{"identifier_mode"} // -1
$settings->{"identifier_mode"}
Truth table100%2/2
A
A= $settings->{"indent"}
Aresult
11
00
Truth table100%2/2
A
A= $settings->{"escape_mode"}
Aresult
11
00
Truth table100%2/2
A
A= $settings->{"identifier_mode"}
Aresult
11
00
84 );
85 184 _write_top_level($walk, $tree);
86 154 $walk->{emitter}->_emit_end;
87 154 1;
88 } or Text::KDL::XS::_rethrow($@, __FILE__);
89  
90 154 my $text = $walk->{emitter}->_get_buffer;
91 154 ▶ return { text => length $text ? $text : "\n", needs_quoting => $walk->{emitter}->_needs_quoting };
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 152 2 154
92 }
93  
94 184 ▶sub _write_top_level {
Subroutine _write_top_level: called CC=12 SCAR=24.9
95 184 my ($walk, $tree) = @_;
96  
97 184 ▶ if (Scalar::Util::blessed($tree)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($tree)) { } 76 108 184
elsif (ref $tree eq 'ARRAY' and _is_node_list($tree)) { } 1 107 108
elsif (_is_container($tree)) { } 107 0 107
98 76 ▶ if ($tree->isa('Text::KDL::XS::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->isa("Text::KDL::XS::Document")) 4 72 76
99 4 _write_node($walk, $_) for @{ $tree->nodes };
100 4 return;
101 }
102 72 ▶ if ($tree->isa('Text::KDL::XS::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->isa("Text::KDL::XS::Node")) 72 0 72
103 72 _write_node($walk, $tree);
104 66 return;
105 }
106 }
107 ▶ elsif (ref $tree eq 'ARRAY' && _is_node_list($tree)) {
Condition100%3/3
ref $tree eq "ARRAY" and _is_node_list($tree)
!ll&&!rl&&r
10521
MC/DC100%2/2
ref $tree eq "ARRAY" and _is_node_list($tree)
ref $tree eq "ARRAY" _is_node_list($tree)
Truth table100%3/3
A and B
A= ref $tree eq "ARRAY"B= _is_node_list($tree)
ABresult
0X0
100
111
108 1 _write_node($walk, $_) for @$tree;
109 1 return;
110 }
111 elsif (_is_container($tree)) {
112 107 local $walk->{on_path}{ _enter($walk, $tree) } = 1;
113 107 ▶ if (ref $tree eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree eq 'HASH') { } 105 2 107
114 105 _write_data_pair($walk, $_, $tree->{$_}) for sort keys %$tree;
115 }
116 else {
117 2 _write_data_pair($walk, '-', $_) for @$tree;
118 }
119 83 return;
120 }
121 0 Carp::croak("emit_kdl: expected Document, Node, ARRAY ref, or HASH ref");
122 }
123  
124 3 ▶sub _is_node_list {
Subroutine _is_node_list: called CC=5 SCAR=16.3
125 3 my ($elements) = @_;
126 3 ▶ return 0 unless @$elements;
Branch: 1 branch
BranchTrueFalseTotal
unless @$elements 0 3 3
127 3 for my $element (@$elements) {
128 3 ▶ return 0 unless Scalar::Util::blessed($element) && $element->isa('Text::KDL::XS::Node');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($element) and $element->isa("Text::KDL::XS::Node") 2 1 3
Condition66%2/3
builtin::blessed($element) and $element->isa("Text::KDL::XS::Node")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($element) and $element->isa("Text::KDL::XS::Node")
builtin::blessed($element) $element->isa("Text::KDL::XS::Node")
Truth table66%2/3
A and B
A= builtin::blessed($element)B= $element->isa("Text::KDL::XS::Node")
ABresult
0X0
100
111
129 }
130 1 return 1;
131 }
132  
133 # The address of a node, hash or array about to be written. Croaks when it is
134 # already being written further up, that is, when the structure is cyclic.
135 214 ▶sub _enter {
Subroutine _enter: called CC=2 SCAR=6.9
136 214 my ($walk, $reference) = @_;
137 214 my $address = Scalar::Util::refaddr($reference);
138 214 ▶ Carp::croak("emit_kdl: cyclic data structure") if $walk->{on_path}{$address};
Branch: 1 branch
BranchTrueFalseTotal
if $walk->{"on_path"}{$address} 3 211 214
139 211 return $address;
140 }
141  
142 # ---------------------------------------------------------------------------
143 # Tree mode
144 # ---------------------------------------------------------------------------
145  
146 82 ▶sub _write_node {
Subroutine _write_node: called CC=10 SCAR=23.9
147 82 my ($walk, $node) = @_;
148 24 ▶ no warnings 'recursion'; # the depth of a tree is bounded by its builder
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 82 ▶ Carp::croak("emit_kdl: tree mode expects Text::KDL::XS::Node, got " . (ref($node) || 'a plain scalar'))
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "Text::KDL::XS::Node" or builtin::blessed($node) and $node->isa("Text::KDL::XS::Node") 0 82 82
Condition37%3/8
ref $node eq "Text::KDL::XS::Node" or builtin::blessed($node) and $node->isa("Text::KDL::XS::Node")
l!l&&r!l&&!r
8020
builtin::blessed($node) and $node->isa("Text::KDL::XS::Node")
!ll&&!rl&&r
002
ref $node || 'a plain scalar'
l!l
00
MC/DC0%0/3
ref $node eq "Text::KDL::XS::Node" or builtin::blessed($node) and $node->isa("Text::KDL::XS::Node")
ref $node eq "Text::KDL::XS::Node" builtin::blessed($node) $node->isa("Text::KDL::XS::Node")
MC/DC0%0/1
ref $node || 'a plain scalar'
ref $node
Truth table50%2/4
A or B and C
A= ref $node eq "Text::KDL::XS::Node"B= builtin::blessed($node)C= $node->isa("Text::KDL::XS::Node")
ABCresult
1XX1
0111
00X0
0100
Truth table0%0/2
A
A= ref $node
Aresult
11
00
151 unless ref $node eq 'Text::KDL::XS::Node'
152 || (Scalar::Util::blessed($node) && $node->isa('Text::KDL::XS::Node'));
153 82 local $walk->{on_path}{ _enter($walk, $node) } = 1;
154  
155 81 my $emitter = $walk->{emitter};
156 81 $emitter->_emit_node($node->name, $node->type_annotation);
157 79 $emitter->_emit_arg(_payload_for($_)) for @{ $node->args };
158 78 for my $property (@{ $node->props }) {
159 67 ▶ Carp::croak("emit_kdl: a property must be a [ key => value ] pair") unless ref $property eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $property eq "ARRAY" 0 67 67
160 67 $emitter->_emit_property($property->[0], _payload_for($property->[1]));
161 }
162  
163 76 my $children = $node->children;
164 76 ▶ return unless @$children;
Branch: 1 branch
BranchTrueFalseTotal
unless @$children 73 3 76
165 3 $emitter->_start_children;
166 3 _write_node($walk, $_) for @$children;
167 2 $emitter->_finish_children;
168 }
169  
170 # ---------------------------------------------------------------------------
171 # Data mode
172 # ---------------------------------------------------------------------------
173  
174 # One node for a hash key or an array element: a single value becomes an
175 # argument, a hash becomes children, an array of single values becomes
176 # arguments, and an array holding hashes or arrays becomes one sibling node
177 # per element.
178 150 ▶sub _write_data_pair {
Subroutine _write_data_pair: called CC=8 SCAR=20.8
179 150 my ($walk, $name, $value) = @_;
180 24 ▶ no warnings 'recursion'; # the depth of the data is the caller's
Subroutine BEGIN: called CC=1 SCAR=0.0
181  
182 150 my $emitter = $walk->{emitter};
183 150 ▶ if (!_is_container($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_container($value)) 125 25 150
184 125 $emitter->_emit_node($name, undef);
185 123 $emitter->_emit_arg(_payload_for($value));
186 103 return;
187 }
188  
189 25 local $walk->{on_path}{ _enter($walk, $value) } = 1;
190  
191 23 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 13 10 23
192 13 $emitter->_emit_node($name, undef);
193 13 ▶ return unless %$value;
Branch: 1 branch
BranchTrueFalseTotal
unless %$value 0 13 13
194 13 $emitter->_start_children;
195 13 _write_data_pair($walk, $_, $value->{$_}) for sort keys %$value;
196 13 $emitter->_finish_children;
197 13 return;
198 }
199  
200 10 ▶ if (grep { _is_container($_) } @$value) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {_is_container($_);} @$value) 4 6 10
201 4 _write_data_pair($walk, $name, $_) for @$value;
202 3 return;
203 }
204 6 $emitter->_emit_node($name, undef);
205 6 $emitter->_emit_arg(_payload_for($_)) for @$value;
206 }
207  
208 # Plain hashes and arrays are structure; everything else is a single value.
209 10277 ▶sub _is_container {
Subroutine _is_container: called CC=2 SCAR=6.9
210 10277 my ($value) = @_;
211 10277 my $type = ref $value;
212 10277 ▶ return $type eq 'HASH' || $type eq 'ARRAY';
Condition100%3/3
$type eq 'HASH' || $type eq 'ARRAY'
l!l&&r!l&&!r
1241410139
MC/DC100%2/2
$type eq 'HASH' || $type eq 'ARRAY'
$type eq 'HASH' $type eq 'ARRAY'
Truth table100%3/3
A || B
A= $type eq 'HASH'B= $type eq 'ARRAY'
ABresult
1X1
011
000
213 }
214  
215 # ---------------------------------------------------------------------------
216 # Values
217 # ---------------------------------------------------------------------------
218  
219 # The payload the XS emitter reads for one argument or property value: a
220 # Text::KDL::XS::Value itself, or a hash with the same keys.
221 10276 ▶sub _payload_for {
Subroutine _payload_for: called CC=11 SCAR=24.0
222 10276 my ($value) = @_;
223  
224 10276 ▶ return $NULL_PAYLOAD unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 10275 10276
225 10275 ▶ return _scalar_payload($value) unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 10109 166 10275
226  
227 166 ▶ my $class = Scalar::Util::blessed($value)
Condition100%2/2
builtin::blessed($value) // Carp::croak('emit_kdl: cannot serialize ' . ref($value) . ' ref')
l!l
1642
MC/DC100%1/1
builtin::blessed($value) // Carp::croak('emit_kdl: cannot serialize ' . ref($value) . ' ref')
builtin::blessed($value)
Truth table100%2/2
A
A= builtin::blessed($value)
Aresult
11
00
228 // Carp::croak("emit_kdl: cannot serialize " . ref($value) . " ref");
229 164 ▶ return $value if $class eq 'Text::KDL::XS::Value' || $value->isa('Text::KDL::XS::Value');
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Text::KDL::XS::Value" or $value->isa("Text::KDL::XS::Value") 154 10 164
Condition100%3/3
$class eq "Text::KDL::XS::Value" or $value->isa("Text::KDL::XS::Value")
l!l&&r!l&&!r
151310
MC/DC100%2/2
$class eq "Text::KDL::XS::Value" or $value->isa("Text::KDL::XS::Value")
$class eq "Text::KDL::XS::Value" $value->isa("Text::KDL::XS::Value")
Truth table100%3/3
A or B
A= $class eq "Text::KDL::XS::Value"B= $value->isa("Text::KDL::XS::Value")
ABresult
1X1
011
000
230 10 ▶ return $value ? $TRUE_PAYLOAD : $FALSE_PAYLOAD if _is_boolean($value);
Branch: 2 branches
BranchTrueFalseTotal
if _is_boolean($value) 2 8 10
$value ? : 1 1 2
231 8 ▶ return _bignum_payload($value) if $value->isa('Math::BigInt') || $value->isa('Math::BigFloat');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Math::BigInt") or $value->isa("Math::BigFloat") 4 4 8
Condition100%3/3
$value->isa("Math::BigInt") or $value->isa("Math::BigFloat")
l!l&&r!l&&!r
314
MC/DC100%2/2
$value->isa("Math::BigInt") or $value->isa("Math::BigFloat")
$value->isa("Math::BigInt") $value->isa("Math::BigFloat")
Truth table100%3/3
A or B
A= $value->isa("Math::BigInt")B= $value->isa("Math::BigFloat")
ABresult
1X1
011
000
232 4 ▶ return { type => 'string', value => "$value" } if overload::Method($value, '""');
Branch: 1 branch
BranchTrueFalseTotal
if overload::Method($value, "\"\"") 2 2 4
233 2 Carp::croak("emit_kdl: cannot serialize $class object");
234 }
235  
236 # A plain scalar is a number when it has a numeric value whose Perl rendering
237 # is its string value (if it has one); otherwise it is a string.
238 10109 ▶sub _scalar_payload {
Subroutine _scalar_payload: called CC=2 SCAR=6.9
239 10109 my ($value) = @_;
240 10109 my $kind = Text::KDL::XS::_number_kind_of($value);
241 10109 ▶ return { type => 'number', kind => $kind, value => $value } if defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kind 10052 57 10109
242 57 return { type => 'string', value => $value };
243 }
244  
245 4 ▶sub _bignum_payload {
Subroutine _bignum_payload: called CC=3 SCAR=11.0
246 4 my ($number) = @_;
247 4 ▶ Carp::croak("emit_kdl: cannot write the " . ref($number) . " $number as a KDL number")
Branch: 1 branch
BranchTrueFalseTotal
if $number->is_nan or $number->is_inf 1 3 4
Condition66%2/3
$number->is_nan or $number->is_inf
l!l&&r!l&&!r
103
MC/DC50%1/2
$number->is_nan or $number->is_inf
$number->is_nan $number->is_inf
Truth table66%2/3
A or B
A= $number->is_nanB= $number->is_inf
ABresult
1X1
011
000
248 if $number->is_nan || $number->is_inf;
249 3 return { type => 'number', kind => 'string', value => "$number" };
250 }
251  
252 10 ▶sub _is_boolean {
Subroutine _is_boolean: called CC=3 SCAR=11.0
253 10 my ($object) = @_;
254 10 for my $class (@BOOLEAN_CLASSES) {
255 42 ▶ return 1 if $object->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $object->isa($class) 2 40 42
256 }
257 8 return 0;
258 }
259  
260 24 1;
261  
262 __END__
↑ Summary blib/lib/Text/KDL/XS/Value.pm »