From 552ed9ba654d034d3846934efe97470e16b4cabc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Wed, 30 May 2018 15:26:18 +0200 Subject: [PATCH] CSV-Helfer: Leere Zeilen ignorieren. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Als leere Zeilen hier gelten auch Zeilen, die nur das Trennzeichen enthalten. Mit leeren Zeilen gab es immer wieder Probleme, teils mit schwer zu interpretierenden Fehlermeldungen, teils mit nicht gewünschtem Verhalten, z.B. beim Warenimport das Anlegen neuer Artikel für jede leere Zeile. --- SL/Helper/Csv.pm | 5 ++++ t/helper/csv.t | 65 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index 2444225ca..cea43ae69 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -239,6 +239,10 @@ sub _check_multiplex_datatype_position { } } +sub _is_empty_row { + return !!all { !$_ } @{$_[0]}; +} + sub _parse_data { my ($self, %params) = @_; my (@data, @errors); @@ -246,6 +250,7 @@ sub _parse_data { while (1) { my $row = $self->_csv->getline($self->_io); if ($row) { + next if _is_empty_row($row); my $header = $self->_header_by_row($row); if (!$header) { push @errors, [ diff --git a/t/helper/csv.t b/t/helper/csv.t index e3247263d..7997fe77f 100644 --- a/t/helper/csv.t +++ b/t/helper/csv.t @@ -1,4 +1,4 @@ -use Test::More tests => 86; +use Test::More tests => 91; use lib 't'; use utf8; @@ -382,6 +382,39 @@ $csv->parse; is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'without profile and class works'; ##### + +$csv = SL::Helper::Csv->new( + file => \<parse; +is_deeply $csv->get_data, [ {partnumber => 1, description => 'Kaffee'}, {partnumber => '', description => ' '}, {partnumber => 3, description => 'Tee'} ], 'ignoring empty lines works (header in csv file)'; + +##### + +$csv = SL::Helper::Csv->new( + file => \< ['description', 'partnumber'], +); +ok $csv->parse; +is_deeply $csv->get_data, [ {partnumber => 1, description => 'Kaffee'}, {partnumber => '', description => ' '}, {partnumber => 3, description => 'Tee'} ], 'ignoring empty lines works'; + +##### + $csv = SL::Helper::Csv->new( file => \"Kaffee;1,50\nSchoke;0,89\n", header => [ @@ -726,6 +759,36 @@ ok $csv->get_objects->[0], 'multiplex: empty path gets ignored in object creatio ##### +$csv = SL::Helper::Csv->new( + file => \< 1, + profile => [ { class => 'SL::DB::Customer', row_ident => 'C' }, + { class => 'SL::DB::Part', row_ident => 'P' }, + ], +); +$csv->parse; +is_deeply $csv->get_data, [ + {datatype => 'C', customernumber => 1000, name => 'Meier'}, + {datatype => 'P', partnumber => 1, description => 'Kaffee'}, + {datatype => 'C', customernumber => undef, name => undef}, + {datatype => 'P', partnumber => '', description => ' '}, + {datatype => 'C', customernumber => 2000, name => 'Meister'}, + {datatype => 'P', partnumber => '3', description => 'Tee'}, +], 'ignoring empty lines works (multiplex data)'; + +##### + # Mappings # simple case $csv = SL::Helper::Csv->new( -- 2.20.1