| 1 | package Data::Sah::Util::Type; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $DATE = '2016-12-09'; # DATE | |||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '0.46'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | our @EXPORT_OK = qw(get_type is_type is_simple is_numeric is_collection is_ref); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # XXX absorb and use metadata from Data::Sah::Type::* | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | our $type_metas = { | |||||||||||||||||||||||||||||||||||||||||
| 16 | all => {scalar=>0, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | any => {scalar=>0, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | array => {scalar=>0, numeric=>0, ref=>1}, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | bool => {scalar=>1, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | buf => {scalar=>1, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | cistr => {scalar=>1, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | code => {scalar=>1, numeric=>0, ref=>1}, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | float => {scalar=>1, numeric=>1, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | hash => {scalar=>0, numeric=>0, ref=>1}, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | int => {scalar=>1, numeric=>1, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | num => {scalar=>1, numeric=>1, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | obj => {scalar=>1, numeric=>0, ref=>1}, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | re => {scalar=>1, numeric=>0, ref=>1, simple=>1}, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | str => {scalar=>1, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | undef => {scalar=>1, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | date => {scalar=>1, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | duration => {scalar=>1, numeric=>0, ref=>0}, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 82 | ▶ | sub get_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_type: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 82 | my $sch = shift; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 82 | ▶ | if (ref($sch) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 24 | $sch = $sch->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 82 | ▶ | if (defined($sch) && !ref($sch)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $sch and not ref $sch
MC/DC0%0/2
defined $sch and not ref $sch
defined $sch
not ref $sch
Truth table33%1/3
A and not(B)
A= defined $schB= not ref $sch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 82 | $sch =~ s/\*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 82 | return $sch; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 24 | ▶ | sub _normalize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 24 | require Data::Sah::Normalize; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 24 | my ($sch, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 24 | ▶ | return $sch if $opts->{schema_is_normalized}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 24 | return Data::Sah::Normalize::normalize_schema($sch); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # for any|all to pass a criteria, we assume that all of the schemas in the 'of' | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # clause must also pass (and there must not be '!of', 'of&', or that kind of | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # thing. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 24 | ▶ | sub _handle_any_all { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_any_all: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 24 | my ($sch, $opts, $crit) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 24 | $sch = _normalize($sch, $opts); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 24 | ▶ | return 0 if $sch->[1]{'of.op'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 16 | my $of = $sch->[1]{of}; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 16 | ▶ | return 0 unless $of && ref($of) eq 'ARRAY' && @$of; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$of and ref $of eq "ARRAY" and @$of
$of and ref $of eq "ARRAY"
MC/DC66%2/3
$of and ref $of eq "ARRAY" and @$of
$of
ref $of eq "ARRAY"
@$of
Truth table75%3/4
A and B and C
A= $ofB= ref $of eq "ARRAY"C= @$of
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 8 | for (@$of) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 14 | ▶ | return 0 unless $crit->($_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | sub is_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_type: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | my ($sch, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | my $type = get_type($sch) or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | my $tmeta = $type_metas->{$type} or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | $type; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 33 | ▶ | sub is_simple { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_simple: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 33 | my ($sch, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 33 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 33 | ▶ | my $type = get_type($sch) or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 33 | ▶ | my $tmeta = $type_metas->{$type} or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 33 | ▶ | if ($type eq 'any' || $type eq 'all') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "any" or $type eq "all"
MC/DC100%2/2
$type eq "any" or $type eq "all"
$type eq "any"
$type eq "all"
Truth table100%3/3
A or B
A= $type eq "any"B= $type eq "all"
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 12 | ▶ | return _handle_any_all($sch, $opts, sub { is_simple(shift) }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 21 | ▶ | return $tmeta->{simple} // ($tmeta->{scalar} && !$tmeta->{ref}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$tmeta->{"simple"} // $tmeta->{"scalar"} && !$tmeta->{"ref"}
$tmeta->{"scalar"} and not $tmeta->{"ref"}
MC/DC100%3/3
$tmeta->{"simple"} // $tmeta->{"scalar"} && !$tmeta->{"ref"}
$tmeta->{"simple"}
$tmeta->{"scalar"}
not $tmeta->{"ref"}
Truth table100%4/4
A // B and not(C)
A= $tmeta->{"simple"}B= $tmeta->{"scalar"}C= not $tmeta->{"ref"}
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 31 | ▶ | sub is_collection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_collection: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 31 | my ($sch, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 31 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 31 | ▶ | my $type = get_type($sch) or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 31 | ▶ | my $tmeta = $type_metas->{$type} or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 31 | ▶ | if ($type eq 'any' || $type eq 'all') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "any" or $type eq "all"
MC/DC100%2/2
$type eq "any" or $type eq "all"
$type eq "any"
$type eq "all"
Truth table100%3/3
A or B
A= $type eq "any"B= $type eq "all"
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 12 | ▶ | return _handle_any_all($sch, $opts, sub { is_collection(shift) }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 19 | return !$tmeta->{scalar}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 9 | ▶ | sub is_numeric { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_numeric: called CC=6 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 9 | my ($sch, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | ▶ | my $type = get_type($sch) or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 9 | ▶ | my $tmeta = $type_metas->{$type} or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 9 | ▶ | if ($type eq 'any' || $type eq 'all') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$type eq "any" or $type eq "all"
MC/DC0%0/2
$type eq "any" or $type eq "all"
$type eq "any"
$type eq "all"
Truth table33%1/3
A or B
A= $type eq "any"B= $type eq "all"
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return _handle_any_all($sch, $opts, sub { is_numeric(shift) }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 9 | return $tmeta->{numeric}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 6 | ▶ | sub is_ref { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_ref: called CC=6 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 6 | my ($sch, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 6 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 6 | ▶ | my $type = get_type($sch) or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 6 | ▶ | my $tmeta = $type_metas->{$type} or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 6 | ▶ | if ($type eq 'any' || $type eq 'all') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$type eq "any" or $type eq "all"
MC/DC0%0/2
$type eq "any" or $type eq "all"
$type eq "any"
$type eq "all"
Truth table33%1/3
A or B
A= $type eq "any"B= $type eq "all"
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | return _handle_any_all($sch, $opts, sub { is_ref(shift) }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 6 | return $tmeta->{ref}; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 131 | # ABSTRACT: Utility functions related to types | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||||||||||||||||