AsType/Draft2020_12/Types.pm

filter: Statementstmt 81.7% 67 / 82 Branchbran 25.0% 6 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 24 / 27 Podpod n/a totaltotal 72.9% 97 / 133 CC 13 SCAR 12.4
CC
13
Cov
69%
__ANON__
12.0
__ANON__
6.0
__ANON__
5.6
CRAP 3.4
« AsType/Draft2020_12/Vocabulary/Applicator.pm ↑ Summary AsType/Draft2019_09/Types.pm »
1 package JSON::Schema::AsType::Draft2020_12::Types;
2 10 our $AUTHORITY = 'cpan:YANICK';
3 10 $JSON::Schema::AsType::Draft2020_12::Types::VERSION = '1.0.0';
4 # ABSTRACT: Type::Tiny types for draft 2020-12 schemas
5  
6  
7 10 ▶use 5.42.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use feature qw/ module_true /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Hash::Merge qw/ merge /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Type::Utils -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Types::Standard qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
15 Str StrictNum HashRef ArrayRef
16 Int
17 Dict slurpy Optional Any
18 Tuple
19 InstanceOf
20 10 /;
21  
22 use Type::Library
23 10 ▶ -base,
Subroutine BEGIN: called CC=1 SCAR=0.0
24 -declare => qw(
25 PrefixItems
26 Contains
27 MinContains MaxContains
28 10 );
29  
30 10 ▶use List::MoreUtils qw/ zip none any all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 10 ▶use List::Util qw/ pairs pairmap reduce uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 10 ▶use JSON::Schema::AsType;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 10 ▶use JSON::Schema::AsType::Annotations;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 10 ▶use JSON::Schema::AsType::Draft4::Types qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
37 Integer Boolean Number String Null Object Array Items
38 ExclusiveMinimum ExclusiveMaximum Dependencies Dependency
39 Not MultipleOf
40 10 /;
41  
42 #__PACKAGE__->meta->add_type( $_ ) for Integer, Boolean, Number, String, Null, Object, Array, Items, ExclusiveMaximum, ExclusiveMinimum;
43  
44 declare PrefixItems,
45 16 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=4 SCAR=16.5
46 16 my $types = shift;
47  
48 16 ▶ if ( Boolean->check($types) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Boolean()->check($types)) 0 16 16
49 0 ▶ return $types ? Any : sub { !@$_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$types ? : 0 0 0
50 }
51  
52 my $type =
53 ref $types eq 'ARRAY'
54 16 ▶ ? Tuple [ ( map { Optional [$_] } @$types ), slurpy Any ]
Branch: 1 branch
BranchTrueFalseTotal
ref $types eq 'ARRAY' ? : 16 0 16
55 : Tuple [ slurpy ArrayRef [$types] ];
56  
57 return ~ArrayRef | (
58 59 ▶ $type & sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
59 59 ▶ if ( ref $types eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $types eq 'ARRAY') { } 59 0 59
60 59 add_annotation( 'prefixItems' => 0 .. $types->$#* );
61 }
62 else {
63 0 add_annotation( 'prefixItems' => 0 .. $_->$#* );
64 }
65 59 return 1;
66 }
67 16 );
68  
69 10 };
70  
71 15 ▶declare Contains, constraint_generator => sub($type) {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 129 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 129 ▶ return any { $type->check($_) } @$_;
Subroutine __ANON__: called CC=1 SCAR=0.0
74 }
75 15 };
76  
77 4 ▶declare MinContains, constraint_generator => sub($min) {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 25 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 25 annotation_for('contains')->@* >= $min;
80 }
81 4 };
82  
83 4 ▶declare MaxContains, constraint_generator => sub($max) {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 16 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 16 annotation_for('contains')->@* <= $max;
86 }
87 4 };
88  
89 26 ▶declare Items, constraint_generator => sub {
Subroutine __ANON__: called CC=3 SCAR=17.2
90 26 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 26 0 26
91 26 my $to_skip = shift;
92 26 my $schema = shift;
93 242 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=17.3
94  
95 242 ▶ return unless ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 0 242 242
96  
97 242 my @v = @$_;
98  
99 242 my @additional = splice @v, $to_skip;
100  
101 242 ▶ if ( ref $schema eq 'JSON::PP::Boolean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "JSON::PP::Boolean") 0 242 242
102 0 my $verdict = @additional;
103 0 ▶ $verdict = !$verdict unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
104 0 return $verdict;
105 }
106  
107 242 ▶ return all { $schema->check($_) } @additional;
Subroutine __ANON__: called CC=1 SCAR=0.0
108 }
109 26 }
110 else {
111 0 my $size = shift;
112 0 ▶ if ( ref $size eq 'JSON::PP::Boolean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $size eq "JSON::PP::Boolean") 0 0 0
113 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
114 0 ▶ my $s = ref($_) eq 'ARRAY' ? @_ : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
115 0 $DB::single = 1;
116 0 ▶ return !!$size ? $s : !$s;
Branch: 1 branch
BranchTrueFalseTotal
!!$size ? : 0 0 0
117 }
118 0 }
119 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
120 0 ▶ my $s = ref($_) eq 'ARRAY' ? @_ : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
121 0 $s <= $size;
122 0 };
123 }
124 10 };
125  
126 __END__
« AsType/Draft2020_12/Vocabulary/Applicator.pm ↑ Summary AsType/Draft2019_09/Types.pm »