From 0929406862437bd518db874e311845dcf14cdc15 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Wed, 2 Mar 2011 14:19:07 +0100 Subject: [PATCH] SL::Helper::Csv: neues flag "strict_profile". Wenn gesetzt werden nur Daten aus dem Profil benutzt, keine DWIM can checks. --- SL/Helper/Csv.pm | 10 +++++++-- SL/Helper/Csv/Dispatcher.pm | 10 ++++++++- t/helper/csv.t | 42 ++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index 945758f5d..1b8a4b77f 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -9,8 +9,8 @@ use Params::Validate qw(:all); use Text::CSV; use Rose::Object::MakeMethods::Generic scalar => [ qw( file encoding sep_char quote_char escape_char header profile class - numberformat dateformat ignore_unknown_columns _io _csv _objects _parsed - _data _errors + numberformat dateformat ignore_unknown_columns strict_profile _io _csv + _objects _parsed _data _errors ) ]; use SL::Helper::Csv::Dispatcher; @@ -32,6 +32,7 @@ sub new { numberformat => 0, dateformat => 0, ignore_unknown_columns => 0, + strict_profile => 0, }); my $self = bless {}, $class; @@ -342,6 +343,11 @@ and the return value used instead of the line itself. If set, the import will ignore unkown header columns. Useful for lazy imports, but deactivated by default. +=item C + +If set, all columns to be parsed must be specified in C. Every header +field not listed there will be treated like an unknown column. + =back =head1 ERROR HANDLING diff --git a/SL/Helper/Csv/Dispatcher.pm b/SL/Helper/Csv/Dispatcher.pm index cbcae0d82..6375daf03 100644 --- a/SL/Helper/Csv/Dispatcher.pm +++ b/SL/Helper/Csv/Dispatcher.pm @@ -71,7 +71,15 @@ sub parse_profile { for my $col (@$header) { next unless $col; - push @specs, $self->make_spec($col, $profile->{$col} || $col); + if ($self->_csv->strict_profile) { + if (exists $profile->{$col}) { + push @specs, $self->make_spec($col, $profile->{$col}); + } else { + $self->unknown_column($col, undef); + } + } else { + push @specs, $self->make_spec($col, $profile->{$col} || $col); + } } $self->_specs(\@specs); diff --git a/t/helper/csv.t b/t/helper/csv.t index cc009940d..e54dcb6fc 100644 --- a/t/helper/csv.t +++ b/t/helper/csv.t @@ -1,4 +1,4 @@ -use Test::More tests => 31; +use Test::More tests => 36; use SL::Dispatcher; use Data::Dumper; use utf8; @@ -225,4 +225,44 @@ is $csv->parse, undef, 'wrong profile gets rejected'; is_deeply $csv->errors, [ 'buchungsgruppen.1.description', undef, "Profile path error. Indexed relationship is not OneToMany around here: 'buchungsgruppen.1'", undef ,0 ], 'error indicates wrong header'; isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified'); +#### + +$csv = SL::Helper::Csv->new( + file => \< '1,000.00', + ignore_unknown_columns => 1, + strict_profile => 1, + class => 'SL::DB::Part', + profile => { + lastcost => 'lastcost_as_number', + } +); +$csv->parse; +is $csv->get_objects->[0]->lastcost, '1221.52', 'strict_profile with ignore'; +is $csv->get_objects->[0]->sellprice, undef, 'strict profile with ignore 2'; + +#### + +$csv = SL::Helper::Csv->new( + file => \< '1,000.00', + strict_profile => 1, + class => 'SL::DB::Part', + profile => { + lastcost => 'lastcost_as_number', + } +); +$csv->parse; + +is_deeply( ($csv->errors)[0], [ 'description', undef, 'header field \'description\' is not recognized', undef, 0 ], 'strict_profile without ignore_columns throws error'); + + # vim: ft=perl -- 2.20.1