[Bps-public-commit] rt-extension-assets-import-csv branch, allow-header-parsing-options, created. 1.4-8-g125eb24
Craig Kaiser
craig at bestpractical.com
Wed Feb 10 16:48:57 EST 2021
The branch, allow-header-parsing-options has been created
at 125eb2411bf8c847dd200813dca9350d7d20e167 (commit)
- Log -----------------------------------------------------------------
commit 125eb2411bf8c847dd200813dca9350d7d20e167
Author: craig kaiser <craig at bestpractical.com>
Date: Wed Feb 10 13:25:31 2021 -0500
Allow header parsing options
If we do not add the 'detect_bom' to the Text::CSV_XS::header method
then CSV files with BOM in them will break the CSV importer.
diff --git a/README b/README
index deb2410..5e2a35d 100644
--- a/README
+++ b/README
@@ -89,6 +89,17 @@ CONFIGURATION
This requires that, after the import, RT becomes the generator of all
asset ids. Otherwise, asset id conflicts may occur.
+ $CSVHeaderOptions
+ Set CSV parsing header options
+ <https://metacpan.org/pod/Text::CSV_XS#header> by setting the
+ $CSVHeaderOptions config value. The default value is shown below:
+
+ Set( $CSVOptions, {
+ detect_bom => 1,
+ munge_column_names => "none",
+ ...
+ });
+
AUTHOR
Best Practical Solutions, LLC <modules at bestpractical.com>
diff --git a/lib/RT/Extension/Assets/Import/CSV.pm b/lib/RT/Extension/Assets/Import/CSV.pm
index d23cea1..692086b 100644
--- a/lib/RT/Extension/Assets/Import/CSV.pm
+++ b/lib/RT/Extension/Assets/Import/CSV.pm
@@ -291,14 +291,18 @@ sub parse_csv {
my $csv = Text::CSV_XS->new( { binary => 1 } );
open my $fh, '<', $file or die "failed to read $file: $!";
- my $header = $csv->getline($fh);
+ my @header = $csv->header($fh, {
+ munge_column_names => 'none',
+ detect_bom => 1,
+ %{ RT->Config->Get('CSVHeaderOptions') || {} },
+ });
my @items;
while ( my $row = $csv->getline($fh) ) {
my $item;
- for ( my $i = 0 ; $i < @$header ; $i++ ) {
- if ( $header->[$i] ) {
- $item->{ $header->[$i] } = $row->[$i];
+ for ( my $i = 0 ; $i < @header ; $i++ ) {
+ if ( $header[$i] ) {
+ $item->{ $header[$i] } = $row->[$i];
}
}
@@ -418,6 +422,18 @@ the C<%AssetsImportFieldMapping>:
This requires that, after the import, RT becomes the generator of all
asset ids. Otherwise, asset id conflicts may occur.
+=head2 C<$CSVHeaderOptions>
+
+Set CSV parsing L<header options|https://metacpan.org/pod/Text::CSV_XS#header> by setting the
+C<$CSVHeaderOptions> config value. The default value is shown below:
+
+ Set( $CSVOptions, {
+ detect_bom => 1,
+ munge_column_names => "none",
+ ...
+ });
+
+
=head1 AUTHOR
Best Practical Solutions, LLC E<lt>modules at bestpractical.comE<gt>
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list