| 1 | package Data::Sah::JS; | ||||||||||||
| 2 | |||||||||||||
| 3 | 5 | our $DATE = '2016-09-14'; # DATE | |||||||||||
| 4 | 5 | our $VERSION = '0.87'; # VERSION | |||||||||||
| 5 | |||||||||||||
| 6 | 5 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | #use Log::Any qw($log); | ||||||||||||
| 10 | |||||||||||||
| 11 | 5 | ▶ | use IPC::System::Options; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 5 | ▶ | use Nodejs::Util qw(get_nodejs_path); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 5 | ▶ | our $Log_Validator_Code = $ENV{LOG_SAH_VALIDATOR_CODE} // 0; | ||||||||||
|
Condition50%1/2
$ENV{"LOG_SAH_VALIDATOR_CODE"} // 0
MC/DC0%0/1
$ENV{"LOG_SAH_VALIDATOR_CODE"} // 0
$ENV{"LOG_SAH_VALIDATOR_CODE"}
Truth table50%1/2
A
A= $ENV{"LOG_SAH_VALIDATOR_CODE"}
| |||||||||||||
| 15 | |||||||||||||
| 16 | 5 | require Exporter; | |||||||||||
| 17 | 5 | our @ISA = qw(Exporter); | |||||||||||
| 18 | 5 | our @EXPORT_OK = qw(gen_validator); | |||||||||||
| 19 | |||||||||||||
| 20 | 0 | ▶ | sub gen_validator { | ||||||||||
|
Subroutine
gen_validator: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 21 | 0 | require Data::Sah; | |||||||||||
| 22 | |||||||||||||
| 23 | 0 | my ($schema, $opts) = @_; | |||||||||||
| 24 | |||||||||||||
| 25 | 0 | state $jsc = Data::Sah->new->get_compiler("js"); | |||||||||||
| 26 | |||||||||||||
| 27 | 0 | ▶ | my %args = (schema => $schema, %{$opts // {}}); | ||||||||||
|
Condition0%0/2
$opts // {}
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||
| 28 | 0 | my $opt_source = delete $args{source}; | |||||||||||
| 29 | |||||||||||||
| 30 | 0 | ▶ | $args{log_result} = 1 if $Log_Validator_Code; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | |||||||||||||
| 32 | 0 | my $v_src = $jsc->expr_validator_sub(%args); | |||||||||||
| 33 | 0 | ▶ | return $v_src if $opt_source; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | |||||||||||||
| 35 | 0 | state $nodejs_path = get_nodejs_path(); | |||||||||||
| 36 | 0 | ▶ | die "Can't find node.js in PATH" unless $nodejs_path; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | |||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 40 | 0 | require File::Temp; | |||||||||||
| 41 | 0 | require JSON::MaybeXS; | |||||||||||
| 42 | #require String::ShellQuote; | ||||||||||||
| 43 | |||||||||||||
| 44 | 0 | my $data = shift; | |||||||||||
| 45 | |||||||||||||
| 46 | 0 | state $json = JSON::MaybeXS->new->allow_nonref; | |||||||||||
| 47 | |||||||||||||
| 48 | # code to be sent to nodejs | ||||||||||||
| 49 | 0 | my $src = "var validator = $v_src;\n\n". | |||||||||||
| 50 | "console.log(JSON.stringify(validator(". | ||||||||||||
| 51 | $json->encode($data).")))"; | ||||||||||||
| 52 | |||||||||||||
| 53 | 0 | my ($jsh, $jsfn) = File::Temp::tempfile(); | |||||||||||
| 54 | 0 | print $jsh $src; | |||||||||||
| 55 | 0 | ▶ | close($jsh) or die "Can't write JS code to file $jsfn: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | |||||||||||||
| 57 | 0 | my $out = IPC::System::Options::readpipe($nodejs_path, $jsfn); | |||||||||||
| 58 | 0 | $json->decode($out); | |||||||||||
| 59 | 0 | }; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | 5 | 1; | |||||||||||
| 63 | # ABSTRACT: Some functions to use JavaScript Sah validator code from Perl | ||||||||||||
| 64 | |||||||||||||
| 65 | __END__ | ||||||||||||