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