| 1 | package Data::Sah::DefaultValueCommon; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use strict 'subs', 'vars'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 4 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||
| 7 | 4 | our $DATE = '2024-01-17'; # DATE | |||||||||||
| 8 | 4 | our $DIST = 'Data-Sah-DefaultValue'; # DIST | |||||||||||
| 9 | 4 | our $VERSION = '0.005'; # VERSION | |||||||||||
| 10 | |||||||||||||
| 11 | 4 | 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 | 4 | 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 | 4 | our %SPEC; | |||||||||||
| 38 | |||||||||||||
| 39 | $SPEC{get_default_value_rules} = { | ||||||||||||
| 40 | 4 | 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 <pm:Data::Sah> compilers, as well as | ||||||||||||
| 49 | <pm:Data::Sah::DefaultValue> and <pm:Data::Sah::DefaultValueJS>. | ||||||||||||
| 50 | |||||||||||||
| 51 | _ | ||||||||||||
| 52 | args => { | ||||||||||||
| 53 | %common_args, | ||||||||||||
| 54 | compiler => { | ||||||||||||
| 55 | schema => 'str*', | ||||||||||||
| 56 | req => 1, | ||||||||||||
| 57 | }, | ||||||||||||
| 58 | extra_args => { | ||||||||||||
| 59 | summary => 'Extra arguments to pass to value() subroutine', | ||||||||||||
| 60 | schema => 'hash*', | ||||||||||||
| 61 | description => <<'MARKDOWN', | ||||||||||||
| 62 | |||||||||||||
| 63 | This is used, for example, by <pm:Data::Sah> when generating validation code | ||||||||||||
| 64 | from Sah schema. Sometimes the default value rule needs to know additional | ||||||||||||
| 65 | information like what a date type should be coerced to (DateTime object, or | ||||||||||||
| 66 | epoch) so it can generate the appropriate default value. | ||||||||||||
| 67 | |||||||||||||
| 68 | MARKDOWN | ||||||||||||
| 69 | }, | ||||||||||||
| 70 | }, | ||||||||||||
| 71 | }; | ||||||||||||
| 72 | 5 | ▶ | sub get_default_value_rules { | ||||||||||
|
Subroutine
get_default_value_rules: called CC=12 SCAR=25.2
Pod
documented
| |||||||||||||
| 73 | 5 | my %args = @_; | |||||||||||
| 74 | |||||||||||||
| 75 | 5 | my $compiler = $args{compiler}; | |||||||||||
| 76 | |||||||||||||
| 77 | 5 | my $prefix = "Data::Sah::Value::$compiler\::"; | |||||||||||
| 78 | |||||||||||||
| 79 | 5 | my @rules0; | |||||||||||
| 80 | 5 | ▶ | for my $item (@{ $args{default_value_rules} // [] }) { | ||||||||||
|
Condition50%1/2
$args{"default_value_rules"} // []
MC/DC0%0/1
$args{"default_value_rules"} // []
$args{"default_value_rules"}
Truth table50%1/2
A
A= $args{"default_value_rules"}
| |||||||||||||
| 81 | 6 | ▶ | my $rule_name = ref $item eq 'ARRAY' ? $item->[0] : $item; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 6 | my $is_exclude = $rule_name =~ s/\A!//; | |||||||||||
| 83 | 6 | ▶ | if ($is_exclude) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 2 | @rules0 = grep { $_ ne $rule_name } @rules0; | |||||||||||
| 85 | } else { | ||||||||||||
| 86 | 0 | ▶ | push @rules0, $item unless grep { $_ eq $rule_name } @rules0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | } | ||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 5 | my @rules; | |||||||||||
| 91 | 5 | for my $item (@rules0) { | |||||||||||
| 92 | 4 | my ($rule_name, $rule_gen_args); | |||||||||||
| 93 | 4 | ▶ | if (ref $item eq 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 1 | $rule_name = $item->[0]; | |||||||||||
| 95 | 1 | $rule_gen_args = $item->[1]; | |||||||||||
| 96 | } else { | ||||||||||||
| 97 | 3 | ▶ | if ($item =~ /(.*?)=(.*)/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | 1 | $rule_name = $1; | |||||||||||
| 99 | 1 | $rule_gen_args = {split /,/, $2}; | |||||||||||
| 100 | } else { | ||||||||||||
| 101 | 2 | $rule_name = $item; | |||||||||||
| 102 | 2 | $rule_gen_args = undef; | |||||||||||
| 103 | } | ||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 4 | my $mod = $prefix . $rule_name; | |||||||||||
| 107 | 4 | (my $mod_pm = "$mod.pm") =~ s!::!/!g; | |||||||||||
| 108 | 4 | require $mod_pm; | |||||||||||
| 109 | 3 | my $rule_meta = &{"$mod\::meta"}; | |||||||||||
| 110 | 3 | ▶ | my $rule_v = ($rule_meta->{v} // 1); | ||||||||||
|
Condition50%1/2
$rule_meta->{"v"} // 1
MC/DC0%0/1
$rule_meta->{"v"} // 1
$rule_meta->{"v"}
Truth table50%1/2
A
A= $rule_meta->{"v"}
| |||||||||||||
| 111 | 3 | ▶ | if ($rule_v != 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | warn "Only value rule module following metadata version 1 is ". | |||||||||||
| 113 | "supported, this rule module '$mod' follows metadata version ". | ||||||||||||
| 114 | "$rule_v and will not be used"; | ||||||||||||
| 115 | 0 | next; | |||||||||||
| 116 | } | ||||||||||||
| 117 | 3 | my $rule = &{"$mod\::value"}( | |||||||||||
| 118 | (args => $rule_gen_args) x !!$rule_gen_args, | ||||||||||||
| 119 | 3 | ▶ | %{ $args{extra_args} // {} }, | ||||||||||
|
Condition50%1/2
$args{"extra_args"} // {}
MC/DC0%0/1
$args{"extra_args"} // {}
$args{"extra_args"}
Truth table50%1/2
A
A= $args{"extra_args"}
| |||||||||||||
| 120 | ); | ||||||||||||
| 121 | 3 | $rule->{name} = $rule_name; | |||||||||||
| 122 | 3 | $rule->{meta} = $rule_meta; | |||||||||||
| 123 | 3 | push @rules, $rule; | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 4 | \@rules; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 4 | 1; | |||||||||||
| 130 | # ABSTRACT: Common stuffs for Data::Sah::DefaultValue and Data::Sah::DefaultValueJS | ||||||||||||
| 131 | |||||||||||||
| 132 | __END__ | ||||||||||||