From: Sven Schöling Date: Wed, 2 Mar 2011 11:02:42 +0000 (+0100) Subject: Csv Errors sind nun Objekte mit entsprechendem Zugriff. X-Git-Tag: release-2.7.0beta1~396^2~68 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=c46898c7a2328d211465818380178e25331d8f3c;p=kivitendo-erp.git Csv Errors sind nun Objekte mit entsprechendem Zugriff. --- diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index 5aac00b48..945758f5d 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -14,6 +14,7 @@ use Rose::Object::MakeMethods::Generic scalar => [ qw( ) ]; use SL::Helper::Csv::Dispatcher; +use SL::Helper::Csv::Error; # public interface @@ -180,7 +181,7 @@ sub _guess_encoding { sub _push_error { my ($self, @errors) = @_; - my @new_errors = ($self->errors, @errors); + my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors); $self->_errors(\@new_errors); } @@ -346,16 +347,13 @@ but deactivated by default. =head1 ERROR HANDLING After parsing a file all errors will be accumulated into C. +Each entry is an object with the following attributes: -Each entry is an arrayref with the following structure: - - [ - 0 offending raw input, - 1 Text::CSV error code if Text:CSV signalled an error, 0 else, - 2 error diagnostics, - 3 position in line, - 4 estimated line in file, - ] + raw_input: offending raw input, + code: Text::CSV error code if Text:CSV signalled an error, 0 else, + diag: error diagnostics, + line: position in line, + col: estimated line in file, Note that the last entry can be off, but will give an estimate. diff --git a/SL/Helper/Csv/Dispatcher.pm b/SL/Helper/Csv/Dispatcher.pm index a8d29fe45..cbcae0d82 100644 --- a/SL/Helper/Csv/Dispatcher.pm +++ b/SL/Helper/Csv/Dispatcher.pm @@ -9,6 +9,8 @@ use Rose::Object::MakeMethods::Generic scalar => [ qw( _specs _errors ) ]; +use SL::Helper::Csv::Error; + sub new { my ($class, $parent) = @_; my $self = bless { }, $class; @@ -137,7 +139,7 @@ sub errors { sub _push_error { my ($self, @errors) = @_; - my @new_errors = ($self->errors, @errors); + my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors); $self->_errors(\@new_errors); } diff --git a/SL/Helper/Csv/Error.pm b/SL/Helper/Csv/Error.pm new file mode 100644 index 000000000..d3c9144ae --- /dev/null +++ b/SL/Helper/Csv/Error.pm @@ -0,0 +1,16 @@ +package SL::Helper::Csv::Error; + +use strict; + +sub new { + my $class = shift; + bless [ @_ ], $class; +} + +sub raw_input { $_->[0] } +sub code { $_->[1] } +sub diag { $_->[2] } +sub col { $_->[3] } +sub line { $_->[4] } + +1; diff --git a/t/helper/csv.t b/t/helper/csv.t index 072143480..cc009940d 100644 --- a/t/helper/csv.t +++ b/t/helper/csv.t @@ -125,6 +125,7 @@ EOL ); is $csv->parse, undef, 'broken csv content won\'t get parsed'; is_deeply $csv->errors, [ '"Kaf"fee";;0.12;1,221.52'."\n", 2023, 'EIQ - QUO character not allowed', 5, 2 ], 'error'; +isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified'); #### @@ -222,5 +223,6 @@ EOL ); 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'); # vim: ft=perl