added header_first to WebPAC::Input::CSV master
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 25 Jul 2019 10:05:07 +0000 (12:05 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 25 Jul 2019 10:05:23 +0000 (12:05 +0200)
lib/WebPAC/Input/CSV.pm
t/2-input-csv.t
t/data/header_first.csv [new file with mode: 0644]

index 26dab49..9f10579 100644 (file)
@@ -26,6 +26,7 @@ Returns new low-level input API object
 
   my $input = new WebPAC::Input::CSV(
        path => '/path/to/records.csv',
 
   my $input = new WebPAC::Input::CSV(
        path => '/path/to/records.csv',
+       header_first => 1,
   );
 
 Options:
   );
 
 Options:
@@ -40,6 +41,8 @@ path to CSV file
 
 Default encoding of input file is C<utf-8>
 
 
 Default encoding of input file is C<utf-8>
 
+C<header_first> will use first line as header names.
+
 =cut
 
 sub new {
 =cut
 
 sub new {
@@ -57,6 +60,12 @@ sub new {
 
        $self->{size} = 0;
 
 
        $self->{size} = 0;
 
+       if ( $self->{header_first} ) {
+               my $line = $csv->getline( $fh );
+               $self->{header_names} = $line;
+               $self->debug( "header_names = ",dump( $self->{header_names} ) );
+       }
+
        while ( 1 ) {
                my $line = $csv->getline( $fh );
                last if $csv->eof;
        while ( 1 ) {
                my $line = $csv->getline( $fh );
                last if $csv->eof;
@@ -67,6 +76,8 @@ sub new {
                $rec->{'000'} = [ ++$self->{size} ];
 
                my $col = 'A';
                $rec->{'000'} = [ ++$self->{size} ];
 
                my $col = 'A';
+               my $header_pos = 0;
+
                foreach my $cell ( @$line ) {
                        my $str = eval { Encode::decode_utf8( $cell ) };
                        if ( $@ ) {
                foreach my $cell ( @$line ) {
                        my $str = eval { Encode::decode_utf8( $cell ) };
                        if ( $@ ) {
@@ -81,6 +92,11 @@ sub new {
                        }
                                
                        $rec->{ $col++ } = $str;
                        }
                                
                        $rec->{ $col++ } = $str;
+
+                       if ( $self->{header_names} ) {
+                               $rec->{ $self->{header_names}->[$header_pos] } = $str;
+                               $header_pos++;
+                       }
                }
 
                push @{ $self->{_rec} }, $rec;
                }
 
                push @{ $self->{_rec} }, $rec;
index 2ed084e..845d1ee 100755 (executable)
@@ -3,7 +3,7 @@
 use strict;
 use lib 'lib';
 
 use strict;
 use lib 'lib';
 
-use Test::More tests => 63;
+use Test::More tests => 102;
 
 BEGIN {
 use_ok( 'WebPAC::Test' );
 
 BEGIN {
 use_ok( 'WebPAC::Test' );
@@ -39,3 +39,20 @@ foreach my $mfn ( 1 ... $size ) {
        diag "rec: ", dump($rec), "\n" if $debug;
 }
 
        diag "rec: ", dump($rec), "\n" if $debug;
 }
 
+ok(my $db = $input->open(
+       path => "$abs_path/data/header_first.csv",
+       header_first => 1,
+), "open");
+ok(my $size = $input->size, "size");
+cmp_ok( $size, '==', 10 - 1, 'size header_first' );
+
+foreach my $mfn ( 1 ... $size ) {
+       my $rec = $input->fetch;
+       ok($rec, "fetch $mfn");
+       cmp_ok($rec->{'000'}->[0], '==', $mfn, 'has mfn');
+       cmp_ok($input->pos, '==', $mfn, "pos $mfn");
+
+       cmp_ok( $rec->{'A'}, 'eq', $rec->{'publication_title'}, 'A eq publication_title' );
+       diag "rec: ", dump($rec), "\n" if $debug;
+}
+
diff --git a/t/data/header_first.csv b/t/data/header_first.csv
new file mode 100644 (file)
index 0000000..ca3f3f6
--- /dev/null
@@ -0,0 +1,10 @@
+"publication_title","print_identifier","online_identifier","date_first_issue_online","num_first_vol_online","num_first_issue_online","date_last_issue_online","num_last_vol_online","num_last_issue_online","title_url","first_author","title_id","embargo_info","coverage_depth","notes","publisher_name","publication_type","date_monograph_published_print","date_monograph_published_online","monograph_volume","monograph_edition","first_editor","parent_publication_title_id","preceding_publication_title_id","access_type"
+"1895. Mille huit cent quatre-vingt-quinze","0769-0959","1960-6176","2000","31","","2015","76","","http://journals.openedition.org/1895","","1895","","fulltext","","Association française de recherche sur l’histoire du cinéma (AFRHC)","serial","","","","","","","","F"
+"ABE Journal","","2275-6639","2012","1","","","","","http://journals.openedition.org/abe","","abe","","fulltext","Full access to the HTML version of the content. Access to PDF and Epub reserved to subscribing institutions.","InVisu","serial","","","","","","","","F"
+"ADLFI. Archéologie de la France - Informations","","2114-0502","2001","2001","","","","","http://journals.openedition.org/adlfi","","adlfi","","fulltext","Autres n° : 1997. Manquent : 2003, 2011, 2012, 2013","Ministère de la Culture","serial","","","","","","","","F"
+"ASp","1246-8185","2108-6354","1993","1","","2017","72","","http://journals.openedition.org/asp","","asp","","fulltext","Comptes rendus : 1994-...","Groupe d'étude et de recherche en anglais de spécialité","serial","","","","","","","","F"
+"Abstracta Iranica","0240-8910","1961-960X","2001","22","","","","","http://journals.openedition.org/abstractairanica","","abstractairanica","","fulltext","Nota bene : Numéros ouverts à partir de 2016","CNRS (UMR 7528 Mondes iraniens et indiens), Éditions de l’IFRI","serial","","","","","","","","F"
+"Acquisition et interaction en langue étrangère","1243-969X","1778-7432","1992","1","","2009","2","","http://journals.openedition.org/aile","","aile","","fulltext","vol.1, 1992 - vol.28, 2008; nouvelle numérotation vol.1, 2009 - vol.2, 2009","Association Encrages","serial","","","","","","","","F"
+"Actes des congrès de la Société française Shakespeare","","2271-6424","1980","1","","","","","http://journals.openedition.org/shakespeare","","shakespeare","","fulltext","","Société Française Shakespeare","serial","","","","","","","","F"
+"Activités","","1765-2723","2004","1","1","","","","http://journals.openedition.org/activites","","activites","","fulltext","","ARPACT - Association Recherches et Pratiques sur les ACTivités","serial","","","","","","","","F"
+"Afrique : Archéologie et Arts","1634-3123","2431-2045","2006","4","","","","","http://journals.openedition.org/aaa","","aaa","","fulltext","Full access to the HTML version of the content. Access to PDF and Epub reserved to subscribing institutions.","CNRS - UMR 7041 (Archéologie et Sciences de l'Antiquité - ArScAn)","serial","","","","","","","","F"