line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Sah::DefaultValueCommon; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19
|
use 5.010001; |
|
1
|
|
|
|
|
4
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict 'subs', 'vars'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
552
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
7
|
|
|
|
|
|
|
our $DATE = '2023-03-30'; # DATE |
8
|
|
|
|
|
|
|
our $DIST = 'Data-Sah-DefaultValue'; # DIST |
9
|
|
|
|
|
|
|
our $VERSION = '0.003'; # VERSION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our %common_args = ( |
12
|
|
|
|
|
|
|
default_value_rules => { |
13
|
|
|
|
|
|
|
summary => 'A specification of default-value rules to use (or avoid)', |
14
|
|
|
|
|
|
|
schema => ['array*', of=>'str*'], |
15
|
|
|
|
|
|
|
description => <<'_', |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
This setting is used to specify which default-value rules to use (or avoid) in a |
18
|
|
|
|
|
|
|
flexible way. Each element is a string, in the form of either `NAME` to mean |
19
|
|
|
|
|
|
|
specifically include a rule, or `!NAME` to exclude a rule. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
To use the default-value rules R1 and R2: |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
['R1', 'R2'] |
24
|
|
|
|
|
|
|
_ |
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our %gen_default_value_code_args = ( |
29
|
|
|
|
|
|
|
%common_args, |
30
|
|
|
|
|
|
|
source => { |
31
|
|
|
|
|
|
|
summary => 'If set to true, will return coercer source code string'. |
32
|
|
|
|
|
|
|
' instead of compiled code', |
33
|
|
|
|
|
|
|
schema => 'bool', |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
our %SPEC; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$SPEC{get_default_value_rules} = { |
40
|
|
|
|
|
|
|
v => 1.1, |
41
|
|
|
|
|
|
|
summary => 'Get default-value rules', |
42
|
|
|
|
|
|
|
description => <<'_', |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
This routine determines default-value rule modules to use (based on the |
45
|
|
|
|
|
|
|
`default_value_rules` specified), loads them, filters out modules with |
46
|
|
|
|
|
|
|
old/incompatible metadata version, and return the list of rules. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
This common routine is used by compilers, as well as |
49
|
|
|
|
|
|
|
and . |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
_ |
52
|
|
|
|
|
|
|
args => { |
53
|
|
|
|
|
|
|
%common_args, |
54
|
|
|
|
|
|
|
compiler => { |
55
|
|
|
|
|
|
|
schema => 'str*', |
56
|
|
|
|
|
|
|
req => 1, |
57
|
|
|
|
|
|
|
}, |
58
|
|
|
|
|
|
|
}, |
59
|
|
|
|
|
|
|
}; |
60
|
|
|
|
|
|
|
sub get_default_value_rules { |
61
|
4
|
|
|
4
|
1
|
12171
|
my %args = @_; |
62
|
|
|
|
|
|
|
|
63
|
4
|
|
|
|
|
11
|
my $compiler = $args{compiler}; |
64
|
|
|
|
|
|
|
|
65
|
4
|
|
|
|
|
11
|
my $prefix = "Data::Sah::Value::$compiler\::"; |
66
|
|
|
|
|
|
|
|
67
|
4
|
|
|
|
|
8
|
my @rules0; |
68
|
4
|
|
50
|
|
|
7
|
for my $item (@{ $args{default_value_rules} // [] }) { |
|
4
|
|
|
|
|
18
|
|
69
|
5
|
100
|
|
|
|
15
|
my $rule_name = ref $item eq 'ARRAY' ? $item->[0] : $item; |
70
|
5
|
|
|
|
|
17
|
my $is_exclude = $rule_name =~ s/\A!//; |
71
|
5
|
100
|
|
|
|
14
|
if ($is_exclude) { |
72
|
2
|
|
|
|
|
5
|
@rules0 = grep { $_ ne $rule_name } @rules0; |
|
1
|
|
|
|
|
5
|
|
73
|
|
|
|
|
|
|
} else { |
74
|
3
|
50
|
|
|
|
12
|
push @rules0, $item unless grep { $_ eq $rule_name } @rules0; |
|
0
|
|
|
|
|
0
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
9
|
my @rules; |
79
|
4
|
|
|
|
|
9
|
for my $item (@rules0) { |
80
|
3
|
100
|
|
|
|
9
|
my $rule_name = ref $item eq 'ARRAY' ? $item->[0] : $item; |
81
|
3
|
100
|
|
|
|
8
|
my $rule_gen_args = ref $item eq 'ARRAY' ? $item->[1] : undef; |
82
|
3
|
|
|
|
|
7
|
my $mod = $prefix . $rule_name; |
83
|
3
|
|
|
|
|
18
|
(my $mod_pm = "$mod.pm") =~ s!::!/!g; |
84
|
3
|
|
|
|
|
1224
|
require $mod_pm; |
85
|
2
|
|
|
|
|
7
|
my $rule_meta = &{"$mod\::meta"}; |
|
2
|
|
|
|
|
13
|
|
86
|
2
|
|
50
|
|
|
11
|
my $rule_v = ($rule_meta->{v} // 1); |
87
|
2
|
50
|
|
|
|
7
|
if ($rule_v != 1) { |
88
|
0
|
|
|
|
|
0
|
warn "Only value rule module following metadata version 1 is ". |
89
|
|
|
|
|
|
|
"supported, this rule module '$mod' follows metadata version ". |
90
|
|
|
|
|
|
|
"$rule_v and will not be used"; |
91
|
0
|
|
|
|
|
0
|
next; |
92
|
|
|
|
|
|
|
} |
93
|
2
|
|
|
|
|
8
|
my $rule = &{"$mod\::value"}( |
|
2
|
|
|
|
|
10
|
|
94
|
|
|
|
|
|
|
(args => $rule_gen_args) x !!$rule_gen_args, |
95
|
|
|
|
|
|
|
); |
96
|
2
|
|
|
|
|
6
|
$rule->{name} = $rule_name; |
97
|
2
|
|
|
|
|
4
|
$rule->{meta} = $rule_meta; |
98
|
2
|
|
|
|
|
8
|
push @rules, $rule; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
3
|
|
|
|
|
13
|
\@rules; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
# ABSTRACT: Common stuffs for Data::Sah::DefaultValue and Data::Sah::DefaultValueJS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |