| 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
Condition100%3/3
$emission->{"needs_quoting"} and not defined $settings->{"identifier_mode"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 13 | ▶ | Carp::croak("emit_kdl: $name must be an integer from 0 to $maximum, got '$value'") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value =~ /\A[0-9]+\z/ and $value <= $maximum
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | ▶ | Carp::croak("emit_kdl: escape_mode must be a combination of 0x10, 0x20, 0x40 and 0x170, got '$value'") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$value =~ /\A[0-9]+\z/ and $value <= $ESCAPE_MODE_BITS and ($value & ~$ESCAPE_MODE_BITS) == 0
$value =~ /\A[0-9]+\z/ and $value <= $ESCAPE_MODE_BITS
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
$settings->{"escape_mode"} // -1
$settings->{"identifier_mode"} // -1
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"}
Truth table100%2/2
A
A= $settings->{"escape_mode"}
Truth table100%2/2
A
A= $settings->{"identifier_mode"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 76 | ▶ | if ($tree->isa('Text::KDL::XS::Document')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | _write_node($walk, $_) for @{ $tree->nodes }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 72 | ▶ | if ($tree->isa('Text::KDL::XS::Node')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | for my $element (@$elements) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | return 0 unless Scalar::Util::blessed($element) && $element->isa('Text::KDL::XS::Node'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($element) and $element->isa("Text::KDL::XS::Node")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition37%3/8
ref $node eq "Text::KDL::XS::Node" or builtin::blessed($node) and $node->isa("Text::KDL::XS::Node")
builtin::blessed($node) and $node->isa("Text::KDL::XS::Node")
ref $node || 'a plain scalar'
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")
Truth table0%0/2
A
A= ref $node
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 13 | $emitter->_emit_node($name, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 13 | ▶ | return unless %$value; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'
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'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 10275 | ▶ | return _scalar_payload($value) unless ref $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 166 | ▶ | my $class = Scalar::Util::blessed($value) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
builtin::blessed($value) // Carp::croak('emit_kdl: cannot serialize ' . ref($value) . ' ref')
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$class eq "Text::KDL::XS::Value" or $value->isa("Text::KDL::XS::Value")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 10 | ▶ | return $value ? $TRUE_PAYLOAD : $FALSE_PAYLOAD if _is_boolean($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | ▶ | return _bignum_payload($value) if $value->isa('Math::BigInt') || $value->isa('Math::BigFloat'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value->isa("Math::BigInt") or $value->isa("Math::BigFloat")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 4 | ▶ | return { type => 'string', value => "$value" } if overload::Method($value, '""'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$number->is_nan or $number->is_inf
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 8 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 24 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||