File Coverage

blib/lib/Catmandu/Cmd/convert.pm
Criterion Covered Total %
statement 37 38 97.3
branch 13 16 81.2
condition 4 6 66.6
subroutine 9 9 100.0
pod 2 2 100.0
total 65 71 91.5


line stmt bran cond sub pod time code
1              
2             use Catmandu::Sane;
3 14     14   133494  
  14         34  
  14         73  
4             our $VERSION = '1.2018';
5              
6             use parent 'Catmandu::Cmd';
7 14     14   121 use Catmandu;
  14         28  
  14         70  
8 14     14   912 use Catmandu::Util qw(array_includes);
  14         28  
  14         63  
9 14     14   2556 use namespace::clean;
  14         23  
  14         608  
10 14     14   76  
  14         20  
  14         66  
11             (
12             ["verbose|v", ""],
13             ["fix=s@", ""],
14 15     15 1 232 ["var=s%", ""],
15             ["preprocess|pp", ""],
16             ["start=i", ""],
17             ["total=i", ""],
18             ["id=s@", ""],
19             [
20             "id-file=s",
21             "A line-delimited file containing the id's to include in the conversion. Other records will be ignored."
22             ],
23             );
24             }
25              
26             my ($self, $opts, $args) = @_;
27              
28             my ($from_args, $from_opts, $into_args, $into_opts)
29 15     15 1 44 = $self->_parse_options($args);
30              
31 15         90 my $from = Catmandu->importer($from_args->[0], $from_opts);
32             my $into = Catmandu->exporter($into_args->[0], $into_opts);
33              
34 15         116 if ($opts->id // $opts->id_file) {
35 14         203 my $id_map = {};
36             if (my $ids = $opts->id) {
37 12 100 66     142 $id_map->{$_} = 1 for @$ids;
    100 66        
38 1         10 }
39 1 50       4 else {
40 0         0 Catmandu->importer('Text', file => $opts->id_file)->each(
41             sub {
42             $id_map->{$_[0]->{text}} = 1;
43             }
44             );
45 3     3   12 }
46             $from = $from->select(
47 1         7 sub {defined $_[0]->{_id} && exists $id_map->{$_[0]->{_id}}});
48             }
49             elsif ($opts->start // $opts->total) {
50 1 50   4   47 $from = $from->slice($opts->start, $opts->total);
  4         29  
51             }
52              
53 1         19 if ($opts->fix) {
54             $from = $self->_build_fixer($opts)->fix($from);
55             }
56 12 100       258  
57 5         66 if ($opts->verbose) {
58             $from = $from->benchmark;
59             }
60 9 100       105  
61 2         13 my $n = $into->add_many($from);
62             $into->commit;
63             if ($opts->verbose) {
64 9         100 say STDERR $n == 1 ? "converted 1 item" : "converted $n items";
65 7         144 say STDERR "done";
66 7 100       25 }
67 2 50       165 }
68 2         42  
69             1;
70              
71              
72             =pod
73              
74             =head1 NAME
75              
76             Catmandu::Cmd::convert - convert items
77              
78             =head1 EXAMPLES
79              
80             catmandu convert <IMPORTER> <OPTIONS> to <EXPORTER> <OPTIONS>
81              
82             cat books.json | catmandu convert JSON to CSV --fields id,title
83              
84             catmandu help importer JSON
85             catmandu help exporter YAML
86              
87             =cut