File Coverage

lib/XML/Compile/RPC/Util.pm
Criterion Covered Total %
statement 52 52 100.0
branch 6 10 60.0
condition 1 2 50.0
subroutine 12 12 100.0
pod 8 8 100.0
total 79 84 94.0


line stmt bran cond sub pod time code
1             # Copyrights 2009-2020 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution XML-Compile-RPC. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package XML::Compile::RPC::Util;
10 5     5   173157 use vars '$VERSION';
  5         14  
  5         243  
11             $VERSION = '0.20';
12              
13 5     5   27 use base 'Exporter';
  5         9  
  5         420  
14              
15 5     5   29 use warnings;
  5         10  
  5         126  
16 5     5   25 use strict;
  5         21  
  5         2768  
17              
18             our @EXPORT = qw/
19             struct_to_hash
20             struct_to_rows
21             struct_from_rows
22             struct_from_hash
23              
24             rpcarray_values
25             rpcarray_from
26              
27             fault_code
28             fault_from
29             /;
30              
31              
32             sub struct_to_hash($)
33 4     4 1 115 { my $s = shift;
34 4         6 my %h;
35              
36 4 50       8 foreach my $member ( @{$s->{member} || []} )
  4         16  
37 9         13 { my ($type, $value) = %{$member->{value}};
  9         23  
38 9         28 $h{$member->{name}} = $value;
39             }
40              
41 4         9 \%h;
42             }
43              
44              
45             sub struct_to_rows($)
46 1     1 1 728 { my $s = shift;
47 1         3 my @r;
48              
49 1 50       2 foreach my $member ( @{$s->{member} || []} )
  1         4  
50 3         4 { my ($type, $value) = %{$member->{value}};
  3         7  
51 3         8 push @r, [ $member->{name}, $type, $value ];
52             }
53              
54 1         3 @r;
55             }
56              
57              
58             sub struct_from_rows(@)
59 2     2 1 897 { my @members = map +{name => $_->[0], value => {$_->[1] => $_->[2]}}, @_;
60 2         11 +{ struct => {member => \@members} };
61             }
62              
63              
64             sub struct_from_hash($$)
65 1     1 1 1518 { my ($type, $hash) = @_;
66 3         12 my @members = map { +{name => $_, value => {$type => $hash->{$_}}} }
67 1 50       3 sort keys %{$hash || {}};
  1         12  
68 1         17 +{ struct => {member => \@members} };
69             }
70              
71              
72             sub rpcarray_values($)
73 1     1 1 1520 { my $rpca = shift;
74 1         3 my @v;
75 1 50       2 foreach ( @{$rpca->{data}{value} || []} )
  1         5  
76 3         6 { my ($type, $value) = %$_;
77 3         7 push @v, $value;
78             }
79 1         6 @v;
80             }
81              
82              
83             sub rpcarray_from($@)
84 1     1 1 2 { my $type = shift;
85 1         3 my @values = map { +{$type => $_} } @_;
  3         8  
86 1         11 +{array => {data => {value => \@values}}};
87             }
88              
89              
90             sub fault_code($)
91 3     3 1 963 { my $h = struct_to_hash shift->{value}{struct};
92 3   50     49 my $fc = $h->{faultCode} || -1;
93 3 100       23 wantarray ? ($fc, $h->{faultString}) : $fc;
94             }
95              
96              
97             sub fault_from($$)
98 1     1 1 442 { my ($rc, $msg) = @_;
99 1         5 my @rows = ([faultCode => int => $rc], [faultString => string => $msg]);
100 1         4 +{fault => {value => struct_from_rows(@rows)}};
101             }
102              
103             1;