blib/lib/Struct/Diff/MergePatch.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 32 / 32 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 100% 101 / 101 CC 18 SCAR 21.1
CC
18
Cov
100%
diff
12.0
patch
7.0
BEGIN
1.0
CRAP 8.2
↑ Summary
1 package Struct::Diff::MergePatch;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Struct::Diff 0.93;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our @EXPORT_OK = qw(
11 diff
12 patch
13 );
14  
15 =head1 NAME
16  
17 Struct::Diff::MergePatch - JSON Merge Patch
18 (L<rfc7396|https://tools.ietf.org/html/rfc7396>) for perl structures
19  
20 =begin html
21  
22 <a href="https://travis-ci.org/mr-mixas/Struct-Diff-MergePatch.pm"><img src="https://travis-ci.org/mr-mixas/Struct-Diff-MergePatch.pm.svg?branch=master" alt="Travis CI"></a>
23 <a href='https://coveralls.io/github/mr-mixas/Struct-Diff-MergePatch.pm?branch=master'><img src='https://coveralls.io/repos/github/mr-mixas/Struct-Diff-MergePatch.pm/badge.svg?branch=master' alt='Coverage Status'/></a>
24 <a href="https://badge.fury.io/pl/Struct-Diff-MergePatch"><img src="https://badge.fury.io/pl/Struct-Diff-MergePatch.svg" alt="CPAN version"></a>
25  
26 =end html
27  
28 =head1 VERSION
29  
30 Version 0.01
31  
32 =cut
33  
34 3 our $VERSION = '0.01';
35  
36 =head1 SYNOPSIS
37  
38 use Struct::Diff::MergePatch qw(diff patch);
39  
40 $old = {a => {b => 1,c => [0], d => 5},f => 2};
41 $new = {a => {b => 1,c => [0,1],e => 6},f => 2};
42  
43 $diff = diff($old, $new);
44 # {a => {c => [0,1],d => undef,e => 6}}
45  
46 patch($old, $diff);
47 # $old now equal to $new
48  
49 =head1 EXPORT
50  
51 Nothing is exported by default.
52  
53 =head1 SUBROUTINES
54  
55 =head2 diff
56  
57 Calculate patch for two arguments:
58  
59 $patch = diff($old, $new);
60  
61 Convert L<Struct::Diff> diff to merge patch when single arg passed:
62  
63 $patch = diff(Struct::Diff::diff($old, $new));
64  
65 =cut
66  
67 22 ▶sub diff($;$) {
Subroutine diff: called CC=12 SCAR=24.8
Pod documented
68 22 my $patch;
69 22 ▶ my @stack = (\$patch, @_ == 2 ? Struct::Diff::diff($_[0], $_[1]) : $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 21 1 22
70  
71 22 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 31 22 53
72 31 my ($p, $d) = splice @stack, 0, 2; # subpatch, subdiff
73  
74 31 ▶ if (exists $d->{D}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $d->{'D'}) { } 15 16 31
elsif (exists $d->{'N'}) { } 15 1 16
75 15 ▶ if (ref $d->{D} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d->{'D'} eq 'ARRAY') { } 2 13 15
76 2 ${$p} = Struct::Diff::split_diff($d)->{b};
77 } else { # HASH
78 13 ▶ while (my ($k, $v) = each %{$d->{D}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$d{"D"};}) 21 13 34
79 21 ▶ if (exists $v->{D} or exists $v->{N}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $v->{'D'} or exists $v->{'N'}) { } 9 12 21
elsif (exists $v->{'A'}) { } 4 8 12
elsif (exists $v->{'R'}) { } 5 3 8
Condition100%3/3
exists $v->{"D"} or exists $v->{"N"}
l!l&&r!l&&!r
3612
MC/DC100%2/2
exists $v->{"D"} or exists $v->{"N"}
exists $v->{"D"} exists $v->{"N"}
Truth table100%3/3
A or B
A= exists $v->{"D"}B= exists $v->{"N"}
ABresult
1X1
011
000
80 9 push @stack, \${$p}->{$k}, $v;
81 } elsif (exists $v->{A}) {
82 4 ${$p}->{$k} = $v->{A};
83 } elsif (exists $v->{R}) {
84 5 ${$p}->{$k} = undef;
85 }
86 }
87 }
88 } elsif (exists $d->{N}) {
89 15 ${$p} = $d->{N};
90 }
91 }
92  
93 22 ▶ return defined $patch ? $patch : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $patch ? : 18 4 22
94 }
95  
96 =head2 patch
97  
98 Apply patch.
99  
100 patch($target, $patch);
101  
102 =cut
103  
104 23 ▶sub patch($$) {
Subroutine patch: called CC=7 SCAR=19.5
Pod documented
105 23 my @stack = (\$_[0], $_[1]); # ref to alias - to be able to change passed scalar
106  
107 23 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 30 23 53
108 30 my ($t, $p) = splice @stack, 0, 2; # subtarget, subpatch
109  
110 30 ▶ if (ref $p eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'HASH') { } 19 11 30
111 19 ▶ ${$t} = {} unless (ref ${$t} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$t;} eq "HASH" 5 14 19
112  
113 19 ▶ while (my ($k, $v) = each %{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$p;}) 21 19 40
114 21 ▶ if (not defined $v) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $v) { } 6 15 21
elsif (ref $v) { } 7 8 15
115 6 delete ${$t}->{$k};
116 } elsif (ref $v) {
117 7 push @stack, \${$t}->{$k}, $v;
118 } else {
119 8 ${$t}->{$k} = $v;
120 }
121 }
122 } else {
123 11 ${$t} = $p;
124 }
125 }
126 }
127  
128 =head1 AUTHOR
129  
130 Michael Samoglyadov, C<< <mixas at cpan.org> >>
131  
132 =head1 BUGS
133  
134 Please report any bugs or feature requests to
135 C<bug-struct-diff-mergepatch at rt.cpan.org>, or through the web interface at
136 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Struct-Diff-MergePatch>. I
137 will be notified, and then you'll automatically be notified of progress on
138 your bug as I make changes.
139  
140 =head1 SUPPORT
141  
142 You can find documentation for this module with the perldoc command.
143  
144 perldoc Struct::Diff::MergePatch
145  
146 You can also look for information at:
147  
148 =over 4
149  
150 =item * RT: CPAN's request tracker (report bugs here)
151  
152 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Struct-Diff-MergePatch>
153  
154 =item * AnnoCPAN: Annotated CPAN documentation
155  
156 L<http://annocpan.org/dist/Struct-Diff-MergePatch>
157  
158 =item * CPAN Ratings
159  
160 L<http://cpanratings.perl.org/d/Struct-Diff-MergePatch>
161  
162 =item * Search CPAN
163  
164 L<http://search.cpan.org/dist/Struct-Diff-MergePatch/>
165  
166 =back
167  
168 =head1 SEE ALSO
169  
170 L<Struct::Diff>, L<JSON::MergePatch>,
171 L<rfc7396|https://tools.ietf.org/html/rfc7396>
172  
173 =head1 LICENSE AND COPYRIGHT
174  
175 Copyright 2017 Michael Samoglyadov.
176  
177 This program is free software; you can redistribute it and/or modify it under
178 the terms of either: the GNU General Public License as published by the Free
179 Software Foundation; or the Artistic License.
180  
181 See L<http://dev.perl.org/licenses/> for more information.
182  
183 =cut
184  
185 3 1; # End of Struct::Diff::MergePatch
↑ Summary