line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
102309
|
use 5.010001; |
|
2
|
|
|
|
|
17
|
|
2
|
2
|
|
|
2
|
|
14
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
45
|
|
3
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
17
|
|
|
2
|
|
|
|
|
150
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:TOBYINK'; |
7
|
|
|
|
|
|
|
our $VERSION = '0.002'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Data::Sah qw( gen_validator ); |
10
|
2
|
|
|
2
|
|
912
|
use Type::Tiny; |
|
2
|
|
|
|
|
10335
|
|
|
2
|
|
|
|
|
116
|
|
11
|
2
|
|
|
2
|
|
3089
|
|
|
2
|
|
|
|
|
46704
|
|
|
2
|
|
|
|
|
80
|
|
12
|
|
|
|
|
|
|
use Exporter::Shiny qw( sah2type ); |
13
|
2
|
|
|
2
|
|
1644
|
|
|
2
|
|
|
|
|
8499
|
|
|
2
|
|
|
|
|
16
|
|
14
|
|
|
|
|
|
|
state $pl = 'Data::Sah'->new->get_compiler("perl"); |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
2
|
1
|
138
|
my ( $schema, %opts ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
31520
|
return 'Type::Tiny'->new( |
19
|
|
|
|
|
|
|
_data_sah => $schema, |
20
|
|
|
|
|
|
|
constraint => sub { |
21
|
|
|
|
|
|
|
state $coderef = gen_validator( $schema ); |
22
|
|
|
|
|
|
|
@_ = $_; |
23
|
0
|
|
|
0
|
|
0
|
goto $coderef |
24
|
0
|
|
|
|
|
0
|
}, |
25
|
0
|
|
|
|
|
0
|
inlined => sub { |
26
|
|
|
|
|
|
|
my $varname = pop; |
27
|
|
|
|
|
|
|
use Data::Dumper; |
28
|
2
|
|
|
2
|
|
6698
|
if ( $varname =~ /\A\$([^\W0-9]\w*)\z/ ) { |
29
|
2
|
|
|
2
|
|
1622
|
my $cd = $pl->compile( schema => $schema, data_name => "$1" ); |
|
2
|
|
|
|
|
12910
|
|
|
2
|
|
|
|
|
772
|
|
30
|
2
|
50
|
|
|
|
28
|
my $code = $cd->{result}; |
31
|
0
|
|
|
|
|
0
|
return $code if ! @{ $cd->{modules} }; |
32
|
0
|
|
|
|
|
0
|
my $modules = join '', map { |
33
|
0
|
0
|
|
|
|
0
|
$_->{use_statement} |
|
0
|
|
|
|
|
0
|
|
34
|
|
|
|
|
|
|
? sprintf( '%s; ', $_->{use_statement} ) |
35
|
|
|
|
|
|
|
: sprintf( 'require %s; ', $_->{name} ) |
36
|
|
|
|
|
|
|
} @{ $cd->{modules} }; |
37
|
|
|
|
|
|
|
return "do { $modules $code }"; |
38
|
0
|
0
|
|
|
|
0
|
} |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
39
|
0
|
|
|
|
|
0
|
else { |
40
|
|
|
|
|
|
|
my $cd = $pl->compile( schema => $schema ); |
41
|
|
|
|
|
|
|
my $code = $cd->{result}; |
42
|
2
|
|
|
|
|
19
|
my $modules = join '', map { |
43
|
2
|
|
|
|
|
59851
|
$_->{use_statement} |
44
|
|
|
|
|
|
|
? sprintf( '%s; ', $_->{use_statement} ) |
45
|
|
|
|
|
|
|
: sprintf( 'require %s; ', $_->{name} ) |
46
|
|
|
|
|
|
|
} @{ $cd->{modules} }; |
47
|
|
|
|
|
|
|
return "do { my \$data = $varname; $modules $code }"; |
48
|
2
|
100
|
|
|
|
4
|
} |
|
6
|
|
|
|
|
38
|
|
|
2
|
|
|
|
|
8
|
|
49
|
2
|
|
|
|
|
104
|
}, |
50
|
|
|
|
|
|
|
constraint_generator => sub { |
51
|
|
|
|
|
|
|
my @params = @_; |
52
|
|
|
|
|
|
|
my $new_schema = [ @$schema, @params ]; |
53
|
1
|
|
|
1
|
|
6030
|
my $child = sah2type( $new_schema, parameters => \@params ); |
54
|
1
|
|
|
|
|
6
|
$child->check(undef); # force type checks to compile BEFORE parent |
55
|
1
|
|
|
|
|
23
|
$child->{parent} = $Type::Tiny::parameterize_type; |
56
|
1
|
|
|
|
|
53
|
return $child; |
57
|
1
|
|
|
|
|
798
|
}, |
58
|
1
|
|
|
|
|
6
|
%opts, |
59
|
|
|
|
|
|
|
); |
60
|
2
|
|
|
|
|
53
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=pod |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=encoding utf-8 |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 NAME |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Type::FromSah - create an efficient Type::Tiny type constraint from a Data::Sah schema |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 SYNOPSIS |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
package My::Types { |
76
|
|
|
|
|
|
|
use Type::Library -base; |
77
|
|
|
|
|
|
|
use Type::FromSah qw( sah2type ); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__PACKAGE__->add_type( |
80
|
|
|
|
|
|
|
sah2type( [ "int", min => 1, max => 10 ], name => 'SmallInt' ) |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
use MyTypes qw(SmallInt); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
SmallInt->assert_valid( 7 ); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 DESCRIPTION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 Functions |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
This module exports one function. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head3 C<< sah2type( $schema, %options ) >> |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Takes a L<Data::Sah> schema (which should be an arrayref), and generates |
97
|
|
|
|
|
|
|
a L<Type::Tiny> type constraint object for it. Additional key-value pairs |
98
|
|
|
|
|
|
|
will be passed to the Type::Tiny constructor. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 BUGS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Please report any bugs to |
103
|
|
|
|
|
|
|
L<http://rt.cpan.org/Dist/Display.html?Queue=Type-FromSah>. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SEE ALSO |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L<Data::Sah>, L<Type::Tiny>. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 AUTHOR |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Toby Inkster E<lt>tobyink@cpan.orgE<gt>. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENCE |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This software is copyright (c) 2022 by Toby Inkster. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
118
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 DISCLAIMER OF WARRANTIES |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
124
|
|
|
|
|
|
|
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
125
|
|
|
|
|
|
|
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
126
|
|
|
|
|
|
|
|