| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Data::Rx::CoreType::rec; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: the Rx //rec type | ||||||||||||||||||||||||||||
| 5 | 1 | $Data::Rx::CoreType::rec::VERSION = '0.200007'; | |||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent 'Data::Rx::CoreType'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 54 | ▶ | sub subname { 'rec' } | ||||||||||||||||||||||||||
|
Subroutine
subname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub guts_from_arg { | ||||||||||||||||||||||||||||
| 13 | 15 | ▶ | my ($class, $arg, $rx, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
guts_from_arg: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 15 | ▶ | Carp::croak("unknown arguments to new") unless | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | Data::Rx::Util->_x_subset_keys_y($arg, { | ||||||||||||||||||||||||||||
| 17 | rest => 1, | ||||||||||||||||||||||||||||
| 18 | required => 1, | ||||||||||||||||||||||||||||
| 19 | optional => 1, | ||||||||||||||||||||||||||||
| 20 | }); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 13 | my $guts = {}; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 13 | my $content_schema = {}; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 13 | ▶ | $guts->{rest_schema} = $rx->make_schema($arg->{rest}) if $arg->{rest}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 13 | TYPE: for my $type (qw(required optional)) { | |||||||||||||||||||||||||||
| 29 | 26 | ▶ | next TYPE unless my $entries = $arg->{$type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 21 | for my $entry (keys %$entries) { | |||||||||||||||||||||||||||
| 32 | Carp::croak("$entry appears in both required and optional") | ||||||||||||||||||||||||||||
| 33 | 25 | ▶ | if $content_schema->{ $entry }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | $content_schema->{ $entry } = { | ||||||||||||||||||||||||||||
| 36 | optional => $type eq 'optional', | ||||||||||||||||||||||||||||
| 37 | 24 | schema => $rx->make_schema($entries->{ $entry }), | |||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | $guts->{content_schema} = $content_schema; | |||||||||||||||||||||||||||
| 43 | 12 | return $guts; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub assert_valid { | ||||||||||||||||||||||||||||
| 47 | 406 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
assert_valid: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 406 | ▶ | unless (! Scalar::Util::blessed($value) and ref $value eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not builtin::blessed($value) and ref $value eq "HASH"
MC/DC100%2/2
not builtin::blessed($value) and ref $value eq "HASH"
not builtin::blessed($value)
ref $value eq "HASH"
Truth table100%3/3
not(A) and B
A= not builtin::blessed($value)B= ref $value eq "HASH"
| |||||||||||||||||||||||||||||
| 50 | 344 | $self->fail({ | |||||||||||||||||||||||||||
| 51 | error => [ qw(type) ], | ||||||||||||||||||||||||||||
| 52 | message => "value is not a hashref", | ||||||||||||||||||||||||||||
| 53 | value => $value, | ||||||||||||||||||||||||||||
| 54 | }); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 62 | my $c_schema = $self->{content_schema}; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 62 | my @subchecks; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 62 | my @rest_keys = grep { ! exists $c_schema->{$_} } keys %$value; | |||||||||||||||||||||||||||
| 62 | 62 | ▶ | if (@rest_keys and not $self->{rest_schema}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@rest_keys and not $self->{"rest_schema"}
MC/DC100%2/2
@rest_keys and not $self->{"rest_schema"}
@rest_keys
not $self->{"rest_schema"}
Truth table100%3/3
A and not(B)
A= @rest_keysB= not $self->{"rest_schema"}
| |||||||||||||||||||||||||||||
| 63 | 9 | @rest_keys = sort @rest_keys; | |||||||||||||||||||||||||||
| 64 | 9 | push @subchecks, | |||||||||||||||||||||||||||
| 65 | $self->new_fail({ | ||||||||||||||||||||||||||||
| 66 | error => [ qw(unexpected) ], | ||||||||||||||||||||||||||||
| 67 | keys => [@rest_keys], | ||||||||||||||||||||||||||||
| 68 | message => "found unexpected entries: @rest_keys", | ||||||||||||||||||||||||||||
| 69 | value => $value, | ||||||||||||||||||||||||||||
| 70 | }); | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 62 | ▶ | for my $key ($self->rx->sort_keys ? sort keys %$c_schema : keys %$c_schema) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 99 | my $check = $c_schema->{$key}; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 99 | ▶ | if (not $check->{optional} and not exists $value->{ $key }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$check->{"optional"} or exists $value->{$key}
MC/DC100%2/2
$check->{"optional"} or exists $value->{$key}
$check->{"optional"}
exists $value->{$key}
Truth table100%3/3
A or B
A= $check->{"optional"}B= exists $value->{$key}
| |||||||||||||||||||||||||||||
| 77 | 23 | push @subchecks, | |||||||||||||||||||||||||||
| 78 | $self->new_fail({ | ||||||||||||||||||||||||||||
| 79 | error => [ qw(missing) ], | ||||||||||||||||||||||||||||
| 80 | keys => [$key], | ||||||||||||||||||||||||||||
| 81 | message => "no value given for required entry $key", | ||||||||||||||||||||||||||||
| 82 | value => $value, | ||||||||||||||||||||||||||||
| 83 | }); | ||||||||||||||||||||||||||||
| 84 | 23 | next; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 76 | ▶ | if (exists $value->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | push @subchecks, [ | ||||||||||||||||||||||||||||
| 89 | $value->{$key}, | ||||||||||||||||||||||||||||
| 90 | $check->{schema}, | ||||||||||||||||||||||||||||
| 91 | { data_path => [ [$key, 'key' ] ], | ||||||||||||||||||||||||||||
| 92 | check_path => [ | ||||||||||||||||||||||||||||
| 93 | 59 | ▶ | [ $check->{optional} ? 'optional' : 'required', 'key' ], | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | [ $key, 'key' ], | ||||||||||||||||||||||||||||
| 95 | ], | ||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||
| 97 | ]; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 62 | ▶ | if (@rest_keys && $self->{rest_schema}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@rest_keys and $self->{"rest_schema"}
MC/DC100%2/2
@rest_keys and $self->{"rest_schema"}
@rest_keys
$self->{"rest_schema"}
Truth table100%3/3
A and B
A= @rest_keysB= $self->{"rest_schema"}
| |||||||||||||||||||||||||||||
| 102 | 21 | my %rest = map { $_ => $value->{$_} } @rest_keys; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | push @subchecks, [ | ||||||||||||||||||||||||||||
| 105 | \%rest, | ||||||||||||||||||||||||||||
| 106 | $self->{rest_schema}, | ||||||||||||||||||||||||||||
| 107 | 21 | { check_path => [ ['rest', 'key' ] ], | |||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | ]; | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 62 | $self->perform_subchecks(\@subchecks); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 25 | return 1; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||