File Coverage

blib/lib/Data/Sah/FormatJS.pm
Criterion Covered Total %
statement 20 44 45.4
branch 0 8 0.0
condition n/a
subroutine 7 9 77.7
pod 1 1 100.0
total 28 62 45.1


line stmt bran cond sub pod time code
1             package Data::Sah::FormatJS;
2              
3             our $DATE = '2016-06-13'; # DATE
4             our $VERSION = '0.001'; # VERSION
5              
6 1     1   421 use 5.010001;
  1         2  
7 1     1   2 use strict 'subs', 'vars';
  1         1  
  1         18  
8 1     1   2 use warnings;
  1         1  
  1         16  
9 1     1   620 use Log::Any::IfLOG '$log';
  1         9  
  1         3  
10              
11 1     1   317 use Data::Sah::FormatCommon;
  1         1  
  1         21  
12 1     1   464 use IPC::System::Options;
  1         2616  
  1         5  
13 1     1   381 use Nodejs::Util qw(get_nodejs_path);
  1         195  
  1         307  
14              
15             our %SPEC;
16              
17             our $Log_Formatter_Code = $ENV{LOG_SAH_FORMATTER_CODE} // 0;
18              
19             $SPEC{gen_formatter} = {
20             v => 1.1,
21             summary => 'Generate formatter code',
22             args => {
23             %Data::Sah::FormatCommon::gen_formatter_args,
24             },
25             result_naked => 1,
26             };
27             sub gen_formatter {
28 0     0 1   my %args = @_;
29              
30 0           my $format = $args{format};
31 0           my $pkg = "Data::Sah::Format::js\::$format";
32 0           (my $pkg_pm = "$pkg.pm") =~ s!::!/!g;
33              
34 0           require $pkg_pm;
35              
36 0           my $fmt = &{"$pkg\::format"}(
37             data_term => 'data',
38 0           (args => $args{formatter_args}) x !!defined($args{formatter_args}),
39             );
40              
41 0           my $code = join(
42             "",
43             "function (data) {\n",
44             " return ($fmt->{expr});\n",
45             "}",
46             );
47              
48 0 0         if ($Log_Formatter_Code) {
49 0           $log->tracef("Formatter code (gen args: %s): %s", \%args, $code);
50             }
51              
52 0 0         return $code if $args{source};
53              
54 0           state $nodejs_path = get_nodejs_path();
55 0 0         die "Can't find node.js in PATH" unless $nodejs_path;
56              
57             sub {
58 0     0     require File::Temp;
59 0           require JSON::MaybeXS;
60             #require String::ShellQuote;
61              
62 0           my $data = shift;
63              
64 0           state $json = JSON::MaybeXS->new->allow_nonref;
65              
66             # code to be sent to nodejs
67 0           my $src = "var formatter = $code;\n\n".
68             "console.log(JSON.stringify(formatter(".
69             $json->encode($data).")))";
70              
71 0           my ($jsh, $jsfn) = File::Temp::tempfile();
72 0           print $jsh $src;
73 0 0         close($jsh) or die "Can't write JS code to file $jsfn: $!";
74              
75 0           my $out = IPC::System::Options::readpipe($nodejs_path, $jsfn);
76 0           $json->decode($out);
77 0           };
78             }
79              
80             1;
81             # ABSTRACT: Generate formatter code
82              
83             __END__