blib/lib/Struct/Diff.pm

filter: Statementstmt 100% 243 / 243 Branchbran 100% 170 / 170 Conditioncond 100% 39 / 39 MC/DCmcdc 100% 24 / 24 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 100% 497 / 497 CC 104 SCAR 30.9
CC
104
Cov
100%
_diff
40.0
split_diff
18.0
patch
17.0
CRAP 22.0
↑ Summary
1 package Struct::Diff;
2  
3 16 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use Algorithm::Diff qw(LCSidx);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Storable 2.05 qw(freeze);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 our @EXPORT_OK = qw(
14 diff
15 list_diff
16 patch
17 split_diff
18 valid_diff
19 );
20  
21 =head1 NAME
22  
23 Struct::Diff - Recursive diff for nested perl structures
24  
25 =begin html
26  
27 <a href="https://travis-ci.org/mr-mixas/Struct-Diff.pm"><img src="https://travis-ci.org/mr-mixas/Struct-Diff.pm.svg?branch=master" alt="Travis CI"></a>
28 <a href='https://coveralls.io/github/mr-mixas/Struct-Diff.pm?branch=master'><img src='https://coveralls.io/repos/github/mr-mixas/Struct-Diff.pm/badge.svg?branch=master' alt='Coverage Status'/></a>
29 <a href="https://badge.fury.io/pl/Struct-Diff"><img src="https://badge.fury.io/pl/Struct-Diff.svg" alt="CPAN version"></a>
30  
31 =end html
32  
33 =head1 VERSION
34  
35 Version 0.98
36  
37 =cut
38  
39 16 our $VERSION = '0.98';
40  
41 =head1 SYNOPSIS
42  
43 use Struct::Diff qw(diff list_diff split_diff patch valid_diff);
44  
45 $x = {one => [1,{two => 2}]};
46 $y = {one => [1,{two => 9}],three => 3};
47  
48 $diff = diff($x, $y, noO => 1, noU => 1); # omit unchanged items and old values
49 # $diff == {D => {one => {D => [{D => {two => {N => 9}},I => 1}]},three => {A => 3}}}
50  
51 @list_diff = list_diff($diff); # list (path and ref pairs) all diff entries
52 # @list_diff == ({K => ['one']},[1],{K => ['two']}],\{N => 9},[{K => ['three']}],\{A => 3})
53  
54 $splitted = split_diff($diff);
55 # $splitted->{a} # does not exist
56 # $splitted->{b} == {one => [{two => 9}],three => 3}
57  
58 patch($x, $diff); # $x now equal to $y by structure and data
59  
60 @errors = valid_diff($diff);
61  
62 =head1 EXPORT
63  
64 Nothing is exported by default.
65  
66 =head1 DIFF FORMAT
67  
68 Diff is simply a HASH whose keys shows status for each item in passed
69 structures. Every status type (except C<D>) may be omitted during the diff
70 calculation. Disabling some or other types produce different diffs: diff with
71 only unchanged items is also possible (when all other types disabled).
72  
73 =over 4
74  
75 =item A
76  
77 Stands for 'added' (exist only in second structure), it's value - added item.
78  
79 =item D
80  
81 Means 'different' and contains subdiff. The only status type which can't be
82 disabled.
83  
84 =item I
85  
86 Index for array item, used only when prior item was omitted.
87  
88 =item N
89  
90 Is a new value for changed item.
91  
92 =item O
93  
94 Alike C<N>, C<O> is a changed item's old value.
95  
96 =item R
97  
98 Similar for C<A>, but for removed items.
99  
100 =item U
101  
102 Represent unchanged items.
103  
104 =back
105  
106 Diff format: metadata alternates with data and, as a result, diff may
107 represent any structure of any data types. Simple types specified as is,
108 arrays and hashes contain subdiffs for their items with native for such types
109 addressing: indexes for arrays and keys for hashes.
110  
111 Sample:
112  
113 old: {one => [5,7]}
114 new: {one => [5],two => 2}
115 opts: {noU => 1} # omit unchanged items
116  
117 diff:
118 {D => {one => {D => [{I => 1,R => 7}]},two => {A => 2}}}
119 || | | || ||| | | | | || |
120 || | | || ||| | | | | || +- with value 2
121 || | | || ||| | | | | |+- key 'two' was added (A)
122 || | | || ||| | | | | +- subdiff for it
123 || | | || ||| | | | +- another key from top-level hash
124 || | | || ||| | | +- what it was (item's value: 7)
125 || | | || ||| | +- what happened to item (R - removed)
126 || | | || ||| +- array item's actual index
127 || | | || ||+- prior item was omitted
128 || | | || |+- subdiff for array item
129 || | | || +- it's value - ARRAY
130 || | | |+- it is deeply changed
131 || | | +- subdiff for key 'one'
132 || | +- it has key 'one'
133 || +- top-level thing is a HASH
134 |+- changes somewhere deeply inside
135 +- diff is always a HASH
136  
137 =head1 SUBROUTINES
138  
139 =head2 diff
140  
141 Returns recursive diff for two passed things.
142  
143 $diff = diff($x, $y, %opts);
144 $patch = diff($x, $y, noU => 1, noO => 1, trimR => 1); # smallest diff
145  
146 Beware changing diff: it's parts are references to substructures of passed
147 arguments.
148  
149 =head3 Options
150  
151 =over 4
152  
153 =item freezer C<< <sub> >>
154  
155 Serializer callback (redefines default serializer). L<Storable/freeze> is used
156 by default, see L</CONFIGURATION VARIABLES> for details.
157  
158 =item noX C<< <true|false> >>
159  
160 Where X is a status (C<A>, C<N>, C<O>, C<R>, C<U>); such status will be
161 omitted.
162  
163 =item trimR C<< <true|false> >>
164  
165 Drop removed item's data.
166  
167 =back
168  
169 =cut
170  
171 967 ▶our $FREEZER = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 967 local $Storable::canonical = 1; # for equal snapshots for equal by data hashes
173 967 local $Storable::Deparse = 1; # for coderefs
174  
175 967 freeze \$_[0];
176 16 };
177  
178 151 ▶sub diff($$;@) {
Subroutine diff: called CC=2 SCAR=6.9
Pod documented
179 151 my ($x, $y, %opts) = @_;
180  
181 151 ▶ $opts{freezer} = $FREEZER unless (exists $opts{freezer});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"freezer"} 150 1 151
182  
183 151 _diff($x, $y, %opts);
184 }
185  
186 sub _diff($$;@);
187 268 ▶sub _diff($$;@) {
Subroutine _diff: called CC=40 SCAR=36.9
188 268 my ($x, $y, %opts) = @_;
189  
190 268 my $d = {};
191 268 my $type = ref $x;
192  
193 268 ▶ if ($type ne ref $y) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type ne ref $y) { } 10 258 268
elsif ($type eq 'ARRAY' and $x != $y) { } 88 170 258
elsif ($type eq 'HASH' and $x != $y) { } 67 103 170
elsif ($type and $x == $y or $opts{'freezer'}->($x) eq $opts{'freezer'}($y)) { } 18 85 103
194 10 ▶ $d->{O} = $x unless ($opts{noO});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noO"} 9 1 10
195 10 ▶ $d->{N} = $y unless ($opts{noN});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noN"} 9 1 10
196 ▶ } elsif ($type eq 'ARRAY' and $x != $y) {
Condition100%3/3
$type eq "ARRAY" and $x != $y
!ll&&!rl&&r
169188
MC/DC100%2/2
$type eq "ARRAY" and $x != $y
$type eq "ARRAY" $x != $y
Truth table100%3/3
A and B
A= $type eq "ARRAY"B= $x != $y
ABresult
0X0
100
111
197 88 my ($lcs, $stat) = _lcs_diff($x, $y, $opts{freezer});
198  
199 88 ▶ if ($stat->{U} * 3 == @{$lcs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat->{'U'} * 3 == @{$lcs;}) { } 5 83 88
200 5 ▶ $d->{U} = $y unless ($opts{noU});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noU"} 3 2 5
201 } else {
202 83 my ($I, $xi, $yi, $op, $sd) = 0;
203  
204 83 ▶ while (@{$lcs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$lcs;}) 240 83 323
205 240 ($op, $xi, $yi) = splice @{$lcs}, 0, 3;
206  
207 240 ▶ if ($op eq 'U') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'U') { } 110 130 240
elsif ($op eq 'D') { } 67 63 130
elsif ($op eq 'A') { } 33 30 63
208 110 ▶ if ($opts{noU}) { $I++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"noU"}) 44 66 110
209 66 push @{$d->{D}}, { U => $y->[$yi] };
210 } elsif ($op eq 'D') {
211 67 $sd = _diff($x->[$xi], $y->[$yi], %opts);
212 67 ▶ unless (keys %{$sd}) { $I++; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$sd;}) 6 61 67
213 61 push @{$d->{D}}, $sd;
214 } elsif ($op eq 'A') {
215 33 ▶ if ($opts{noA}) { $I++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"noA"}) 6 27 33
216 27 push @{$d->{D}}, { A => $y->[$yi] };
217 } else {
218 30 ▶ if ($opts{noR}) { $I++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"noR"}) 4 26 30
219 26 ▶ push @{$d->{D}}, { R => $opts{trimR} ? undef : $x->[$xi] };
Branch: 1 branch
BranchTrueFalseTotal
$opts{'trimR'} ? : 2 24 26
220 }
221  
222 180 ▶ if ($I) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I) 32 148 180
223 32 $d->{D}->[-1]->{I} = $xi;
224 32 $I = 0;
225 }
226 }
227 }
228 ▶ } elsif ($type eq 'HASH' and $x != $y) {
Condition100%3/3
$type eq "HASH" and $x != $y
!ll&&!rl&&r
101267
MC/DC100%2/2
$type eq "HASH" and $x != $y
$type eq "HASH" $x != $y
Truth table100%3/3
A and B
A= $type eq "HASH"B= $x != $y
ABresult
0X0
100
111
229 67 my @keys = keys %{{ %{$x}, %{$y} }}; # uniq keys for both hashes
230 67 ▶ return $opts{noU} ? {} : { U => {} } unless (@keys);
Branch: 2 branches
BranchTrueFalseTotal
unless @keys 2 65 67
$opts{'noU'} ? : 1 1 2
231  
232 65 for my $k (@keys) {
233 107 ▶ if (exists $x->{$k} and exists $y->{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $x->{$k} and exists $y->{$k}) { } 75 32 107
elsif (exists $x->{$k}) { } 18 14 32
Condition100%3/3
exists $x->{$k} and exists $y->{$k}
!ll&&!rl&&r
141875
MC/DC100%2/2
exists $x->{$k} and exists $y->{$k}
exists $x->{$k} exists $y->{$k}
Truth table100%3/3
A and B
A= exists $x->{$k}B= exists $y->{$k}
ABresult
0X0
100
111
234 75 ▶ if ($opts{freezer}($x->{$k}) eq $opts{freezer}($y->{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'freezer'}->($x->{$k}) eq $opts{'freezer'}->($y->{$k})) { } 25 50 75
235 25 ▶ $d->{U}->{$k} = $y->{$k} unless ($opts{noU});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noU"} 17 8 25
236 } else {
237 50 my $sd = _diff($x->{$k}, $y->{$k}, %opts);
238 50 ▶ $d->{D}->{$k} = $sd if (keys %{$sd});
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$sd;} 48 2 50
239 }
240 } elsif (exists $x->{$k}) {
241 $d->{D}->{$k}->{R} = $opts{trimR} ? undef : $x->{$k}
242 18 ▶ unless ($opts{noR});
Branch: 2 branches
BranchTrueFalseTotal
unless $opts{"noR"} 16 2 18
$opts{'trimR'} ? : 2 14 16
243 } else {
244 14 ▶ $d->{D}->{$k}->{A} = $y->{$k} unless ($opts{noA});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noA"} 12 2 14
245 }
246 }
247  
248 65 ▶ if (exists $d->{U} and exists $d->{D}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d->{"U"} and exists $d->{"D"}) 12 53 65
Condition100%3/3
exists $d->{"U"} and exists $d->{"D"}
!ll&&!rl&&r
51212
MC/DC100%2/2
exists $d->{"U"} and exists $d->{"D"}
exists $d->{"U"} exists $d->{"D"}
Truth table100%3/3
A and B
A= exists $d->{"U"}B= exists $d->{"D"}
ABresult
0X0
100
111
249 12 map { $d->{D}->{$_}->{U} = $d->{U}->{$_} } keys %{$d->{U}};
250 12 delete $d->{U};
251 }
252 ▶ } elsif ($type && $x == $y || $opts{freezer}($x) eq $opts{freezer}($y)) {
Condition100%6/6
$type and $x == $y or $opts{"freezer"}->($x) eq $opts{"freezer"}($y)
l!l&&r!l&&!r
71185
$type and $x == $y
!ll&&!rl&&r
80167
MC/DC100%3/3
$type and $x == $y or $opts{"freezer"}->($x) eq $opts{"freezer"}($y)
$type $x == $y $opts{"freezer"}->($x) eq $opts{"freezer"}($y)
Truth table100%5/5
A and B or C
A= $typeB= $x == $yC= $opts{"freezer"}->($x) eq $opts{"freezer"}($y)
ABCresult
11X1
0X11
1011
0X00
1000
253 18 ▶ $d->{U} = $x unless ($opts{noU});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noU"} 16 2 18
254 } else {
255 85 ▶ $d->{O} = $x unless ($opts{noO});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noO"} 73 12 85
256 85 ▶ $d->{N} = $y unless ($opts{noN});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"noN"} 77 8 85
257 }
258  
259 266 return $d;
260 }
261  
262 88 ▶sub _lcs_diff {
Subroutine _lcs_diff: called CC=8 SCAR=20.8
263 88 my ($xm, $ym) = LCSidx(@_);
264 88 my ($xi, $yi, @diff, %stat) = (0, 0);
265  
266 # additional unchanged items to collect trailing non-matched
267 88 push @{$xm}, scalar @{$_[0]};
268 88 push @{$ym}, scalar @{$_[1]};
269  
270 88 ▶ while (@{$xm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$xm;}) 331 88 419
271 331 ▶ if ($xi == $xm->[0] and $yi == $ym->[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($xi == $xm->[0] and $yi == $ym->[0]) { } 201 130 331
elsif ($xi < $xm->[0] and $yi < $ym->[0]) { } 67 63 130
elsif ($xi < $xm->[0]) { } 30 33 63
Condition100%3/3
$xi == $xm->[0] and $yi == $ym->[0]
!ll&&!rl&&r
9733201
MC/DC100%2/2
$xi == $xm->[0] and $yi == $ym->[0]
$xi == $xm->[0] $yi == $ym->[0]
Truth table100%3/3
A and B
A= $xi == $xm->[0]B= $yi == $ym->[0]
ABresult
0X0
100
111
272 201 push @diff, 'U', shift @{$xm}, shift @{$ym};
273 201 $xi++; $yi++;
274 201 $stat{U}++;
275 ▶ } elsif ($xi < $xm->[0] and $yi < $ym->[0]) {
Condition100%3/3
$xi < $xm->[0] and $yi < $ym->[0]
!ll&&!rl&&r
333067
MC/DC100%2/2
$xi < $xm->[0] and $yi < $ym->[0]
$xi < $xm->[0] $yi < $ym->[0]
Truth table100%3/3
A and B
A= $xi < $xm->[0]B= $yi < $ym->[0]
ABresult
0X0
100
111
276 67 push @diff, 'D', $xi++, $yi++;
277 67 $stat{N}++;
278 } elsif ($xi < $xm->[0]) {
279 30 push @diff, 'R', $xi++, $yi;
280 30 $stat{R}++;
281 } else {
282 33 push @diff, 'A', $xi, $yi++;
283 33 $stat{A}++;
284 }
285 }
286  
287 88 ▶ $stat{O} = $stat{N} if (exists $stat{N});
Branch: 1 branch
BranchTrueFalseTotal
if exists $stat{"N"} 48 40 88
288  
289 # remove added above trailing item
290 88 splice @diff, -3, 3;
291 88 $stat{U}--;
292  
293 88 return \@diff, \%stat;
294 }
295  
296 =head2 list_diff
297  
298 List all pairs (path-to-subdiff, ref-to-subdiff) for provided diff. See
299 L<Struct::Path/ADDRESSING SCHEME> for path format specification.
300  
301 @list = list_diff($diff);
302  
303 =head3 Options
304  
305 =over 4
306  
307 =item depth C<< <int> >>
308  
309 Don't dive deeper than defined number of levels; C<undef> used by default
310 (unlimited).
311  
312 =item sort C<< <sub|true|false> >>
313  
314 Defines how to handle hash subdiffs. Keys will be picked randomly (default
315 C<keys> behavior), sorted by provided subroutine (if value is a coderef) or
316 lexically sorted if set to some other true value.
317  
318 =back
319  
320 =cut
321  
322 8 ▶sub list_diff($;@) {
Subroutine list_diff: called CC=10 SCAR=23.0
Pod documented
323 8 my @stack = ([], \shift); # init: (path, diff)
324 8 my %opts = @_;
325 8 my ($diff, @list, $path, $I);
326  
327 8 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 36 8 44
328 36 ($path, $diff) = splice @stack, -2, 2;
329  
330 36 ▶ if (!exists ${$diff}->{D} or $opts{depth} and @{$path} >= $opts{depth}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists ${$diff;}->{'D'} or $opts{'depth'} and @{$path;} >= $opts{'depth'}) { } 18 18 36
elsif (ref ${$diff;}->{'D'} eq 'ARRAY') { } 8 10 18
Condition100%6/6
not exists ${$diff;}->{"D"} or $opts{"depth"} and @{$path;} >= $opts{"depth"}
l!l&&r!l&&!r
16218
$opts{"depth"} and @{$path;} >= $opts{"depth"}
!ll&&!rl&&r
1532
MC/DC100%3/3
not exists ${$diff;}->{"D"} or $opts{"depth"} and @{$path;} >= $opts{"depth"}
not exists ${$diff;}->{"D"} $opts{"depth"} @{$path;} >= $opts{"depth"}
Truth table100%4/4
not(A) or B and C
A= not exists ${$diff;}->{"D"}B= $opts{"depth"}C= @{$path;} >= $opts{"depth"}
ABCresult
1XX1
0111
00X0
0100
331 18 unshift @list, $path, $diff;
332 18 } elsif (ref ${$diff}->{D} eq 'ARRAY') {
333 8 $I = 0;
334 8 for (@{${$diff}->{D}}) {
335 14 ▶ $I = $_->{I} if (exists $_->{I}); # use provided index
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"I"} 1 13 14
336 14 push @stack, [@{$path}, [$I]], \$_;
337 14 $I++;
338 }
339 } else { # HASH
340 map {
341 14 ▶ push @stack, [@{$path}, {K => [$_]}], \${$diff}->{D}->{$_}
Branch: 2 branches
BranchTrueFalseTotal
$opts{'sort'} ? : 2 8 10
ref $opts{'sort'} eq 'CODE' ? : 1 1 2
342 } $opts{sort}
343 ? ref $opts{sort} eq 'CODE'
344 1 ? $opts{sort}(keys %{${$diff}->{D}})
345 1 : sort keys %{${$diff}->{D}}
346 10 : keys %{${$diff}->{D}};
347 }
348 }
349  
350 8 return @list;
351 }
352  
353 =head2 split_diff
354  
355 Divide diff to pseudo original structures.
356  
357 $structs = split_diff(diff($x, $y));
358 # $structs->{a}: items from $x
359 # $structs->{b}: items from $y
360  
361 =cut
362  
363 sub split_diff($);
364 65 ▶sub split_diff($) {
Subroutine split_diff: called CC=18 SCAR=28.9
Pod documented
365 65 my $d = $_[0];
366 65 my (%out, $sd);
367  
368 65 ▶ if (exists $d->{D}) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $d->{'D'}) { } 18 47 65
elsif (exists $d->{'U'}) { } 14 33 47
elsif (exists $d->{'A'}) { } 8 25 33
elsif (exists $d->{'R'}) { } 8 17 25
369 18 ▶ if (ref $d->{D} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d->{'D'} eq 'ARRAY') { } 10 8 18
370 10 for (@{$d->{D}}) {
371 22 $sd = split_diff($_);
372 22 ▶ push @{$out{a}}, $sd->{a} if (exists $sd->{a});
Branch: 1 branch
BranchTrueFalseTotal
if exists $sd->{"a"} 20 2 22
373 22 ▶ push @{$out{b}}, $sd->{b} if (exists $sd->{b});
Branch: 1 branch
BranchTrueFalseTotal
if exists $sd->{"b"} 20 2 22
374 }
375  
376 10 ▶ $out{a} = [] unless (exists $out{a});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out{"a"} 1 9 10
377 10 ▶ $out{b} = [] unless (exists $out{b});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out{"b"} 1 9 10
378 } else { # HASH
379 8 for (keys %{$d->{D}}) {
380 29 $sd = split_diff($d->{D}->{$_});
381 29 ▶ $out{a}->{$_} = $sd->{a} if (exists $sd->{a});
Branch: 1 branch
BranchTrueFalseTotal
if exists $sd->{"a"} 23 6 29
382 29 ▶ $out{b}->{$_} = $sd->{b} if (exists $sd->{b});
Branch: 1 branch
BranchTrueFalseTotal
if exists $sd->{"b"} 23 6 29
383 }
384  
385 8 ▶ $out{a} = {} unless (exists $out{a});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out{"a"} 1 7 8
386 8 ▶ $out{b} = {} unless (exists $out{b});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out{"b"} 1 7 8
387 }
388 } elsif (exists $d->{U}) {
389 14 $out{a} = $out{b} = $d->{U};
390 } elsif (exists $d->{A}) {
391 8 $out{b} = $d->{A};
392 } elsif (exists $d->{R}) {
393 8 $out{a} = $d->{R};
394 } else {
395 17 ▶ $out{b} = $d->{N} if (exists $d->{N});
Branch: 1 branch
BranchTrueFalseTotal
if exists $d->{"N"} 16 1 17
396 17 ▶ $out{a} = $d->{O} if (exists $d->{O});
Branch: 1 branch
BranchTrueFalseTotal
if exists $d->{"O"} 16 1 17
397 }
398  
399 65 return \%out;
400 }
401  
402 =head2 patch
403  
404 Apply diff.
405  
406 patch($target, $diff);
407  
408 =cut
409  
410 112 ▶sub patch($$) {
Subroutine patch: called CC=17 SCAR=28.3
Pod documented
411 112 my @stack = (\$_[0], $_[1]); # ref to alias - to be able to change passed scalar
412  
413 112 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 169 111 280
414 169 my ($s, $d) = splice @stack, 0, 2; # struct, subdiff
415  
416 169 ▶ if (exists $d->{D}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $d->{'D'}) { } 83 86 169
elsif (exists $d->{'N'}) { } 61 25 86
417 83 ▶ croak "Structure does not match" unless (ref ${$s} eq ref $d->{D});
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$s;} eq ref $d->{"D"} 1 82 83
418  
419 82 ▶ if (ref $d->{D} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d->{'D'} eq 'ARRAY') { } 50 32 82
420 50 my ($i, $j) = (0, 0); # target array idx, jitter
421  
422 50 for (@{$d->{D}}) {
423 122 ▶ $i = $_->{I} + $j if (exists $_->{I});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"I"} 20 102 122
424  
425 122 ▶ if (exists $_->{D} or exists $_->{N}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $_->{'D'} or exists $_->{'N'}) { } 37 85 122
elsif (exists $_->{'A'}) { } 25 60 85
elsif (exists $_->{'R'}) { } 24 36 60
Condition100%3/3
exists $_->{"D"} or exists $_->{"N"}
l!l&&r!l&&!r
73085
MC/DC100%2/2
exists $_->{"D"} or exists $_->{"N"}
exists $_->{"D"} exists $_->{"N"}
Truth table100%3/3
A or B
A= exists $_->{"D"}B= exists $_->{"N"}
ABresult
1X1
011
000
426 37 push @stack, \${$s}->[$i], $_;
427 } elsif (exists $_->{A}) {
428 25 splice @{${$s}}, $i, 0, $_->{A};
429 25 $j++;
430 } elsif (exists $_->{R}) {
431 24 splice @{${$s}}, $i, 1;
432 24 $j--;
433 24 next; # don't increment $i
434 }
435  
436 98 $i++;
437 }
438 } else { # HASH
439 32 ▶ while (my ($k, $v) = each %{$d->{D}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$d{"D"};}) 42 32 74
440 42 ▶ if (exists $v->{D} or exists $v->{N}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $v->{'D'} or exists $v->{'N'}) { } 20 22 42
elsif (exists $v->{'A'}) { } 6 16 22
elsif (exists $v->{'R'}) { } 10 6 16
Condition100%3/3
exists $v->{"D"} or exists $v->{"N"}
l!l&&r!l&&!r
16422
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
441 20 push @stack, \${$s}->{$k}, $v;
442 } elsif (exists $v->{A}) {
443 6 ${$s}->{$k} = $v->{A};
444 } elsif (exists $v->{R}) {
445 10 delete ${$s}->{$k};
446 }
447 }
448 }
449 } elsif (exists $d->{N}) {
450 61 ${$s} = $d->{N};
451 }
452 }
453 }
454  
455 =head2 valid_diff
456  
457 Validate diff structure. In scalar context returns C<1> for valid diff,
458 C<undef> otherwise. In list context returns list of pairs (path, type) for
459 each error. See L<Struct::Path/ADDRESSING SCHEME> for path format
460 specification.
461  
462 @errors_list = valid_diff($diff); # list context
463  
464 or
465  
466 $is_valid = valid_diff($diff); # scalar context
467  
468 =cut
469  
470 143 ▶sub valid_diff($) {
Subroutine valid_diff: called CC=15 SCAR=27.1
Pod documented
471 143 my @stack = ([], shift); # (path, diff)
472 143 my ($diff, @errs, $path);
473  
474 143 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 342 139 481
475 342 ($path, $diff) = splice @stack, 0, 2;
476  
477 342 ▶ unless (ref $diff eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $diff eq "HASH") 5 337 342
478 5 ▶ return undef unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 4 5
479 4 push @errs, $path, 'BAD_DIFF_TYPE';
480 4 next;
481 }
482  
483 337 ▶ if (exists $diff->{D}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $diff->{"D"}) 104 233 337
484 104 ▶ if (ref $diff->{D} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $diff->{'D'} eq 'ARRAY') { } 62 42 104
elsif (ref $diff->{'D'} eq 'HASH') { } 35 7 42
485 map {
486 153 unshift @stack, [@{$path}, [$_]], $diff->{D}->[$_]
487 62 } 0 .. $#{$diff->{D}};
488 } elsif (ref $diff->{D} eq 'HASH') {
489 map {
490 46 unshift @stack, [@{$path}, {K => [$_]}], $diff->{D}->{$_}
491 35 } sort keys %{$diff->{D}};
492 } else {
493 7 ▶ return undef unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 6 7
494 6 unshift @errs, $path, 'BAD_D_TYPE';
495 }
496 }
497  
498 336 ▶ if (exists $diff->{I}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $diff->{"I"}) 37 299 336
499 37 ▶ if (!looks_like_number($diff->{I}) or int($diff->{I}) != $diff->{I}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &looks_like_number($diff->{"I"}) or int $diff->{"I"} != $diff->{"I"}) 6 31 37
Condition100%3/3
not &looks_like_number($diff->{"I"}) or int $diff->{"I"} != $diff->{"I"}
l!l&&r!l&&!r
5131
MC/DC100%2/2
not &looks_like_number($diff->{"I"}) or int $diff->{"I"} != $diff->{"I"}
not &looks_like_number($diff->{"I"}) int $diff->{"I"} != $diff->{"I"}
Truth table100%3/3
not(A) or B
A= not &looks_like_number($diff->{"I"})B= int $diff->{"I"} != $diff->{"I"}
ABresult
1X1
011
000
500 6 ▶ return undef unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 5 6
501 5 unshift @errs, $path, 'BAD_I_TYPE';
502 }
503  
504 36 ▶ if (keys %{$diff} < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$diff;} < 2) 2 34 36
505 2 ▶ return undef unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 1 2
506 1 unshift @errs, $path, 'LONESOME_I';
507 }
508 }
509 }
510  
511 139 ▶ return wantarray ? @errs : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 130 139
512 }
513  
514 =head1 CONFIGURATION VARIABLES
515  
516 =over 4
517  
518 =item $Struct::Diff::FREEZER
519  
520 Contains reference to default serialization function (C<diff()> rely on it
521 to determine data equivalency). L<Storable/freeze> with enabled
522 C<$Storable::canonical> and C<$Storable::Deparse> opts used by default.
523  
524 L<Data::Dumper> is suitable for structures with regular expressions:
525  
526 use Data::Dumper;
527  
528 $Struct::Diff::FREEZER = sub {
529 local $Data::Dumper::Deparse = 1;
530 local $Data::Dumper::Sortkeys = 1;
531 local $Data::Dumper::Terse = 1;
532  
533 return Dumper @_;
534 }
535  
536 But comparing to L<Storable> it has two another issues: speed and unability
537 to distinguish numbers from their string representations.
538  
539 =back
540  
541 =head1 LIMITATIONS
542  
543 Only arrays and hashes traversed. All other types compared by reference
544 addresses and serialized content.
545  
546 L<Storable/freeze> (serializer used by default) will fail serializing compiled
547 regexps, so, consider to use other serializer if data contains regular
548 expressions. See L<CONFIGURATION VARIABLES> for details.
549  
550 Struct::Diff will fail on structures with loops in references;
551 C<has_circular_ref> from L<Data::Structure::Util> can help to detect such
552 structures.
553  
554 =head1 AUTHOR
555  
556 Michael Samoglyadov, C<< <mixas at cpan.org> >>
557  
558 =head1 BUGS
559  
560 Please report any bugs or feature requests to
561 C<bug-struct-diff at rt.cpan.org>, or through the web interface at
562 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Struct-Diff>. I will be
563 notified, and then you'll automatically be notified of progress on your bug as
564 I make changes.
565  
566 =head1 SUPPORT
567  
568 You can find documentation for this module with the perldoc command.
569  
570 perldoc Struct::Diff
571  
572 You can also look for information at:
573  
574 =over 4
575  
576 =item * RT: CPAN's request tracker (report bugs here)
577  
578 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Struct-Diff>
579  
580 =item * AnnoCPAN: Annotated CPAN documentation
581  
582 L<http://annocpan.org/dist/Struct-Diff>
583  
584 =item * CPAN Ratings
585  
586 L<http://cpanratings.perl.org/d/Struct-Diff>
587  
588 =item * Search CPAN
589  
590 L<http://search.cpan.org/dist/Struct-Diff/>
591  
592 =back
593  
594 =head1 SEE ALSO
595  
596 L<Algorithm::Diff>, L<Data::Deep>, L<Data::Diff>, L<Data::Difference>,
597 L<JSON::Patch>, L<JSON::MergePatch>, L<Struct::Diff::MergePatch>
598  
599 L<Data::Structure::Util>, L<Struct::Path>, L<Struct::Path::PerlStyle>
600  
601 =head1 LICENSE AND COPYRIGHT
602  
603 Copyright 2015-2019 Michael Samoglyadov.
604  
605 This program is free software; you can redistribute it and/or modify it
606 under the terms of either: the GNU General Public License as published
607 by the Free Software Foundation; or the Artistic License.
608  
609 See L<http://dev.perl.org/licenses/> for more information.
610  
611 =cut
612  
613 16 1; # End of Struct::Diff
↑ Summary