line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package GraphQL::Role::HashMappable; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
12551
|
use 5.014; |
|
17
|
|
|
|
|
66
|
|
4
|
17
|
|
|
17
|
|
105
|
use strict; |
|
51
|
|
|
|
|
325
|
|
|
51
|
|
|
|
|
465
|
|
5
|
51
|
|
|
17
|
|
344
|
use warnings; |
|
51
|
|
|
|
|
192
|
|
|
134
|
|
|
|
|
668
|
|
6
|
134
|
|
|
17
|
|
398
|
use Moo::Role; |
|
134
|
|
|
|
|
540
|
|
|
134
|
|
|
|
|
508
|
|
7
|
51
|
|
|
17
|
|
7218
|
use Types::Standard -all; |
|
45
|
|
|
|
|
226
|
|
|
17
|
|
|
|
|
140
|
|
8
|
17
|
|
|
17
|
|
750659
|
use GraphQL::MaybeTypeCheck; |
|
17
|
|
|
|
|
52
|
|
|
17
|
|
|
|
|
217
|
|
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
|
135
|
method hashmap(Maybe[HashRef] $item, HashRef $source, CodeRef $code) :ReturnType(Maybe[HashRef]) { |
|
34
|
50
|
|
|
|
105
|
|
|
34
|
100
|
|
|
|
64
|
|
|
34
|
100
|
|
|
|
87
|
|
|
34
|
100
|
|
|
|
137
|
|
|
34
|
|
|
|
|
335
|
|
|
34
|
|
|
|
|
279
|
|
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
|
|
10829
|
} |
|
17
|
|
|
|
|
53
|
|
|
17
|
|
|
|
|
152
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable(); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |