App-NDTools

 view release on metacpan or  search on metacpan

lib/App/NDTools/NDProc/Module/JsonPatch.pm  view on Meta::CPAN

package App::NDTools::NDProc::Module::JsonPatch;

use strict;
use warnings FATAL => 'all';
use parent 'App::NDTools::NDProc::Module';

use App::NDTools::Util qw(chomp_evaled_error);
use JSON::Patch 0.04 qw(patch);
use Log::Log4Cli;
use Struct::Path 0.80 qw(path);

our $VERSION = '0.02';

sub MODINFO { "Apply JSON Patch (rfc6902) patches" }

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 ($@);

    for (@refs) {
        eval { patch(${$_}, $source) };
        die_fatal chomp_evaled_error($@), 8 if ($@);
    }
}

1; # End of App::NDTools::NDProc::Module::JsonPatch

__END__

=head1 NAME

Patch - Apply JSON Patch (rfc6902) patches

=head1 OPTIONS

=over 4

=item B<--[no]blame>

Blame calculation toggle. Enabled by default.

=item B<--cond> E<lt>pathE<gt>

Apply rule when condition met only. Condition is met when path leads to at
least one item in the structure. May be used several times (in this case
conditions are AND'ed).

=item B<--path> E<lt>pathE<gt>



( run in 0.797 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )