| 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
| |||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | 22 | ▶ | while (@stack) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | 31 | my ($p, $d) = splice @stack, 0, 2; # subpatch, subdiff | |||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||
| 74 | 31 | ▶ | if (exists $d->{D}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 75 | 15 | ▶ | if (ref $d->{D} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 76 | 2 | ${$p} = Struct::Diff::split_diff($d)->{b}; | |||||||||||||||||||||||||||||||||||
| 77 | } else { # HASH | ||||||||||||||||||||||||||||||||||||
| 78 | 13 | ▶ | while (my ($k, $v) = each %{$d->{D}}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | 21 | ▶ | if (exists $v->{D} or exists $v->{N}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
exists $v->{"D"} or exists $v->{"N"}
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"}
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 108 | 30 | my ($t, $p) = splice @stack, 0, 2; # subtarget, subpatch | |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 30 | ▶ | if (ref $p eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 111 | 19 | ▶ | ${$t} = {} unless (ref ${$t} eq 'HASH'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | 19 | ▶ | while (my ($k, $v) = each %{$p}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 114 | 21 | ▶ | if (not defined $v) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||