| 1 | package Data::Sah::Compiler::perl::TH::duration; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | #use Log::Any '$log'; | ||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Mo qw(build default); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Role::Tiny::With; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Scalar::Util qw(blessed looks_like_number); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 2 | extends 'Data::Sah::Compiler::perl::TH'; | |||||||||||||||||||||||||||||
| 13 | 2 | with 'Data::Sah::Type::duration'; | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||
| 16 | 2 | our $DATE = '2024-02-16'; # DATE | |||||||||||||||||||||||||||||
| 17 | 2 | our $DIST = 'Data-Sah'; # DIST | |||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.917'; # VERSION | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | sub handle_type { | ||||||||||||||||||||||||||||
|
Subroutine
handle_type: called CC=4 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 21 | 8 | my ($self, $cd) = @_; | |||||||||||||||||||||||||||||
| 22 | 8 | my $c = $self->compiler; | |||||||||||||||||||||||||||||
| 23 | 8 | my $dt = $cd->{data_term}; | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 8 | ▶ | $cd->{coerce_to} = $cd->{nschema}[1]{"x.perl.coerce_to"} // 'float(secs)'; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$cd->{"nschema"}[1]{"x.perl.coerce_to"} // "float(secs)"
MC/DC0%0/1
$cd->{"nschema"}[1]{"x.perl.coerce_to"} // "float(secs)"
$cd->{"nschema"}[1]{"x.perl.coerce_to"}
Truth table50%1/2
A
A= $cd->{"nschema"}[1]{"x.perl.coerce_to"}
| |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | 8 | my $coerce_to = $cd->{coerce_to}; | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | 8 | ▶ | if ($coerce_to eq 'float(secs)') { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 30 | 8 | $cd->{_ccl_check_type} = "!ref($dt) && $dt =~ /\\A[0-9]+(?:\\.[0-9]+)?\\z/"; # XXX no support exp notation for yet? | |||||||||||||||||||||||||||||
| 31 | } elsif ($coerce_to eq 'DateTime::Duration') { | ||||||||||||||||||||||||||||||
| 32 | 0 | $c->add_runtime_module($cd, 'Scalar::Util'); | |||||||||||||||||||||||||||||
| 33 | 0 | $cd->{_ccl_check_type} = "Scalar::Util::blessed($dt) && $dt\->isa('DateTime::Duration')"; | |||||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||||
| 35 | 0 | die "BUG: Unknown coerce_to value '$coerce_to', use either ". | |||||||||||||||||||||||||||||
| 36 | "float(secs) or DateTime::Duration"; | ||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub superclause_comparable { | ||||||||||||||||||||||||||||
|
Subroutine
superclause_comparable: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 41 | 0 | my ($self, $which, $cd) = @_; | |||||||||||||||||||||||||||||
| 42 | 0 | my $c = $self->compiler; | |||||||||||||||||||||||||||||
| 43 | 0 | my $cv = $cd->{cl_value}; | |||||||||||||||||||||||||||||
| 44 | 0 | my $ct = $cd->{cl_term}; | |||||||||||||||||||||||||||||
| 45 | 0 | my $dt = $cd->{data_term}; | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($cd->{cl_is_expr}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | # i'm lazy, technical debt | ||||||||||||||||||||||||||||||
| 49 | 0 | $c->_die($cd, "duration's comparison with expression not yet supported"); | |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 0 | my $coerce_to = $cd->{coerce_to}; | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($coerce_to eq 'float(secs)') { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($which eq 'is') { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 55 | 0 | $c->add_ccl($cd, "$dt == $ct"); # XXX yeah we're not supposed to use == with floats | |||||||||||||||||||||||||||||
| 56 | } elsif ($which eq 'in') { | ||||||||||||||||||||||||||||||
| 57 | 0 | $c->add_runtime_module('List::Util'); | |||||||||||||||||||||||||||||
| 58 | 0 | $c->add_ccl($cd, "List::Util::first(sub{$dt == \$_}, $ct)"); # XXX yeah we're not supposed to use == with floats | |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||
| 60 | } elsif ($coerce_to eq 'DateTime::Duration') { | ||||||||||||||||||||||||||||||
| 61 | # we need to express this like this because if we just use the raw $cv | ||||||||||||||||||||||||||||||
| 62 | # (dump) it will be unwieldy | ||||||||||||||||||||||||||||||
| 63 | 0 | my $ect = join( | |||||||||||||||||||||||||||||
| 64 | "", | ||||||||||||||||||||||||||||||
| 65 | "DateTime::Duration->new(", | ||||||||||||||||||||||||||||||
| 66 | "years => " .$cv->years.",", | ||||||||||||||||||||||||||||||
| 67 | "months => " .$cv->months.",", | ||||||||||||||||||||||||||||||
| 68 | "weeks => " .$cv->weeks.",", | ||||||||||||||||||||||||||||||
| 69 | "days => " .$cv->days.",", | ||||||||||||||||||||||||||||||
| 70 | "hours => " .$cv->hours.",", | ||||||||||||||||||||||||||||||
| 71 | "minutes => ".$cv->minutes.",", | ||||||||||||||||||||||||||||||
| 72 | "seconds => ".$cv->seconds.",", | ||||||||||||||||||||||||||||||
| 73 | ")", | ||||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($which eq 'is') { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 77 | 0 | $c->add_ccl($cd, "DateTime::Duration->compare($dt, $ect)==0"); | |||||||||||||||||||||||||||||
| 78 | } elsif ($which eq 'in') { | ||||||||||||||||||||||||||||||
| 79 | 0 | $c->add_runtime_module('List::Util'); | |||||||||||||||||||||||||||||
| 80 | 0 | $c->add_ccl($cd, "List::Util::first(sub{DateTime::Duration->compare($dt, \$_)==0}, $ect)"); | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub superclause_sortable { | ||||||||||||||||||||||||||||
|
Subroutine
superclause_sortable: not called CC=17 SCAR=57.2
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 86 | 0 | my ($self, $which, $cd) = @_; | |||||||||||||||||||||||||||||
| 87 | 0 | my $c = $self->compiler; | |||||||||||||||||||||||||||||
| 88 | 0 | my $cv = $cd->{cl_value}; | |||||||||||||||||||||||||||||
| 89 | 0 | my $ct = $cd->{cl_term}; | |||||||||||||||||||||||||||||
| 90 | 0 | my $dt = $cd->{data_term}; | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($cd->{cl_is_expr}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 93 | # i'm lazy, technical debt | ||||||||||||||||||||||||||||||
| 94 | 0 | $c->_die($cd, "duration's comparison with expression not yet supported"); | |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | 0 | my $coerce_to = $cd->{coerce_to}; | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($coerce_to eq 'float(secs)') { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ($which eq 'min') { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 100 | 0 | $c->add_ccl($cd, "$dt >= $cv"); | |||||||||||||||||||||||||||||
| 101 | } elsif ($which eq 'xmin') { | ||||||||||||||||||||||||||||||
| 102 | 0 | $c->add_ccl($cd, "$dt > $cv"); | |||||||||||||||||||||||||||||
| 103 | } elsif ($which eq 'max') { | ||||||||||||||||||||||||||||||
| 104 | 0 | $c->add_ccl($cd, "$dt <= $cv"); | |||||||||||||||||||||||||||||
| 105 | } elsif ($which eq 'xmax') { | ||||||||||||||||||||||||||||||
| 106 | 0 | $c->add_ccl($cd, "$dt < $cv"); | |||||||||||||||||||||||||||||
| 107 | } elsif ($which eq 'between') { | ||||||||||||||||||||||||||||||
| 108 | 0 | $c->add_ccl($cd, "$dt >= $cv->[0] && $dt <= $cv->[1]"); | |||||||||||||||||||||||||||||
| 109 | } elsif ($which eq 'xbetween') { | ||||||||||||||||||||||||||||||
| 110 | 0 | $c->add_ccl($cd, "$dt > $cv->[0] && $dt < $cv->[1]"); | |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||
| 112 | } elsif ($coerce_to eq 'DateTime::Duration') { | ||||||||||||||||||||||||||||||
| 113 | # we need to express this like this because if we just use the raw $cv | ||||||||||||||||||||||||||||||
| 114 | # (dump) it will be unwieldy | ||||||||||||||||||||||||||||||
| 115 | 0 | my ($ect, $ect0, $ect1); | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (ref($cv) eq 'ARRAY') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 117 | 0 | $ect0 = join( | |||||||||||||||||||||||||||||
| 118 | "", | ||||||||||||||||||||||||||||||
| 119 | "DateTime::Duration->new(", | ||||||||||||||||||||||||||||||
| 120 | "years => " .$cv->[0]->years.",", | ||||||||||||||||||||||||||||||
| 121 | "months => " .$cv->[0]->months.",", | ||||||||||||||||||||||||||||||
| 122 | "weeks => " .$cv->[0]->weeks.",", | ||||||||||||||||||||||||||||||
| 123 | "days => " .$cv->[0]->days.",", | ||||||||||||||||||||||||||||||
| 124 | "hours => " .$cv->[0]->hours.",", | ||||||||||||||||||||||||||||||
| 125 | "minutes => ".$cv->[0]->minutes.",", | ||||||||||||||||||||||||||||||
| 126 | "seconds => ".$cv->[0]->seconds.",", | ||||||||||||||||||||||||||||||
| 127 | ")", | ||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||
| 129 | 0 | $ect1 = join( | |||||||||||||||||||||||||||||
| 130 | "", | ||||||||||||||||||||||||||||||
| 131 | "DateTime::Duration->new(", | ||||||||||||||||||||||||||||||
| 132 | "years => " .$cv->[1]->years.",", | ||||||||||||||||||||||||||||||
| 133 | "months => " .$cv->[1]->months.",", | ||||||||||||||||||||||||||||||
| 134 | "weeks => " .$cv->[1]->weeks.",", | ||||||||||||||||||||||||||||||
| 135 | "days => " .$cv->[1]->days.",", | ||||||||||||||||||||||||||||||
| 136 | "hours => " .$cv->[1]->hours.",", | ||||||||||||||||||||||||||||||
| 137 | "minutes => ".$cv->[1]->minutes.",", | ||||||||||||||||||||||||||||||
| 138 | "seconds => ".$cv->[1]->seconds.",", | ||||||||||||||||||||||||||||||
| 139 | ")", | ||||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||||
| 142 | 0 | $ect = join( | |||||||||||||||||||||||||||||
| 143 | "", | ||||||||||||||||||||||||||||||
| 144 | "DateTime::Duration->new(", | ||||||||||||||||||||||||||||||
| 145 | "years => " .$cv->years.",", | ||||||||||||||||||||||||||||||
| 146 | "months => " .$cv->months.",", | ||||||||||||||||||||||||||||||
| 147 | "weeks => " .$cv->weeks.",", | ||||||||||||||||||||||||||||||
| 148 | "days => " .$cv->days.",", | ||||||||||||||||||||||||||||||
| 149 | "hours => " .$cv->hours.",", | ||||||||||||||||||||||||||||||
| 150 | "minutes => ".$cv->minutes.",", | ||||||||||||||||||||||||||||||
| 151 | "seconds => ".$cv->seconds.",", | ||||||||||||||||||||||||||||||
| 152 | ")", | ||||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($which eq 'min') { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 157 | 0 | $c->add_ccl($cd, "DateTime->compare($dt, $ect) >= 0"); | |||||||||||||||||||||||||||||
| 158 | } elsif ($which eq 'xmin') { | ||||||||||||||||||||||||||||||
| 159 | 0 | $c->add_ccl($cd, "DateTime->compare($dt, $ect) > 0"); | |||||||||||||||||||||||||||||
| 160 | } elsif ($which eq 'max') { | ||||||||||||||||||||||||||||||
| 161 | 0 | $c->add_ccl($cd, "DateTime->compare($dt, $ect) <= 0"); | |||||||||||||||||||||||||||||
| 162 | } elsif ($which eq 'xmax') { | ||||||||||||||||||||||||||||||
| 163 | 0 | $c->add_ccl($cd, "DateTime->compare($dt, $ect) < 0"); | |||||||||||||||||||||||||||||
| 164 | } elsif ($which eq 'between') { | ||||||||||||||||||||||||||||||
| 165 | 0 | $c->add_ccl($cd, "DateTime->compare($dt, $ect0) >= 0 && DateTime->compare($dt, $ect1) <= 0"); | |||||||||||||||||||||||||||||
| 166 | } elsif ($which eq 'xbetween') { | ||||||||||||||||||||||||||||||
| 167 | 0 | $c->add_ccl($cd, "DateTime->compare($dt, $ect0) > 0 && DateTime->compare($dt, $ect1) < 0"); | |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | 2 | 1; | |||||||||||||||||||||||||||||
| 173 | # ABSTRACT: perl's type handler for type "duration" | ||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||||