| 1 | package Data::Sah::Util::Subschema; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2021-07-29'; # DATE | |||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'Data-Sah-Util-Subschema'; # DIST | |||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.005'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Data::Sah::Normalize qw(normalize_schema); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Data::Sah::Resolve qw(resolve_schema); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | our @EXPORT_OK = qw(extract_subschemas); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | my %clausemeta_cache; # key = TYPE.CLAUSE | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | sub extract_subschemas { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extract_subschemas: called CC=14 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | ▶ | my $opts = ref($_[0]) eq 'HASH' ? shift : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | my $sch = shift; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | my $seen = shift // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // {}
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | $seen->{"$sch"}++ and return (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | ▶ | unless ($opts->{schema_is_normalized}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | $sch = normalize_schema($sch); | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | my $res = resolve_schema( | |||||||||||||||||||||||||||||||||||||||||
| 32 | {schema_is_normalized => 1}, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | $sch); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | my $typeclass = "Data::Sah::Type::$res->{type}"; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | (my $typeclass_pm = "$typeclass.pm") =~ s!::!/!g; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | require $typeclass_pm; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # XXX handle def and/or resolve schema into builtin types. for now we only | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # have one clause set because we don't handle those. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | my @clsets = @{ $res->{'clsets_after_type.alt.merge.merged'} }; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | my @res; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | for my $clset (@clsets) { | |||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | for my $clname (keys %$clset) { | |||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | ▶ | next unless $clname =~ /\A[A-Za-z][A-Za-z0-9_]*\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | my $cache_key = "$sch->[0].$clname"; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | my $clmeta = $clausemeta_cache{$cache_key}; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | unless ($clmeta) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | my $meth = "clausemeta_$clname"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | $clmeta = $clausemeta_cache{$cache_key} = | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | $typeclass->${\("clausemeta_$clname")}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 5 | ▶ | next unless $clmeta->{subschema}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | my $op = $clset->{"$clname.op"}; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | my @clvalues; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | if (defined($op) && ($op eq 'or' || $op eq 'and')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $op and $op eq "or" || $op eq "and"
$op eq "or" or $op eq "and"
MC/DC33%1/3
defined $op and $op eq "or" || $op eq "and"
defined $op
$op eq "or"
$op eq "and"
Truth table50%2/4
A and B or C
A= defined $opB= $op eq "or"C= $op eq "and"
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | @clvalues = @{ $clset->{$clname} }; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | @clvalues = ( $clset->{$clname} ); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | for my $clvalue (@clvalues) { | |||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | my @subsch = $clmeta->{subschema}->($clvalue); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | push @res, @subsch; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | push @res, map { extract_subschemas($opts, $_, $seen) } @subsch; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | @res; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 74 | # ABSTRACT: Extract subschemas from a schema | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||||||||||||||||||||||||||