Skip to content

Commit

Permalink
Struct::Diff diff conversion supported in diff()
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Oct 16, 2018
1 parent 6be66db commit 8c180e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -4,6 +4,7 @@ Makefile.PL
MANIFEST This list of files
README
t/00-load.t
t/diff.t
t/errors.t
t/manifest.t
t/pod-coverage.t
Expand Down
16 changes: 11 additions & 5 deletions lib/JSON/Patch.pm
Expand Up @@ -62,15 +62,21 @@ Nothing is exported by default.
=head2 diff
Calculate patch.
Calculate patch for two arguments:
$patch = diff($old, $new);
Convert L<Struct::Diff> diff to JSON Patch when single arg passed:
require Struct::Diff;
$patch = diff(Struct::Diff::diff($old, $new));
=cut

sub diff($$) {
my @stask = Struct::Diff::list_diff
Struct::Diff::diff($_[0], $_[1], noO => 1, noU => 1, trimR => 1);
my @stask = Struct::Diff::list_diff @_ == 2
? Struct::Diff::diff($_[0], $_[1], noO => 1, noU => 1, trimR => 1)
: $_[0];

my ($hunk, @patch, $path);

Expand Down Expand Up @@ -101,7 +107,7 @@ Apply patch.
=cut

sub patch($$) {
sub patch($;$) {
croak "Arrayref expected for patch" unless (ref $_[1] eq 'ARRAY');

for my $hunk (@{$_[1]}) {
Expand Down Expand Up @@ -195,7 +201,7 @@ L<http://search.cpan.org/dist/JSON-Patch/>
=head1 SEE ALSO
L<rfc6902|https://tools.ietf.org/html/rfc6902>
L<rfc6902|https://tools.ietf.org/html/rfc6902>,
L<Struct::Diff>, L<Struct::Diff::MergePatch>
=head1 LICENSE AND COPYRIGHT
Expand Down
23 changes: 23 additions & 0 deletions t/diff.t
@@ -0,0 +1,23 @@
#!perl -T

use strict;
use warnings FATAL => 'all';

use Test::More tests => 1;

require Struct::Diff;
require JSON::Patch;

my $patch = JSON::Patch::diff(
Struct::Diff::diff(
{foo => ['bar']},
{foo => ['bar', 'baz']}
)
);
is_deeply(
$patch,
[
{op => 'add', path => '/foo/1', value => 'baz'}
]
);

0 comments on commit 8c180e7

Please sign in to comment.