Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ndproc: Patch (nested diff) module added
- Loading branch information
Showing
14 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package App::NDTools::NDProc::Module::Patch; | ||
|
||
use strict; | ||
use warnings FATAL => 'all'; | ||
use parent 'App::NDTools::NDProc::Module'; | ||
|
||
use Log::Log4Cli; | ||
use Struct::Diff qw(patch); | ||
use Struct::Path 0.80 qw(path); | ||
|
||
our $VERSION = '0.01'; | ||
|
||
sub MODINFO { "Apply nested diff to the structure" } | ||
|
||
sub arg_opts { | ||
my $self = shift; | ||
|
||
return ( | ||
$self->SUPER::arg_opts(), | ||
'source=s' => \$self->{OPTS}->{source}, | ||
'strict!' => \$self->{OPTS}->{strict}, | ||
) | ||
} | ||
|
||
sub check_rule { | ||
my ($self, $rule) = @_; | ||
|
||
die_fatal "Source file should be specified", 1 | ||
unless ($rule->{source}); | ||
|
||
push @{$rule->{path}}, '' unless (@{$rule->{path}}); | ||
|
||
return $self; | ||
} | ||
|
||
sub configure { | ||
my $self = shift; | ||
|
||
# to prevent source resolve to target (ndproc defaults) | ||
delete $self->{OPTS}->{source} | ||
unless (defined $self->{OPTS}->{source}); | ||
} | ||
|
||
sub process_path { | ||
my ($self, $data, $path, $spath, $opts, $source) = @_; | ||
|
||
my @refs = eval { path(${$data}, $spath, strict => $opts->{strict}) }; | ||
die_fatal "Failed to lookup path '$path'", 4 if ($@); | ||
|
||
map { patch(${$_}, $source) } @refs; | ||
} | ||
|
||
1; # End of App::NDTools::NDProc::Module::Patch | ||
|
||
__END__ | ||
=head1 NAME | ||
Patch - Apply nested diff to the structure | ||
=head1 OPTIONS | ||
=over 4 | ||
=item B<--[no]blame> | ||
Blame calculation toggle. Enabled by default. | ||
=item B<--path> E<lt>pathE<gt> | ||
Path in the structure to patch. May be used several times. Whole structure | ||
will be patched if omitted or empty. | ||
=item B<--preserve> E<lt>pathE<gt> | ||
Preserve specified substructure. May be used several times. | ||
=item B<--source> E<lt>uriE<gt> | ||
Source containing patch. | ||
=item B<--strict> | ||
Fail if path specified for patch doesn't exist. | ||
=back | ||
=head1 SEE ALSO | ||
L<ndproc>, L<ndproc-modules> | ||
L<nddiff>, L<ndquery>, L<Struct::Path::PerlStyle> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"false" : false, | ||
"list" : [ | ||
true, | ||
false, | ||
true, | ||
false | ||
], | ||
"same" : { | ||
"false" : false, | ||
"true" : true | ||
}, | ||
"true" : true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"false" : true, | ||
"list" : [ | ||
true, | ||
true, | ||
false, | ||
false | ||
], | ||
"same" : { | ||
"false" : false, | ||
"true" : true | ||
}, | ||
"true" : false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"false" : false, | ||
"list" : [ | ||
true, | ||
false, | ||
true, | ||
false | ||
], | ||
"same" : { | ||
"false" : false, | ||
"true" : true | ||
}, | ||
"true" : false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"N" : false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"D" : { | ||
"false" : { | ||
"N" : true | ||
}, | ||
"list" : { | ||
"D" : [ | ||
{ | ||
"I" : 1, | ||
"R" : false | ||
}, | ||
{ | ||
"A" : false, | ||
"I" : 3 | ||
} | ||
] | ||
}, | ||
"true" : { | ||
"N" : false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"D" : { | ||
"false" : { | ||
"N" : true | ||
}, | ||
"list" : { | ||
"D" : [ | ||
{ | ||
"I" : 1, | ||
"R" : false | ||
}, | ||
{ | ||
"A" : false, | ||
"I" : 3 | ||
} | ||
] | ||
}, | ||
"true" : { | ||
"N" : false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"false" : true, | ||
"list" : [ | ||
true, | ||
true, | ||
false, | ||
false | ||
], | ||
"same" : { | ||
"false" : false, | ||
"true" : true | ||
}, | ||
"true" : true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"D" : { | ||
"false" : { | ||
"N" : true | ||
}, | ||
"list" : { | ||
"D" : [ | ||
{ | ||
"I" : 1, | ||
"R" : false | ||
}, | ||
{ | ||
"A" : false, | ||
"I" : 3 | ||
} | ||
] | ||
}, | ||
"true" : { | ||
"N" : false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
garbage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use strict; | ||
use warnings FATAL => 'all'; | ||
|
||
use File::Copy qw(copy); | ||
use Test::File::Contents; | ||
use Test::More tests => 8; | ||
|
||
use App::NDTools::Test; | ||
|
||
chdir t_dir or die "Failed to change test dir"; | ||
|
||
my $test; | ||
my $mod = 'App::NDTools::NDProc'; | ||
my @cmd = ($mod, '--module', 'Patch'); | ||
|
||
require_ok($mod) || BAIL_OUT("Failed to load $mod"); | ||
|
||
$test = "path"; | ||
run_ok( | ||
name => $test, | ||
pre => sub { copy("_bool.a.json", "$test.got") }, | ||
cmd => [ @cmd, '--source', "$test.patch", '--path', '{true}', "$test.got" ], | ||
test => sub { files_eq_or_diff("$test.exp", "$test.got", $test) }, | ||
); | ||
|
||
$test = "path_absent"; | ||
run_ok( | ||
name => $test, | ||
pre => sub { copy("_bool.a.json", "$test.got") }, | ||
cmd => [ @cmd, '--source', "$test.patch", "$test.got" ], | ||
test => sub { files_eq_or_diff("_bool.b.json", "$test.got", $test) }, | ||
); | ||
|
||
$test = "path_empty"; # full doc patched | ||
run_ok( | ||
name => $test, | ||
pre => sub { copy("_bool.a.json", "$test.got") }, | ||
cmd => [ @cmd, '--source', "$test.patch", '--path', '', "$test.got" ], | ||
test => sub { files_eq_or_diff("_bool.b.json", "$test.got", $test) }, | ||
); | ||
|
||
$test = "path_strict"; | ||
run_ok( | ||
name => $test, | ||
pre => sub { copy("_bool.a.json", "$test.got") }, | ||
cmd => [ @cmd, '--source', '_bool.b.json', '--path', '{some}{absent}{path}', '--strict', "$test.got" ], | ||
stderr => qr/ FATAL] Failed to lookup path /, | ||
exit => 4, | ||
); | ||
|
||
$test = "preserve"; | ||
run_ok( | ||
name => $test, | ||
pre => sub { copy("_bool.a.json", "$test.got") }, | ||
cmd => [ @cmd, '--source', "$test.patch", '--preserve', '{true}', "$test.got" ], | ||
test => sub { files_eq_or_diff("$test.exp", "$test.got", $test) }, | ||
); | ||
|
||
$test = "source_absent"; | ||
run_ok( | ||
name => $test, | ||
pre => sub { copy("_bool.a.json", "$test.got") }, | ||
cmd => [ @cmd, '--path', '{true}', "$test.got" ], | ||
stderr => qr/ FATAL] Source file should be specified/, | ||
exit => 1, | ||
); | ||
|
||
$test = "source_broken"; | ||
run_ok( | ||
name => $test, | ||
pre => sub { copy("_bool.a.json", "$test.got") }, | ||
cmd => [ @cmd, '--source', "$test.patch", "$test.got" ], | ||
stderr => qr/ FATAL] Failed to decode 'JSON'/, | ||
exit => 4, | ||
); | ||
|