| 1 | package JSON::Patch; | ||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Struct::Diff 0.96; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Struct::Path 0.82 qw(path); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Struct::Path::JsonPointer 0.04 qw(path2str str2path); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||
| 14 | diff | ||||||||||||||||||||||||||||||||||||||||
| 15 | patch | ||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||
| 20 | JSON::Patch - JSON Patch (rfc6902) for perl structures | ||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||
| 22 | =begin html | ||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||
| 24 | <a href="https://travis-ci.org/mr-mixas/JSON-Patch.pm"><img src="https://travis-ci.org/mr-mixas/JSON-Patch.pm.svg?branch=master" alt="Travis CI"></a> | ||||||||||||||||||||||||||||||||||||||||
| 25 | <a href='https://coveralls.io/github/mr-mixas/JSON-Patch.pm?branch=master'><img src='https://coveralls.io/repos/github/mr-mixas/JSON-Patch.pm/badge.svg?branch=master' alt='Coverage Status'/></a> | ||||||||||||||||||||||||||||||||||||||||
| 26 | <a href="https://badge.fury.io/pl/JSON-Patch"><img src="https://badge.fury.io/pl/JSON-Patch.svg" alt="CPAN version"></a> | ||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||
| 28 | =end html | ||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||
| 32 | Version 0.04 | ||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||
| 38 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||
| 40 | use Test::More tests => 2; | ||||||||||||||||||||||||||||||||||||||||
| 41 | use JSON::Patch qw(diff patch); | ||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||
| 43 | my $old = {foo => ['bar']}; | ||||||||||||||||||||||||||||||||||||||||
| 44 | my $new = {foo => ['bar', 'baz']}; | ||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||
| 46 | my $patch = diff($old, $new); | ||||||||||||||||||||||||||||||||||||||||
| 47 | is_deeply( | ||||||||||||||||||||||||||||||||||||||||
| 48 | $patch, | ||||||||||||||||||||||||||||||||||||||||
| 49 | [ | ||||||||||||||||||||||||||||||||||||||||
| 50 | {op => 'add', path => '/foo/1', value => 'baz'} | ||||||||||||||||||||||||||||||||||||||||
| 51 | ] | ||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||
| 54 | patch($old, $patch); | ||||||||||||||||||||||||||||||||||||||||
| 55 | is_deeply($old, $new); | ||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||
| 57 | =head1 EXPORT | ||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||
| 59 | Nothing is exported by default. | ||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||
| 61 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||
| 63 | =head2 diff | ||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||
| 65 | Calculate patch for two arguments: | ||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||
| 67 | $patch = diff($old, $new); | ||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||
| 69 | Convert L<Struct::Diff> diff to JSON Patch when single arg passed: | ||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||
| 71 | require Struct::Diff; | ||||||||||||||||||||||||||||||||||||||||
| 72 | $patch = diff(Struct::Diff::diff($old, $new)); | ||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | sub diff($;$) { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | my $diff = @_ == 2 | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 78 | ? Struct::Diff::diff($_[0], $_[1], noO => 1, noU => 1, trimR => 1) | ||||||||||||||||||||||||||||||||||||||||
| 79 | : $_[0]; | ||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | my @stask = Struct::Diff::list_diff($diff, sort => 1); | |||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | my ($hunk, @patch, $path); | |||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | while (@stask) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 85 | 13 | ($path, $hunk) = splice @stask, -2, 2; | |||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||
| 87 | 13 | ▶ | if (exists ${$hunk}->{A}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | push @patch, {op => 'add', value => ${$hunk}->{A}}; | |||||||||||||||||||||||||||||||||||||||
| 89 | 11 | } elsif (exists ${$hunk}->{N}) { | |||||||||||||||||||||||||||||||||||||||
| 90 | 10 | push @patch, {op => 'replace', value => ${$hunk}->{N}}; | |||||||||||||||||||||||||||||||||||||||
| 91 | 1 | } elsif (exists ${$hunk}->{R}) { | |||||||||||||||||||||||||||||||||||||||
| 92 | 0 | push @patch, {op => 'remove'}; | |||||||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | next; | |||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||
| 97 | 12 | $patch[-1]->{path} = path2str($path); | |||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | return \@patch; | |||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||
| 103 | =head2 patch | ||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||
| 105 | Apply patch. | ||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||
| 107 | patch($target, $patch); | ||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||
| 111 | 12 | ▶ | sub patch($$) { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
patch: called CC=20 SCAR=31.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 112 | 12 | ▶ | croak "Arrayref expected for patch" unless (ref $_[1] eq 'ARRAY'); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||
| 114 | 11 | for my $hunk (@{$_[1]}) { | |||||||||||||||||||||||||||||||||||||||
| 115 | 11 | ▶ | croak "Hashref expected for patch item" unless (ref $hunk eq 'HASH'); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | ▶ | croak "Undefined op value" unless (defined $hunk->{op}); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 117 | 9 | ▶ | croak "Path parameter missing" unless (exists $hunk->{path}); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||
| 119 | 8 | ▶ | my $path = eval { str2path($hunk->{path}) } | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 120 | or croak "Failed to parse 'path' pointer"; | ||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||
| 122 | 7 | ▶ | if ($hunk->{op} eq 'add' or $hunk->{op} eq 'replace') { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition66%2/3
$hunk->{"op"} eq "add" or $hunk->{"op"} eq "replace"
MC/DC50%1/2
$hunk->{"op"} eq "add" or $hunk->{"op"} eq "replace"
$hunk->{"op"} eq "add"
$hunk->{"op"} eq "replace"
Truth table66%2/3
A or B
A= $hunk->{"op"} eq "add"B= $hunk->{"op"} eq "replace"
| |||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | ▶ | croak "Value parameter missing" unless (exists $hunk->{value}); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 124 | path( | ||||||||||||||||||||||||||||||||||||||||
| 125 | $_[0], | ||||||||||||||||||||||||||||||||||||||||
| 126 | $path, | ||||||||||||||||||||||||||||||||||||||||
| 127 | assign => $hunk->{value}, | ||||||||||||||||||||||||||||||||||||||||
| 128 | expand => 1, | ||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | insert => $hunk->{op} eq 'add', | |||||||||||||||||||||||||||||||||||||||
| 130 | strict => 1, | ||||||||||||||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||
| 133 | } elsif ($hunk->{op} eq 'remove') { | ||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | eval { path($_[0], $path, delete => 1) } or | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 135 | croak "Path does not exist"; | ||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||
| 137 | ▶ | } elsif ($hunk->{op} eq 'move' or $hunk->{op} eq 'copy') { | |||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$hunk->{"op"} eq "move" or $hunk->{"op"} eq "copy"
MC/DC100%2/2
$hunk->{"op"} eq "move" or $hunk->{"op"} eq "copy"
$hunk->{"op"} eq "move"
$hunk->{"op"} eq "copy"
Truth table100%3/3
A or B
A= $hunk->{"op"} eq "move"B= $hunk->{"op"} eq "copy"
| |||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | my $from = eval { str2path($hunk->{from}) } or | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 139 | croak "Failed to parse 'from' pointer"; | ||||||||||||||||||||||||||||||||||||||||
| 140 | my @found = path( | ||||||||||||||||||||||||||||||||||||||||
| 141 | $_[0], | ||||||||||||||||||||||||||||||||||||||||
| 142 | $from, | ||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | delete => $hunk->{op} eq 'move', | |||||||||||||||||||||||||||||||||||||||
| 144 | deref => 1 | ||||||||||||||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | croak "Source path does not exist" unless (@found); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | path($_[0], $path, assign => $found[0], expand => 1); | |||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||
| 150 | } elsif ($hunk->{op} eq 'test') { | ||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | ▶ | croak "Value parameter missing" unless (exists $hunk->{value}); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | ▶ | my @found = path($_[0], $path, deref => 1) or | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 153 | croak "Path does not exist"; | ||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $diff = Struct::Diff::diff($found[0], $hunk->{value}, noU => 1); | |||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | croak "Test failed" if (keys %{$diff}); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | croak "Unsupported op '$hunk->{op}'"; | |||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||
| 163 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||
| 165 | Michael Samoglyadov, C<< <mixas at cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||
| 167 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||
| 169 | Please report any bugs or feature requests to C<bug-json-patch at rt.cpan.org>, | ||||||||||||||||||||||||||||||||||||||||
| 170 | or through the web interface at | ||||||||||||||||||||||||||||||||||||||||
| 171 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Patch>. I will be | ||||||||||||||||||||||||||||||||||||||||
| 172 | notified, and then you'll automatically be notified of progress on your bug as | ||||||||||||||||||||||||||||||||||||||||
| 173 | I make changes. | ||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||
| 175 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||
| 177 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||
| 179 | perldoc JSON::Patch | ||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||
| 181 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||
| 183 | =over 4 | ||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||
| 185 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||
| 187 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=JSON-Patch> | ||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||
| 189 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||
| 191 | L<http://annocpan.org/dist/JSON-Patch> | ||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||
| 193 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||
| 195 | L<http://cpanratings.perl.org/d/JSON-Patch> | ||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||
| 197 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||
| 199 | L<http://search.cpan.org/dist/JSON-Patch/> | ||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||
| 201 | =back | ||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||
| 203 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||
| 205 | L<rfc6902|https://tools.ietf.org/html/rfc6902>, | ||||||||||||||||||||||||||||||||||||||||
| 206 | L<Struct::Diff>, L<Struct::Diff::MergePatch> | ||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||
| 208 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||
| 210 | Copyright 2018 Michael Samoglyadov. | ||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||
| 212 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||
| 213 | the terms of either: the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||||||||||||||
| 214 | Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||
| 216 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||
| 220 | 6 | 1; # End of JSON::Patch | |||||||||||||||||||||||||||||||||||||||