| 1 | # Copyrights 2009-2013 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.01. | ||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | package XML::Compile::RPC::Util; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '0.17'; | ||
| 11 | |||
| 12 | use base 'Exporter'; | ||
| 13 | |||
| 14 | our @EXPORT = qw/ | ||
| 15 | struct_to_hash | ||
| 16 | struct_to_rows | ||
| 17 | struct_from_rows | ||
| 18 | struct_from_hash | ||
| 19 | |||
| 20 | rpcarray_values | ||
| 21 | rpcarray_from | ||
| 22 | |||
| 23 | fault_code | ||
| 24 | fault_from | ||
| 25 | /; | ||
| 26 | |||
| 27 | |||
| 28 | sub struct_to_hash($) | ||
| 29 | { my $s = shift; | ||
| 30 | my %h; | ||
| 31 | |||
| 32 | foreach my $member ( @{$s->{member} || []} ) | ||
| 33 | { my ($type, $value) = %{$member->{value}}; | ||
| 34 | $h{$member->{name}} = $value; | ||
| 35 | } | ||
| 36 | |||
| 37 | \%h; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | sub struct_to_rows($) | ||
| 42 | { my $s = shift; | ||
| 43 | my @r; | ||
| 44 | |||
| 45 | foreach my $member ( @{$s->{member} || []} ) | ||
| 46 | { my ($type, $value) = %{$member->{value}}; | ||
| 47 | push @r, [ $member->{name}, $type, $value ]; | ||
| 48 | } | ||
| 49 | |||
| 50 | @r; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | sub struct_from_rows(@) | ||
| 55 | { my @members = map { +{name => $_->[0], value => {$_->[1] => $_->[2]}}} @_; | ||
| 56 | +{ struct => {member => \@members} }; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub struct_from_hash($$) | ||
| 61 | { my ($type, $hash) = @_; | ||
| 62 | my @members = map { +{name => $_, value => {$type => $hash->{$_}}} } | ||
| 63 | sort keys %{$hash || {}}; | ||
| 64 | +{ struct => {member => \@members} }; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | sub rpcarray_values($) | ||
| 69 | { my $rpca = shift; | ||
| 70 | my @v; | ||
| 71 | foreach ( @{$rpca->{data}{value} || []} ) | ||
| 72 | { my ($type, $value) = %$_; | ||
| 73 | push @v, $value; | ||
| 74 | } | ||
| 75 | @v; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub rpcarray_from($@) | ||
| 80 | { my $type = shift; | ||
| 81 | my @values = map { +{$type => $_} } @_; | ||
| 82 | +{array => {data => {value => \@values}}}; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | sub fault_code($) | ||
| 87 | { my $h = struct_to_hash shift->{value}{struct}; | ||
| 88 | my $fc = $h->{faultCode} || -1; | ||
| 89 | wantarray ? ($fc, $h->{faultString}) : $fc; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | sub fault_from($$) | ||
| 94 | { my ($rc, $msg) = @_; | ||
| 95 | my @rows = ([faultCode => int => $rc], [faultString => string => $msg]); | ||
| 96 | +{fault => {value => struct_from_rows(@rows)}}; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; |