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;
numberformat => 0,
dateformat => 0,
ignore_unknown_columns => 0,
+ strict_profile => 0,
});
my $self = bless {}, $class;
If set, the import will ignore unkown header columns. Useful for lazy imports,
but deactivated by default.
+=item C<strict_profile>
+
+If set, all columns to be parsed must be specified in C<profile>. Every header
+field not listed there will be treated like an unknown column.
+
=back
=head1 ERROR HANDLING
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);
-use Test::More tests => 31;
+use Test::More tests => 36;
use SL::Dispatcher;
use Data::Dumper;
use utf8;
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 => \<<EOL,
+description;partnumber;sellprice;lastcost;wiener;
+Kaffee;;0.12;1,221.52;ja wiener
+Beer;1123245;0.12;1.5234;nein kein wieder
+EOL
+ numberformat => '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 => \<<EOL,
+description;partnumber;sellprice;lastcost;wiener;
+Kaffee;;0.12;1,221.52;ja wiener
+Beer;1123245;0.12;1.5234;nein kein wieder
+EOL
+ numberformat => '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