| 1 | package Data::Sah::Coerce; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 8 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 8 | ▶ | no warnings 'once'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 8 | ▶ | use Log::ger; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 8 | ▶ | use Data::Sah::CoerceCommon; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 8 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 8 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||
| 14 | 8 | our $DATE = '2023-10-24'; # DATE | |||||||||||||
| 15 | 8 | our $DIST = 'Data-Sah-Coerce'; # DIST | |||||||||||||
| 16 | 8 | our $VERSION = '0.054'; # VERSION | |||||||||||||
| 17 | |||||||||||||||
| 18 | 8 | our @EXPORT_OK = qw(gen_coercer); | |||||||||||||
| 19 | |||||||||||||||
| 20 | 8 | our %SPEC; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 8 | ▶ | our $Log_Coercer_Code = $ENV{LOG_SAH_COERCER_CODE} // 0; | ||||||||||||
|
Condition50%1/2
$ENV{"LOG_SAH_COERCER_CODE"} // 0
MC/DC0%0/1
$ENV{"LOG_SAH_COERCER_CODE"} // 0
$ENV{"LOG_SAH_COERCER_CODE"}
Truth table50%1/2
A
A= $ENV{"LOG_SAH_COERCER_CODE"}
| |||||||||||||||
| 23 | |||||||||||||||
| 24 | $SPEC{gen_coercer} = { | ||||||||||||||
| 25 | 8 | v => 1.1, | |||||||||||||
| 26 | summary => 'Generate coercer code', | ||||||||||||||
| 27 | description => <<'_', | ||||||||||||||
| 28 | |||||||||||||||
| 29 | This is mostly for testing. Normally the coercion rules will be used from | ||||||||||||||
| 30 | <pm:Data::Sah>. | ||||||||||||||
| 31 | |||||||||||||||
| 32 | _ | ||||||||||||||
| 33 | args => { | ||||||||||||||
| 34 | %Data::Sah::CoerceCommon::gen_coercer_args, | ||||||||||||||
| 35 | }, | ||||||||||||||
| 36 | result_naked => 1, | ||||||||||||||
| 37 | }; | ||||||||||||||
| 38 | 18 | ▶ | sub gen_coercer { | ||||||||||||
|
Subroutine
gen_coercer: called CC=25 SCAR=32.6
Pod
documented
| |||||||||||||||
| 39 | 18 | my %args = @_; | |||||||||||||
| 40 | |||||||||||||||
| 41 | 18 | ▶ | my $rt = $args{return_type} // 'val'; | ||||||||||||
|
Condition100%2/2
$args{"return_type"} // "val"
MC/DC100%1/1
$args{"return_type"} // "val"
$args{"return_type"}
Truth table100%2/2
A
A= $args{"return_type"}
| |||||||||||||||
| 42 | # old values still supported but deprecated | ||||||||||||||
| 43 | 18 | ▶ | $rt = 'bool_coerced+val' if $rt eq 'status+val'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 18 | ▶ | $rt = 'bool_coerced+str_errmsg+val' if $rt eq 'status+err+val'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | 18 | my $rules = Data::Sah::CoerceCommon::get_coerce_rules( | |||||||||||||
| 47 | %args, | ||||||||||||||
| 48 | compiler=>'perl', | ||||||||||||||
| 49 | data_term=>'$data', | ||||||||||||||
| 50 | ); | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 18 | my $code; | |||||||||||||
| 53 | 18 | ▶ | if (@$rules) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 17 | my $code_require = ''; | |||||||||||||
| 55 | 17 | my %mem; | |||||||||||||
| 56 | 17 | for my $rule (@$rules) { | |||||||||||||
| 57 | 58 | ▶ | next unless $rule->{modules}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 44 | for my $mod (keys %{$rule->{modules}}) { | |||||||||||||
| 59 | 51 | ▶ | next if $mem{$mod}++; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 27 | $code_require .= "require $mod;\n"; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 17 | my $expr; | |||||||||||||
| 65 | 17 | for my $i (reverse 0..$#{$rules}) { | |||||||||||||
| 66 | 58 | my $rule = $rules->[$i]; | |||||||||||||
| 67 | 58 | my $prev_term; | |||||||||||||
| 68 | 58 | ▶ | if ($i == $#{$rules}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 17 | ▶ | if ($rt eq 'val') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 70 | 13 | $prev_term = '$data'; | |||||||||||||
| 71 | } elsif ($rt eq 'bool_coerced+val') { | ||||||||||||||
| 72 | 3 | $prev_term = '[undef, $data]'; | |||||||||||||
| 73 | } else { # bool_coerced+str_errmsg+val | ||||||||||||||
| 74 | 1 | $prev_term = '[undef, undef, $data]'; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | } else { | ||||||||||||||
| 77 | 41 | $prev_term = $expr; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 58 | ▶ | if ($rt eq 'val') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 81 | 47 | ▶ | if ($rule->{meta}{might_fail}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 14 | $expr = "do { if ($rule->{expr_match}) { my \$res = $rule->{expr_coerce}; \$res->[0] ? undef : \$res->[1] } else { $prev_term } }"; | |||||||||||||
| 83 | } else { | ||||||||||||||
| 84 | 33 | $expr = "($rule->{expr_match}) ? ($rule->{expr_coerce}) : $prev_term"; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | } elsif ($rt eq 'bool_coerced+val') { | ||||||||||||||
| 87 | 7 | ▶ | if ($rule->{meta}{might_fail}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 2 | $expr = "do { if ($rule->{expr_match}) { my \$res = $rule->{expr_coerce}; \$res->[0] ? [1,\$res->[1]] : [1,\$res->[1]] } else { $prev_term } }"; | |||||||||||||
| 89 | } else { | ||||||||||||||
| 90 | 5 | $expr = "($rule->{expr_match}) ? [1, $rule->{expr_coerce}] : $prev_term"; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } else { # bool_coerced+str_errmsg+val | ||||||||||||||
| 93 | 4 | ▶ | if ($rule->{meta}{might_fail}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 1 | $expr = "do { if ($rule->{expr_match}) { my \$res = $rule->{expr_coerce}; \$res->[0] ? [1, \$res->[0], \$res->[1]] : [1, undef, \$res->[1]] } else { $prev_term } }"; | |||||||||||||
| 95 | } else { | ||||||||||||||
| 96 | 3 | $expr = "($rule->{expr_match}) ? [1, undef, $rule->{expr_coerce}] : $prev_term"; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 17 | ▶ | $code = join( | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 102 | "", | ||||||||||||||
| 103 | $code_require, | ||||||||||||||
| 104 | "sub {\n", | ||||||||||||||
| 105 | " my \$data = shift;\n", | ||||||||||||||
| 106 | " unless (defined \$data) {\n", | ||||||||||||||
| 107 | " ", ($rt eq 'val' ? "return undef;" : | ||||||||||||||
| 108 | $rt eq 'bool_coerced+val' ? "return [undef, undef];" : | ||||||||||||||
| 109 | "return [undef, undef, undef];" # bool_coerced+str_errmsg+val | ||||||||||||||
| 110 | ), "\n", | ||||||||||||||
| 111 | " }\n", | ||||||||||||||
| 112 | " $expr;\n", | ||||||||||||||
| 113 | "}", | ||||||||||||||
| 114 | ); | ||||||||||||||
| 115 | } else { | ||||||||||||||
| 116 | 1 | ▶ | if ($rt eq 'val') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 117 | 1 | $code = 'sub { $_[0] }'; | |||||||||||||
| 118 | } elsif ($rt eq 'bool_coerced+val') { | ||||||||||||||
| 119 | 0 | $code = 'sub { [undef, $_[0]] }'; | |||||||||||||
| 120 | } else { # bool_coerced+str_errmsg+val | ||||||||||||||
| 121 | 0 | $code = 'sub { [undef, undef, $_[0]] }'; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 18 | ▶ | if ($Log_Coercer_Code) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 0 | log_trace("Coercer code (gen args: %s): %s", \%args, $code); | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | 18 | ▶ | return $code if $args{source}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | |||||||||||||||
| 131 | 18 | my $coercer = eval $code; ## no critic: BuiltinFunctions::ProhibitStringyEval | |||||||||||||
| 132 | 18 | ▶ | die if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 18 | $coercer; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 8 | 1; | |||||||||||||
| 137 | # ABSTRACT: Coercion rules for Data::Sah | ||||||||||||||
| 138 | |||||||||||||||
| 139 | __END__ | ||||||||||||||