Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision de4b1e97

Von Bernd Bleßmann vor fast 11 Jahren hinzugefügt

  • ID de4b1e970660f9ab8ef22e357cc87639cb9be85e
  • Vorgänger 43010559
  • Nachfolger ffb54c7e

case_insensitive-Flag für Multiplex-Daten anpassen.

Unterschiede anzeigen:

SL/Helper/Csv.pm
169 169
  # mopst likely meant that field, so rewrite the header
170 170
  if ($self->case_insensitive_header) {
171 171
    die 'case_insensitive_header is only possible with profile' unless $self->profile;
172
    my @names = (
173
      keys %{ $self->profile || {} },
174
    );
175
    for my $name (@names) {
176
      for my $i (0..$#$header) {
177
        $header->[$i] = $name if lc $header->[$i] eq lc $name;
172
    if ($header) {
173
      my $p_num = 0;
174
      foreach my $h (@{ $header }) {
175
        my @names = (
176
          keys %{ $self->profile->[$p_num]->{profile} || {} },
177
        );
178
        for my $name (@names) {
179
          for my $i (0..$#$h) {
180
            $h->[$i] = $name if lc $h->[$i] eq lc $name;
181
          }
182
        }
183
        $p_num++;
178 184
      }
179 185
    }
180 186
  }
......
448 454
will have to do that for yourself. Since you provided the profile, it is
449 455
assumed you know what to do in this case.
450 456

  
457
If no profile is given, any header field found will be taken as is.
458

  
459
If the path in a profile entry is empty, the field will be subjected to
460
C<strict_profile> and C<case_insensitive_header> checking, will be parsed into
461
C<get_data>, but will not be attempted to be dispatched into objects.
462

  
451 463
If C<class> is present, the line will be handed to the new sub of this class,
452 464
and the return value used instead of the line itself.
453 465

  
t/helper/csv.t
1
use Test::More tests => 64;
1
use Test::More tests => 71;
2 2

  
3 3
use lib 't';
4 4
use utf8;
......
549 549
);
550 550
ok !$csv->_check_multiplexed, 'multiplex check detects empty header';
551 551

  
552
#####
553

  
554
$csv = SL::Helper::Csv->new(
555
  file   => \"Datatype;Description\nDatatype;Name\nP;Kaffee\nC;Meier",        # " # make emacs happy
556
  case_insensitive_header => 1,
557
  ignore_unknown_columns => 1,
558
  profile => [
559
    {
560
      profile   => { datatype => 'datatype', description => 'description' },
561
      class     => 'SL::DB::Part',
562
      row_ident => 'P'
563
    },
564
    {
565
      profile   => { datatype => 'datatype', name => 'name' },
566
      class     => 'SL::DB::Customer',
567
      row_ident => 'C'
568
    }
569
  ],
570
);
571
$csv->parse;
572
is_deeply $csv->get_data, [ { datatype => 'P', description => 'Kaffee' },
573
                            { datatype => 'C', name => 'Meier'} ],
574
                          'multiplex: case insensitive header from csv works';
575

  
576
#####
577

  
578
$csv = SL::Helper::Csv->new(
579
  file   => \"P;Kaffee\nC;Meier",          # " # make emacs happy
580
  header =>  [[ 'Datatype', 'Description' ], [ 'Datatype', 'Name']],
581
  case_insensitive_header => 1,
582
  profile => [
583
    {
584
      profile   => { datatype => 'datatype', description => 'description' },
585
      class     => 'SL::DB::Part',
586
      row_ident => 'P'
587
    },
588
    {
589
      profile => { datatype => 'datatype', name => 'name' },
590
      class  => 'SL::DB::Customer',
591
      row_ident => 'C'
592
    }
593
  ],
594
);
595
$csv->parse;
596
is_deeply $csv->get_data, [ { datatype => 'P', description => 'Kaffee' },
597
                            { datatype => 'C', name => 'Meier' } ],
598
                          'multiplex: case insensitive header as param works';
599

  
600

  
601
#####
602

  
603
$csv = SL::Helper::Csv->new(
604
  file   => \"P;Kaffee\nC;Meier",          # " # make emacs happy
605
  header =>  [[ 'Datatype', 'Description' ], [ 'Datatype', 'Name']],
606
  profile => [
607
    {
608
      profile   => { datatype => 'datatype', description => 'description' },
609
      class     => 'SL::DB::Part',
610
      row_ident => 'P'
611
    },
612
    {
613
      profile => { datatype => 'datatype', name => 'name' },
614
      class  => 'SL::DB::Customer',
615
      row_ident => 'C'
616
    }
617
  ],
618
);
619
$csv->parse;
620
is_deeply $csv->get_data, undef, 'multiplex: case insensitive header without flag ignores';
621

  
622
#####
623

  
624
$csv = SL::Helper::Csv->new(
625
  file   => \<<EOL,
626
P;Kaffee;lecker
627
C;Meier;froh
628
EOL
629
# " # make emacs happy
630
  header => [[ 'datatype', 'Afoo', 'Abar' ], [ 'datatype', 'Bfoo', 'Bbar']],
631
  profile => [{
632
    profile   => { datatype => '', Afoo => '', Abar => '' },
633
    class     => 'SL::DB::Part',
634
    row_ident => 'P'
635
  },
636
  {
637
    profile   => { datatype => '', Bfoo => '', Bbar => '' },
638
    class     => 'SL::DB::Customer',
639
    row_ident => 'C'
640
  }],
641
);
642
$csv->parse;
643

  
644
is_deeply $csv->get_data,
645
    [ { datatype => 'P', Afoo => 'Kaffee', Abar => 'lecker' }, { datatype => 'C', Bfoo => 'Meier', Bbar => 'froh' } ],
646
    'multiplex: empty path still gets parsed into data';
647
ok $csv->get_objects->[0], 'multiplex: empty path gets ignored in object creation';
648

  
649
#####
650

  
651
$csv = SL::Helper::Csv->new(
652
  file   => \<<EOL,
653
P;Kaffee;lecker
654
C;Meier;froh
655
EOL
656
# " # make emacs happy
657
  header => [[ 'datatype', 'Afoo', 'Abar' ], [ 'datatype', 'Bfoo', 'Bbar']],
658
  strict_profile => 1,
659
  profile => [{
660
    profile   => { datatype => '', Afoo => '', Abar => '' },
661
    class     => 'SL::DB::Part',
662
    row_ident => 'P'
663
  },
664
  {
665
    profile   => { datatype => '', Bfoo => '', Bbar => '' },
666
    class     => 'SL::DB::Customer',
667
    row_ident => 'C'
668
  }],
669
);
670
$csv->parse;
671

  
672
is_deeply $csv->get_data,
673
    [ { datatype => 'P', Afoo => 'Kaffee', Abar => 'lecker' }, { datatype => 'C', Bfoo => 'Meier', Bbar => 'froh' } ],
674
    'multiplex: empty path still gets parsed into data (strict profile)';
675
ok $csv->get_objects->[0], 'multiplex: empty path gets ignored in object creation (strict profile)';
676

  
677
#####
678

  
552 679

  
553 680
# vim: ft=perl
554 681
# set emacs to perl mode

Auch abrufbar als: Unified diff