blib/lib/JSON/MergePatch.pm

filter: Statementstmt 100% 59 / 59 Branchbran 96.1% 25 / 26 Conditioncond 89.5% 43 / 48 MC/DCmcdc 23.8% 5 / 21 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 86.9% 147 / 169 CC 32 SCAR 27.3
CC
32
Cov
95%
diff
23.1
patch
10.0
BEGIN
1.0
CRAP 15.3
↑ Summary
1 package JSON::MergePatch;
2 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = "0.04";
7  
8 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::MaybeXS qw/encode_json decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Test::Deep::NoTest;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our @EXPORT = qw/json_merge_patch json_merge_diff/;
14  
15  
16 52 ▶sub patch {
Subroutine patch: called CC=10 SCAR=23.0
Pod documented
17 52 my ($class, $target, $patch, $opt) = @_;
18 52 ▶ if (defined $target && !$opt->{repeat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target and not $opt->{"repeat"}) 30 22 52
Condition100%3/3
defined $target and not $opt->{"repeat"}
!ll&&!rl&&r
101230
MC/DC100%2/2
defined $target and not $opt->{"repeat"}
defined $target not $opt->{"repeat"}
Truth table100%3/3
A and not(B)
A= defined $targetB= not $opt->{"repeat"}
ABresult
0X0
100
111
19 30 $target = decode_json($target);
20 }
21  
22 52 ▶ if (ref $patch eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $patch eq "HASH") 28 24 52
23 28 ▶ unless (ref $target eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $target eq "HASH") 6 22 28
24 6 $target = +{};
25 }
26  
27 28 for my $key (keys %$patch) {
28 32 ▶ if (defined $patch->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $patch->{$key}) { } 22 10 32
29 22 $target->{$key} = __PACKAGE__->patch($target->{$key}, $patch->{$key}, {repeat => 1});
30 }
31 else {
32 10 ▶ if (exists $target->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $target->{$key}) 4 6 10
33 4 delete $target->{$key};
34 }
35 }
36 }
37 28 ▶ return ref $target ? encode_json($target) : $target;
Branch: 1 branch
BranchTrueFalseTotal
ref $target ? : 28 0 28
38 }
39  
40 24 ▶ return ref $patch ? encode_json($patch) : $patch;
Branch: 1 branch
BranchTrueFalseTotal
ref $patch ? : 8 16 24
41 }
42  
43 112 ▶sub diff {
Subroutine diff: called CC=23 SCAR=31.4
Pod documented
44 112 my ($class, $source, $target, $opt) = @_;
45  
46 112 my ($decoded_source, $decoded_target);
47 112 ▶ if ($opt->{repeat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'repeat'}) { } 66 46 112
48 66 $decoded_source = $source;
49 66 $decoded_target = $target;
50 } else {
51 46 $decoded_source = eval {
52 46 decode_json($source);
53 };
54 46 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 42 46
55 4 return $source;
56 }
57  
58 42 $decoded_target = eval {
59 42 decode_json($target);
60 };
61 42 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 40 42
62 2 return $decoded_source;
63 }
64 }
65  
66 106 ▶ if (ref $decoded_source eq 'HASH' && ref $decoded_target eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded_source eq "HASH" and ref $decoded_target eq "HASH") 44 62 106
Condition100%3/3
ref $decoded_source eq "HASH" and ref $decoded_target eq "HASH"
!ll&&!rl&&r
54844
MC/DC100%2/2
ref $decoded_source eq "HASH" and ref $decoded_target eq "HASH"
ref $decoded_source eq "HASH" ref $decoded_target eq "HASH"
Truth table100%3/3
A and B
A= ref $decoded_source eq "HASH"B= ref $decoded_target eq "HASH"
ABresult
0X0
100
111
67 44 for my $key (uniq (keys %$decoded_target, keys %$decoded_source)) {
68 66 $decoded_source->{$key} = __PACKAGE__->diff($decoded_source->{$key}, $decoded_target->{$key}, {repeat => 1});
69  
70 66 ▶ if (exists $decoded_target->{$key} && exists $decoded_source->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $decoded_target->{$key} and exists $decoded_source->{$key}) 58 8 66
Condition66%2/3
exists $decoded_target->{$key} and exists $decoded_source->{$key}
!ll&&!rl&&r
8058
MC/DC50%1/2
exists $decoded_target->{$key} and exists $decoded_source->{$key}
exists $decoded_target->{$key} exists $decoded_source->{$key}
Truth table66%2/3
A and B
A= exists $decoded_target->{$key}B= exists $decoded_source->{$key}
ABresult
0X0
100
111
71 58 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $decoded_target->{$key} || defined $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and $decoded_target->{$key} eq $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "HASH" and not %{$decoded_source->{$key};} and ref $decoded_target->{$key} eq "HASH" or defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY" and eq_deeply($decoded_target->{$key}, $decoded_source->{$key})) 12 46 58
Condition90%27/30
not defined $decoded_target->{$key} || defined $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and $decoded_target->{$key} eq $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "HASH" and not %{$decoded_source->{$key};} and ref $decoded_target->{$key} eq "HASH" or defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY" and eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
l!l&&r!l&&!r
10246
not defined $decoded_target->{$key} || defined $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and $decoded_target->{$key} eq $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "HASH" and not %{$decoded_source->{$key};} and ref $decoded_target->{$key} eq "HASH"
l!l&&r!l&&!r
8248
not defined $decoded_target->{$key} || defined $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and $decoded_target->{$key} eq $decoded_source->{$key}
l!l&&r!l&&!r
2650
defined $decoded_target->{$key} || defined $decoded_source->{$key}
l!l&&r!l&&!r
5602
defined $decoded_target->{$key} and defined $decoded_source->{$key} and $decoded_target->{$key} eq $decoded_source->{$key}
!ll&&!rl&&r
10406
defined $decoded_target->{$key} and defined $decoded_source->{$key}
!ll&&!rl&&r
01046
defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "HASH" and not %{$decoded_source->{$key};} and ref $decoded_target->{$key} eq "HASH"
!ll&&!rl&&r
4622
defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "HASH" and not %{$decoded_source->{$key};}
!ll&&!rl&&r
36104
defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "HASH"
!ll&&!rl&&r
102614
defined $decoded_target->{$key} and defined $decoded_source->{$key}
!ll&&!rl&&r
01040
MC/DC0%0/11
not defined $decoded_target->{$key} || defined $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and $decoded_target->{$key} eq $decoded_source->{$key} or defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "HASH" and not %{$decoded_source->{$key};} and ref $decoded_target->{$key} eq "HASH" or defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY" and eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
defined $decoded_target->{$key} defined $decoded_source->{$key} defined $decoded_target->{$key} defined $decoded_source->{$key} $decoded_target->{$key} eq $decoded_source->{$key} defined $decoded_target->{$key} defined $decoded_source->{$key} ref $decoded_source->{$key} eq "HASH" not %{$decoded_source->{$key};} ref $decoded_target->{$key} eq "HASH" defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY" and eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
Truth table10%7/69
not(A || B) or C and D and E or F and G and H and not(I) and J or K
A= defined $decoded_target->{$key}B= defined $decoded_source->{$key}C= defined $decoded_target->{$key}D= defined $decoded_source->{$key}E= $decoded_target->{$key} eq $decoded_source->{$key}F= defined $decoded_target->{$key}G= defined $decoded_source->{$key}H= ref $decoded_source->{$key} eq "HASH"I= not %{$decoded_source->{$key};}J= ref $decoded_target->{$key} eq "HASH"K= defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY" and eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
ABCDEFGHIJKresult
00XXXXXXXXX1
1X111XXXXXX1
01111XXXXXX1
1X0XX11111X1
1X10X11111X1
1X11011111X1
010XX11111X1
0110X11111X1
0111011111X1
1X0XX0XXXX11
1X0XX10XXX11
1X0XX110XX11
1X0XX1110X11
1X0XX1111011
1X10X0XXXX11
1X10X10XXX11
1X10X110XX11
1X10X1110X11
1X10X1111011
1X1100XXXX11
1X11010XXX11
1X110110XX11
1X1101110X11
1X1101111011
010XX0XXXX11
010XX10XXX11
010XX110XX11
010XX1110X11
010XX1111011
0110X0XXXX11
0110X10XXX11
0110X110XX11
0110X1110X11
0110X1111011
011100XXXX11
0111010XXX11
01110110XX11
011101110X11
011101111011
1X0XX0XXXX00
1X0XX10XXX00
1X0XX110XX00
1X0XX1110X00
1X0XX1111000
1X10X0XXXX00
1X10X10XXX00
1X10X110XX00
1X10X1110X00
1X10X1111000
1X1100XXXX00
1X11010XXX00
1X110110XX00
1X1101110X00
1X1101111000
010XX0XXXX00
010XX10XXX00
010XX110XX00
010XX1110X00
010XX1111000
0110X0XXXX00
0110X10XXX00
0110X110XX00
0110X1110X00
0110X1111000
011100XXXX00
0111010XXX00
01110110XX00
011101110X00
011101111000
72 (!defined $decoded_target->{$key} && !defined $decoded_source->{$key}) ||
73 (defined $decoded_target->{$key} && defined $decoded_source->{$key} && $decoded_target->{$key} eq $decoded_source->{$key}) ||
74 14 ▶ (defined $decoded_target->{$key} && defined $decoded_source->{$key} && ref $decoded_source->{$key} eq 'HASH' && !%{$decoded_source->{$key}} && ref $decoded_target->{$key} eq 'HASH') ||
Condition88%8/9
defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY" and eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
!ll&&!rl&&r
4242
defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY"
!ll&&!rl&&r
10326
defined $decoded_target->{$key} and defined $decoded_source->{$key}
!ll&&!rl&&r
01038
MC/DC0%0/4
defined $decoded_target->{$key} and defined $decoded_source->{$key} and ref $decoded_source->{$key} eq "ARRAY" and eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
defined $decoded_target->{$key} defined $decoded_source->{$key} ref $decoded_source->{$key} eq "ARRAY" eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
Truth table0%0/5
A and B and C and D
A= defined $decoded_target->{$key}B= defined $decoded_source->{$key}C= ref $decoded_source->{$key} eq "ARRAY"D= eq_deeply($decoded_target->{$key}, $decoded_source->{$key})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
75 (defined $decoded_target->{$key} && defined $decoded_source->{$key} && ref $decoded_source->{$key} eq 'ARRAY' && eq_deeply($decoded_target->{$key}, $decoded_source->{$key}))
76 ) {
77 12 delete $decoded_source->{$key};
78 }
79 }
80 }
81 }
82  
83 106 return $decoded_source;
84 }
85  
86 15 ▶sub json_merge_patch {
Subroutine json_merge_patch: called CC=1 SCAR=0.0
Pod documented
87 15 __PACKAGE__->patch(@_);
88 }
89  
90 23 ▶sub json_merge_diff {
Subroutine json_merge_diff: called CC=1 SCAR=0.0
Pod documented
91 23 __PACKAGE__->diff(@_);
92 }
93  
94  
95 3 1;
96 __END__
↑ Summary