Revision 09294068
Von Sven Schöling vor mehr als 13 Jahren hinzugefügt
SL/Helper/Csv.pm | ||
---|---|---|
9 | 9 |
use Text::CSV; |
10 | 10 |
use Rose::Object::MakeMethods::Generic scalar => [ qw( |
11 | 11 |
file encoding sep_char quote_char escape_char header profile class |
12 |
numberformat dateformat ignore_unknown_columns _io _csv _objects _parsed
|
|
13 |
_data _errors |
|
12 |
numberformat dateformat ignore_unknown_columns strict_profile _io _csv
|
|
13 |
_objects _parsed _data _errors
|
|
14 | 14 |
) ]; |
15 | 15 |
|
16 | 16 |
use SL::Helper::Csv::Dispatcher; |
... | ... | |
32 | 32 |
numberformat => 0, |
33 | 33 |
dateformat => 0, |
34 | 34 |
ignore_unknown_columns => 0, |
35 |
strict_profile => 0, |
|
35 | 36 |
}); |
36 | 37 |
my $self = bless {}, $class; |
37 | 38 |
|
... | ... | |
342 | 343 |
If set, the import will ignore unkown header columns. Useful for lazy imports, |
343 | 344 |
but deactivated by default. |
344 | 345 |
|
346 |
=item C<strict_profile> |
|
347 |
|
|
348 |
If set, all columns to be parsed must be specified in C<profile>. Every header |
|
349 |
field not listed there will be treated like an unknown column. |
|
350 |
|
|
345 | 351 |
=back |
346 | 352 |
|
347 | 353 |
=head1 ERROR HANDLING |
SL/Helper/Csv/Dispatcher.pm | ||
---|---|---|
71 | 71 |
|
72 | 72 |
for my $col (@$header) { |
73 | 73 |
next unless $col; |
74 |
push @specs, $self->make_spec($col, $profile->{$col} || $col); |
|
74 |
if ($self->_csv->strict_profile) { |
|
75 |
if (exists $profile->{$col}) { |
|
76 |
push @specs, $self->make_spec($col, $profile->{$col}); |
|
77 |
} else { |
|
78 |
$self->unknown_column($col, undef); |
|
79 |
} |
|
80 |
} else { |
|
81 |
push @specs, $self->make_spec($col, $profile->{$col} || $col); |
|
82 |
} |
|
75 | 83 |
} |
76 | 84 |
|
77 | 85 |
$self->_specs(\@specs); |
t/helper/csv.t | ||
---|---|---|
1 |
use Test::More tests => 31;
|
|
1 |
use Test::More tests => 36;
|
|
2 | 2 |
use SL::Dispatcher; |
3 | 3 |
use Data::Dumper; |
4 | 4 |
use utf8; |
... | ... | |
225 | 225 |
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'; |
226 | 226 |
isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified'); |
227 | 227 |
|
228 |
#### |
|
229 |
|
|
230 |
$csv = SL::Helper::Csv->new( |
|
231 |
file => \<<EOL, |
|
232 |
description;partnumber;sellprice;lastcost;wiener; |
|
233 |
Kaffee;;0.12;1,221.52;ja wiener |
|
234 |
Beer;1123245;0.12;1.5234;nein kein wieder |
|
235 |
EOL |
|
236 |
numberformat => '1,000.00', |
|
237 |
ignore_unknown_columns => 1, |
|
238 |
strict_profile => 1, |
|
239 |
class => 'SL::DB::Part', |
|
240 |
profile => { |
|
241 |
lastcost => 'lastcost_as_number', |
|
242 |
} |
|
243 |
); |
|
244 |
$csv->parse; |
|
245 |
is $csv->get_objects->[0]->lastcost, '1221.52', 'strict_profile with ignore'; |
|
246 |
is $csv->get_objects->[0]->sellprice, undef, 'strict profile with ignore 2'; |
|
247 |
|
|
248 |
#### |
|
249 |
|
|
250 |
$csv = SL::Helper::Csv->new( |
|
251 |
file => \<<EOL, |
|
252 |
description;partnumber;sellprice;lastcost;wiener; |
|
253 |
Kaffee;;0.12;1,221.52;ja wiener |
|
254 |
Beer;1123245;0.12;1.5234;nein kein wieder |
|
255 |
EOL |
|
256 |
numberformat => '1,000.00', |
|
257 |
strict_profile => 1, |
|
258 |
class => 'SL::DB::Part', |
|
259 |
profile => { |
|
260 |
lastcost => 'lastcost_as_number', |
|
261 |
} |
|
262 |
); |
|
263 |
$csv->parse; |
|
264 |
|
|
265 |
is_deeply( ($csv->errors)[0], [ 'description', undef, 'header field \'description\' is not recognized', undef, 0 ], 'strict_profile without ignore_columns throws error'); |
|
266 |
|
|
267 |
|
|
228 | 268 |
# vim: ft=perl |
Auch abrufbar als: Unified diff
SL::Helper::Csv: neues flag "strict_profile". Wenn gesetzt werden nur Daten aus dem Profil benutzt, keine DWIM can checks.