1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
package Catmandu::Fix::lookup;
use Catmandu::Sane;
our $VERSION = '1.2024';
use Catmandu::Importer::CSV;
use Catmandu::Util::Path qw(as_path);
use Catmandu::Util qw(is_value);
use Moo;
use namespace::clean;
use Catmandu::Fix::Has;
with 'Catmandu::Fix::Builder';
has path => (fix_arg => 1);
has file => (fix_arg => 1);
has default => (fix_opt => 1, predicate => 1);
has delete => (fix_opt => 1);
has csv_args => (fix_opt => 'collect');
has dictionary => (is => 'lazy', init_arg => undef);
sub _build_dictionary {
my ($self) = @_;
Catmandu::Importer::CSV->new(
%{$self->csv_args},
file => $self->file,
header => 0,
fields => ['key', 'val'],
)->reduce(
{},
sub {
my ($dict, $pair) = @_;
$dict->{$pair->{key}} = $pair->{val};
$dict;
}
);
}
sub _build_fixer {
my ($self) = @_;
my $path = as_path($self->path);
my $dict = $self->dictionary;
my $has_default = $self->has_default;
my $default = $self->default;
my $delete = $self->delete;
$path->updater(
sub {
my $val = $_[0];
if (is_value($val) && defined(my $new_val = $dict->{$val})) {
return $new_val;
}
elsif ($delete) {
return undef, 1, 1;
}
elsif ($has_default) {
return $default;
}
return undef, 1, 0;
}
);
}
1;
__END__
=pod
=head1 NAME
Catmandu::Fix::lookup - change the value of a HASH key or ARRAY index by
looking up its value in a dictionary
=head1 SYNOPSIS
# dictionary.csv
# id,planet
# 1,sun
# 2,earth
# 3,moon
# values found in the dictionary.csv will be replaced
# {foo => {bar => 2}}
lookup(foo.bar, dictionary.csv)
# {foo => {bar => 'earth'}}
# values not found will be kept
# {foo => {bar => 232}}
lookup(foo.bar, dictionary.csv)
# {foo => {bar => 232}}
# in case you have a different seperator
lookup(foo.bar, dictionary.csv, sep_char: |)
# delete value if the lookup fails:
lookup(foo.bar, dictionary.csv, delete: 1)
# use a default value if the lookup fails:
lookup(foo.bar, dictionary.csv, default: 'default value')
=head1 SEE ALSO
L<Catmandu::Fix>, L<Catmandu::Fix::mapping>
=cut
|