line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package GraphQL::Role::HashMappable; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
11611
|
use 5.014; |
|
17
|
|
|
|
|
69
|
|
4
|
17
|
|
|
17
|
|
103
|
use strict; |
|
51
|
|
|
|
|
344
|
|
|
51
|
|
|
|
|
483
|
|
5
|
51
|
|
|
17
|
|
362
|
use warnings; |
|
51
|
|
|
|
|
196
|
|
|
134
|
|
|
|
|
770
|
|
6
|
134
|
|
|
17
|
|
527
|
use Moo::Role; |
|
134
|
|
|
|
|
567
|
|
|
134
|
|
|
|
|
562
|
|
7
|
51
|
|
|
17
|
|
6921
|
use Types::Standard -all; |
|
45
|
|
|
|
|
182
|
|
|
17
|
|
|
|
|
144
|
|
8
|
17
|
|
|
17
|
|
800577
|
use GraphQL::MaybeTypeCheck; |
|
17
|
|
|
|
|
47
|
|
|
17
|
|
|
|
|
175
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
GraphQL::Role::HashMappable - GraphQL object role |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
with qw(GraphQL::Role::HashMappable); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# or runtime |
21
|
|
|
|
|
|
|
Role::Tiny->apply_roles_to_object($foo, qw(GraphQL::Role::HashMappable)); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Provides method for mapping code over a hash-ref. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 METHODS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 hashmap |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Given a hash-ref, returns a modified copy of the data. Returns undef if |
32
|
|
|
|
|
|
|
given that. Parameters: |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=item $item |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Hash-ref. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=item $source |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Hash-ref of the source data for this hash. Will be used only for its keys. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item $code |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Code-ref. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=back |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Each value will be the original value returned by the given code-ref, |
51
|
|
|
|
|
|
|
which is called with C<$keyname>, C<$value>. Will call the code for all |
52
|
|
|
|
|
|
|
given keys, but not copy over any values not existing in original item. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
If code throws an exception, the message will have added to it information |
55
|
|
|
|
|
|
|
about which data element caused it. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
34
|
50
|
|
34
|
1
|
120
|
method hashmap(Maybe[HashRef] $item, HashRef $source, CodeRef $code) :ReturnType(Maybe[HashRef]) { |
|
34
|
50
|
|
|
|
104
|
|
|
34
|
100
|
|
|
|
74
|
|
|
34
|
100
|
|
|
|
80
|
|
|
34
|
100
|
|
|
|
112
|
|
|
34
|
|
|
|
|
343
|
|
|
34
|
|
|
|
|
290
|
|
60
|
|
|
|
|
|
|
return $item if !defined $item; |
61
|
|
|
|
|
|
|
my @errors = map qq{In field "$_": Unknown field.\n}, grep !exists $source->{$_}, sort keys %$item; |
62
|
|
|
|
|
|
|
my %newvalue = map { |
63
|
|
|
|
|
|
|
my @pair = eval { ($_ => scalar $code->($_, $item->{$_})) }; |
64
|
|
|
|
|
|
|
push @errors, qq{In field "$_": $@} if $@; |
65
|
|
|
|
|
|
|
exists $item->{$_} ? @pair : (); |
66
|
|
|
|
|
|
|
} sort keys %$source; |
67
|
|
|
|
|
|
|
die @errors if @errors; |
68
|
|
|
|
|
|
|
\%newvalue; |
69
|
17
|
|
|
17
|
|
10874
|
} |
|
17
|
|
|
|
|
42
|
|
|
17
|
|
|
|
|
172
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable(); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |