File Coverage

blib/lib/Text/KDL/XS/Node.pm
Criterion Covered Total %
statement 36 36 100.0
branch 1 2 50.0
condition 4 8 50.0
subroutine 13 13 100.0
pod 7 8 87.5
total 61 67 91.0


line stmt bran cond sub pod time code
1             package Text::KDL::XS::Node;
2              
3 7     7   46 use strict;
  7         21  
  7         239  
4 7     7   62 use warnings;
  7         23  
  7         4046  
5              
6             # A Node is a hashref:
7             # name : string
8             # type_annotation : string|undef
9             # args : arrayref of Text::KDL::XS::Value
10             # props : arrayref of [name => Text::KDL::XS::Value] (ordered)
11             # prop_index : { name => idx_into_props } (last-wins)
12             # children : arrayref of Text::KDL::XS::Node
13             sub new {
14 58     58 0 213 my ($class, %args) = @_;
15             return bless {
16             name => $args{name},
17             type_annotation => $args{type_annotation},
18             args => $args{args} // [],
19             props => $args{props} // [],
20             prop_index => $args{prop_index} // {},
21 58   50     767 children => $args{children} // [],
      50        
      50        
      50        
22             }, $class;
23             }
24              
25 36     36 1 2052 sub name { $_[0]->{name} }
26 5     5 1 27 sub type_annotation { $_[0]->{type_annotation} }
27 30     30 1 127 sub args { $_[0]->{args} }
28 6     6 1 12 sub props { $_[0]->{props} }
29 12     12 1 2353 sub children { $_[0]->{children} }
30              
31             sub prop {
32 2     2 1 4 my ($self, $key) = @_;
33 2         5 my $idx = $self->{prop_index}{$key};
34 2 50       6 return undef unless defined $idx;
35 2         7 return $self->{props}[$idx][1];
36             }
37              
38             # Plain Perl view - lossy with respect to property order and per-arg type
39             # annotations. Documented in POD.
40             sub as_data {
41 12     12 1 17 my ($self) = @_;
42             return {
43             name => $self->{name},
44             type => $self->{type_annotation},
45 12         38 args => [ map { $_->as_perl } @{ $self->{args} } ],
  12         45  
46 4         9 props => { map { $_->[0] => $_->[1]->as_perl } @{ $self->{props} } },
  12         20  
47 12         16 children => [ map { $_->as_data } @{ $self->{children} } ],
  6         12  
  12         47  
48             };
49             }
50              
51             # Internal: append (used by the tree builder)
52             sub _push_arg {
53 57     57   82 my ($self, $value) = @_;
54 57         70 push @{ $self->{args} }, $value;
  57         114  
55             }
56              
57             sub _push_prop {
58 4     4   7 my ($self, $key, $value) = @_;
59 4         5 push @{ $self->{props} }, [ $key, $value ];
  4         8  
60 4         4 $self->{prop_index}{$key} = $#{ $self->{props} };
  4         34  
61             }
62              
63             sub _push_child {
64 19     19   27 my ($self, $child) = @_;
65 19         21 push @{ $self->{children} }, $child;
  19         37  
66             }
67              
68             1;
69              
70             __END__