CoerceCommon.pm

filter: Statementstmt 90.7% 69 / 76 Branchbran 75.0% 21 / 28 Conditioncond 46.6% 14 / 30 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 100% 3 / 3 Podpod 100% 1 / 1 totaltotal 70.7% 109 / 154 CC 30 SCAR 37.2
CC
30
Cov
78%
get_coerce_rules
44.1
BEGIN
1.0
BEGIN
1.0
CRAP 41.4
« CoerceJS.pm ↑ Summary Coerce/perl/To_date/From_float/epoch_always.pm »
1 package Data::Sah::CoerceCommon;
2  
3 14 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7 14 our $DATE = '2023-10-24'; # DATE
8 14 our $DIST = 'Data-Sah-Coerce'; # DIST
9 14 our $VERSION = '0.054'; # VERSION
10  
11 14 ▶our $SUPPORT_OLD_PREFIX = $ENV{PERL_DATA_SAH_COERCE_SUPPORT_OLD_PREFIX} // 1;
Condition50%1/2
$ENV{"PERL_DATA_SAH_COERCE_SUPPORT_OLD_PREFIX"} // 1
l!l
014
MC/DC0%0/1
$ENV{"PERL_DATA_SAH_COERCE_SUPPORT_OLD_PREFIX"} // 1
$ENV{"PERL_DATA_SAH_COERCE_SUPPORT_OLD_PREFIX"}
Truth table50%1/2
A
A= $ENV{"PERL_DATA_SAH_COERCE_SUPPORT_OLD_PREFIX"}
Aresult
11
00
12  
13 14 our %Default_Rules = (
14 perl => {
15 bool => [qw//],
16 datenotime => [qw/From_float::epoch From_obj::datetime From_obj::time_moment From_str::iso8601/],
17 date => [qw/From_float::epoch From_obj::datetime From_obj::time_moment From_str::iso8601/],
18 datetime => [qw/From_float::epoch From_obj::datetime From_obj::time_moment From_str::iso8601/],
19 duration => [qw/From_float::seconds From_obj::datetime_duration From_str::hms From_str::human From_str::iso8601/],
20 float => [qw/From_str::percent/],
21 num => [qw/From_str::percent/],
22 timeofday => [qw/From_obj::date_timeofday From_str::hms/],
23 },
24 js => {
25 bool => [qw/From_float::zero_one From_str::common_words/],
26 datenotime => [qw/From_float::epoch From_obj::date From_str::date_parse/],
27 date => [qw/From_float::epoch From_obj::date From_str::date_parse/],
28 datetime => [qw/From_float::epoch From_obj::date From_str::date_parse/],
29 duration => [qw/From_float::seconds From_str::iso8601/],
30 #float => [qw/From_str::percent/],
31 #num => [qw/From_str::percent/],
32 timeofday => [qw/From_str::hms/],
33 },
34 );
35  
36 14 our %common_args = (
37 type => {
38 schema => 'sah::type_name*',
39 req => 1,
40 pos => 0,
41 },
42 coerce_to => {
43 schema => 'str*',
44 description => <<'_',
45  
46 Some Sah types, like `date`, can be represented in a choice of types in the
47 target language. For example, in Perl you can store it as a floating number
48 a.k.a. `float(epoch)`, or as a <pm:DateTime> object, or <pm:Time::Moment>
49 object. Storing in DateTime can be convenient for date manipulation but requires
50 an overhead of loading the module and storing in a bulky format. The choice is
51 yours to make, via this setting.
52  
53 _
54 },
55 coerce_rules => {
56 summary => 'A specification of coercion rules to use (or avoid)',
57 schema => ['array*', of=>'str*'],
58 description => <<'_',
59  
60 This setting is used to specify which coercion rules to use (or avoid) in a
61 flexible way. Each element is a string, in the form of either `NAME` to mean
62 specifically include a rule, or `!NAME` to exclude a rule.
63  
64 Some coercion modules are used by default, unless explicitly avoided using the
65 '!NAME' rule.
66  
67 To not use any rules:
68  
69 To use the default rules plus R1 and R2:
70  
71 ['R1', 'R2']
72  
73 To use the default rules but not R1 and R2:
74  
75 ['!R1', '!R2']
76  
77 _
78 },
79 );
80  
81 14 our %gen_coercer_args = (
82 %common_args,
83 return_type => {
84 schema => ['str*', {
85 in => [qw/val bool_coerced+val bool_coerced+str_errmsg+val/],
86 prefilters => [
87 ["Str::replace_map", {map=>{
88 "status+val" => "bool_coerced+val",
89 "status+err+val" => "bool_coerced+str_errmsg+val",
90 }}],
91 ],
92 }],
93 default => 'val',
94 description => <<'_',
95  
96 `val` means the coercer will return the input (possibly) coerced or undef if
97 coercion fails.
98  
99 `bool_coerced+val` means the coercer will return a 2-element array. The first
100 element is a bool value set to 1 if coercion has been performed or 0 if
101 otherwise. The second element is the (possibly) coerced input.
102  
103 `bool_coerced+str_errmsg+val` means the coercer will return a 3-element array.
104 The first element is a bool value set to 1 if coercion has been performed or 0
105 if otherwise. The second element is the error message string which will be set
106 if there is a failure in coercion (or undef if coercion is successful). The
107 third element is the (possibly) coerced input.
108  
109 _
110 },
111 source => {
112 summary => 'If set to true, will return coercer source code string'.
113 ' instead of compiled code',
114 schema => 'bool',
115 },
116 );
117  
118 14 our %SPEC;
119  
120 $SPEC{get_coerce_rules} = {
121 14 v => 1.1,
122 summary => 'Get coerce rules',
123 description => <<'_',
124  
125 This routine determines coerce rule modules to use (based on the default set and
126 `coerce_rules` specified), loads them, filters out modules with old/incompatible
127 metadata version, and return the list of rules.
128  
129 This common routine is used by <pm:Data::Sah> compilers, as well as
130 <pm:Data::Sah::Coerce> and <pm:Data::Sah::CoerceJS>.
131  
132 _
133 args => {
134 %common_args,
135 compiler => {
136 schema => 'str*',
137 req => 1,
138 },
139 data_term => {
140 schema => 'str*',
141 req => 1,
142 },
143 },
144 };
145 29 ▶sub get_coerce_rules {
Subroutine get_coerce_rules: called CC=30 SCAR=37.9
Pod documented
146 29 my %args = @_;
147  
148 29 my $type = $args{type};
149 29 my $compiler = $args{compiler};
150 29 my $dt = $args{data_term};
151  
152 29 my $typen = $type; $typen =~ s/::/__/g;
153 29 my $old_prefix = "Data::Sah::Coerce::$compiler\::$typen\::"; # deprecated, <0.034, will be removed in the future
154 29 my $prefix = "Data::Sah::Coerce::$compiler\::To_$typen\::";
155  
156 29 ▶ my @rules0 = @{ $Default_Rules{$compiler}{$typen} || [] };
Condition50%1/2
$Data::Sah::CoerceCommon::Default_Rules{$compiler}{$typen} || []
l!l
290
MC/DC0%0/1
$Data::Sah::CoerceCommon::Default_Rules{$compiler}{$typen} || []
$Data::Sah::CoerceCommon::Default_Rules{$compiler}{$typen}
Truth table50%1/2
A
A= $Data::Sah::CoerceCommon::Default_Rules{$compiler}{$typen}
Aresult
11
00
157 29 ▶ for my $item (@{ $args{coerce_rules} // [] }) {
Condition100%2/2
$args{"coerce_rules"} // []
l!l
821
MC/DC100%1/1
$args{"coerce_rules"} // []
$args{"coerce_rules"}
Truth table100%2/2
A
A= $args{"coerce_rules"}
Aresult
11
00
158 9 ▶ my $rule_name = ref $item eq 'ARRAY' ? $item->[0] : $item;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 2 7 9
159 9 my $is_exclude = $rule_name =~ s/\A!//;
160 9 ▶ if ($SUPPORT_OLD_PREFIX && $rule_name =~ /\A\w+\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIX and $rule_name =~ /\A\w+\z/) { } 0 9 9
elsif ($rule_name =~ /\AFrom_[A-Za-z0-9_]+::[A-Za-z0-9_]+\z/) { } 9 0 9
Condition33%1/3
$Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIX and $rule_name =~ /\A\w+\z/
!ll&&!rl&&r
090
MC/DC0%0/2
$Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIX and $rule_name =~ /\A\w+\z/
$Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIX $rule_name =~ /\A\w+\z/
Truth table33%1/3
A and B
A= $Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIXB= $rule_name =~ /\A\w+\z/
ABresult
0X0
100
111
161 # old name
162 } elsif ($rule_name =~ /\AFrom_[A-Za-z0-9_]+::[A-Za-z0-9_]+\z/) {
163 # new name
164 } else {
165 0 die "Invalid syntax for coercion rule item '$item', please ".
166 "only use (!)?From_<type>::<description>";
167 }
168 9 ▶ if ($is_exclude) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_exclude) { } 4 5 9
169 4 @rules0 = grep { $_ ne $rule_name } @rules0;
170 } else {
171 5 ▶ push @rules0, $item unless grep { $_ eq $rule_name } @rules0;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $rule_name;} @rules0 5 0 5
172 }
173 }
174  
175 29 my @rules;
176 29 for my $item (@rules0) {
177 84 ▶ my $rule_name = ref $item eq 'ARRAY' ? $item->[0] : $item;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 2 82 84
178 84 ▶ my $rule_gen_args = ref $item eq 'ARRAY' ? $item->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 2 82 84
179 84 ▶ my $is_old_name = $SUPPORT_OLD_PREFIX && $rule_name =~ /\A\w+\z/;
Condition33%1/3
$Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIX && $rule_name =~ /\A\w+\z/
!ll&&!rl&&r
0840
MC/DC0%0/2
$Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIX && $rule_name =~ /\A\w+\z/
$Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIX $rule_name =~ /\A\w+\z/
Truth table33%1/3
A && B
A= $Data::Sah::CoerceCommon::SUPPORT_OLD_PREFIXB= $rule_name =~ /\A\w+\z/
ABresult
0X0
100
111
180 84 ▶ my $mod = ($is_old_name ? $old_prefix : $prefix) . $rule_name;
Branch: 1 branch
BranchTrueFalseTotal
$is_old_name ? : 0 84 84
181 84 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
182 84 require $mod_pm;
183 83 my $rule_meta = &{"$mod\::meta"};
184 83 ▶ my $rule_v = ($rule_meta->{v} // 1);
Condition50%1/2
$rule_meta->{"v"} // 1
l!l
830
MC/DC0%0/1
$rule_meta->{"v"} // 1
$rule_meta->{"v"}
Truth table50%1/2
A
A= $rule_meta->{"v"}
Aresult
11
00
185 83 ▶ if ($rule_v != 3 && $rule_v != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_v != 3 and $rule_v != 4) 0 83 83
Condition33%1/3
$rule_v != 3 and $rule_v != 4
!ll&&!rl&&r
0830
MC/DC0%0/2
$rule_v != 3 and $rule_v != 4
$rule_v != 3 $rule_v != 4
Truth table33%1/3
A and B
A= $rule_v != 3B= $rule_v != 4
ABresult
0X0
100
111
186 0 warn "Only coercion rule module following metadata version 3/4 is ".
187 "supported, this rule module '$mod' follows metadata version ".
188 "$rule_v and will not be used";
189 0 next;
190 }
191 83 my $rule = &{"$mod\::coerce"}(
192 data_term => $dt,
193 coerce_to => $args{coerce_to},
194 83 (args => $rule_gen_args) x !!$rule_gen_args,
195 );
196 83 $rule->{name} = $rule_name;
197 83 $rule->{meta} = $rule_meta;
198 83 push @rules, $rule;
199 }
200  
201 # sort by priority (then name)
202 @rules = sort {
203 28 ($a->{meta}{prio}//50) <=> ($b->{meta}{prio}//50) ||
204 $a->{name} cmp $b->{name}
205 80 ▶ } @rules;
Condition57%4/7
($Data::Sah::CoerceCommon::a->{'meta'}{'prio'} // 50) <=> ($Data::Sah::CoerceCommon::b->{'meta'}{'prio'} // 50) || $Data::Sah::CoerceCommon::a->{'name'} cmp $Data::Sah::CoerceCommon::b->{'name'}
l!l&&r!l&&!r
18620
$Data::Sah::CoerceCommon::a->{"meta"}{"prio"} // 50
l!l
800
$Data::Sah::CoerceCommon::b->{"meta"}{"prio"} // 50
l!l
800
MC/DC0%0/2
($Data::Sah::CoerceCommon::a->{'meta'}{'prio'} // 50) <=> ($Data::Sah::CoerceCommon::b->{'meta'}{'prio'} // 50) || $Data::Sah::CoerceCommon::a->{'name'} cmp $Data::Sah::CoerceCommon::b->{'name'}
$Data::Sah::CoerceCommon::a->{"meta"}{"prio"} $Data::Sah::CoerceCommon::a->{'name'} cmp $Data::Sah::CoerceCommon::b->{'name'}
MC/DC0%0/1
$Data::Sah::CoerceCommon::b->{"meta"}{"prio"} // 50
$Data::Sah::CoerceCommon::b->{"meta"}{"prio"}
Truth table33%1/3
A || B
A= $Data::Sah::CoerceCommon::a->{"meta"}{"prio"}B= $Data::Sah::CoerceCommon::a->{'name'} cmp $Data::Sah::CoerceCommon::b->{'name'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $Data::Sah::CoerceCommon::b->{"meta"}{"prio"}
Aresult
11
00
206  
207 # precludes
208 {
209 28 my $i = 0;
210 28 ▶ while ($i < @rules) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @rules) 79 28 107
211 79 my $rule = $rules[$i];
212 79 ▶ if ($rule->{meta}{precludes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"meta"}{"precludes"}) 12 67 79
213 12 for my $j (reverse 0 .. $#rules) {
214 46 ▶ next if $j == $i;
Branch: 1 branch
BranchTrueFalseTotal
if $j == $i 12 34 46
215 34 my $match;
216 34 for my $p (@{ $rule->{meta}{precludes} }) {
217 34 ▶ if (ref($p) eq 'Regexp' && $rules[$j]{name} =~ $p ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "Regexp" and $rules[$j]{"name"} =~ /$p/ or $rules[$j]{"name"} eq $p) 0 34 34
Condition33%2/6
ref $p eq "Regexp" and $rules[$j]{"name"} =~ /$p/ or $rules[$j]{"name"} eq $p
l!l&&r!l&&!r
0034
ref $p eq "Regexp" and $rules[$j]{"name"} =~ /$p/
!ll&&!rl&&r
0340
MC/DC0%0/3
ref $p eq "Regexp" and $rules[$j]{"name"} =~ /$p/ or $rules[$j]{"name"} eq $p
ref $p eq "Regexp" $rules[$j]{"name"} =~ /$p/ $rules[$j]{"name"} eq $p
Truth table20%1/5
A and B or C
A= ref $p eq "Regexp"B= $rules[$j]{"name"} =~ /$p/C= $rules[$j]{"name"} eq $p
ABCresult
11X1
0X11
1011
0X00
1000
218 $rules[$j]{name} eq $p) {
219 0 $match = 1;
220 0 last;
221 }
222 }
223 34 ▶ next unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 34 0 34
224 0 warn "Coercion rule $rules[$j]{name} is precluded by rule $rule->{name}";
225 0 splice @rules, $j, 1;
226 }
227 }
228 79 $i++;
229 }
230 }
231  
232 28 \@rules;
233 }
234  
235 14 1;
236 # ABSTRACT: Common stuffs for Data::Sah::Coerce and Data::Sah::CoerceJS
237  
238 __END__
« CoerceJS.pm ↑ Summary Coerce/perl/To_date/From_float/epoch_always.pm »