| 1 | package Google::RestApi::Types; | ||
| 2 | |||
| 3 | # custom type constrants. see Type::Library. | ||
| 4 | # NOTE: can't use Google::RestApi::Setup here because that module imports this one. | ||
| 5 | |||
| 6 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | our $VERSION = '2.2.5'; | |
| 10 | |||
| 11 | 2 | ▶ | use Types::Standard qw( Undef Str StrMatch Int ArrayRef HashRef Tuple HasMethods ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 2 | my @types = qw( | |
| 14 | ReadableDir ReadableFile | ||
| 15 | EmptyArrayRef EmptyHashRef | ||
| 16 | EmptyString Zero False | ||
| 17 | HasApi | ||
| 18 | ); | ||
| 19 | |||
| 20 | 2 | ▶ | use Type::Library -base, -declare => @types; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | 2 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 2 | our %EXPORT_TAGS = (all => \@types); | |
| 24 | |||
| 25 | 2 | my $meta = __PACKAGE__->meta; | |
| 26 | |||
| 27 | |||
| 28 | $meta->add_type( | ||
| 29 | name => 'ReadableDir', | ||
| 30 | 142 | ▶ | parent => Str->where( sub { -d -r; } ), |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 31 | 2 | ▶ | message => sub { "Must point to a file system directory that's readable" }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 32 | 2 | ); | |
| 33 | |||
| 34 | $meta->add_type( | ||
| 35 | name => 'ReadableFile', | ||
| 36 | 647 | ▶ | parent => Str->where( sub { -f -r; } ), |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 37 | 4 | ▶ | message => sub { "Must point to a file that's readable" }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 38 | 2 | ); | |
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | $meta->add_type( | ||
| 43 | name => 'EmptyArrayRef', | ||
| 44 | 0 | ▶ | parent => ArrayRef->where( sub { scalar @$_ == 0; } ), |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 45 | 0 | ▶ | message => sub { "Must be an empty array" }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 46 | 2 | ); | |
| 47 | |||
| 48 | $meta->add_type( | ||
| 49 | name => 'EmptyHashRef', | ||
| 50 | 2 | ▶ | parent => HashRef->where( sub { scalar keys %$_ == 0; } ), |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 51 | 0 | ▶ | message => sub { "Must be an empty hash" }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 52 | 2 | ); | |
| 53 | |||
| 54 | my $empty_string = $meta->add_type( | ||
| 55 | name => 'EmptyString', | ||
| 56 | parent => StrMatch[qr/^$/], | ||
| 57 | 0 | ▶ | message => sub { "Must be an empty string" }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 58 | 2 | ); | |
| 59 | |||
| 60 | my $zero = $meta->add_type( | ||
| 61 | name => 'Zero', | ||
| 62 | 34 | ▶ | parent => Int->where( sub { $_ == 0; } ), |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 63 | 0 | ▶ | message => sub { "Must be an int equal to 0" }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 64 | 2 | ); | |
| 65 | |||
| 66 | # TODO: perhaps add emptyarray and emptyhash to this? | ||
| 67 | my $false = $meta->add_type( | ||
| 68 | name => 'False', | ||
| 69 | parent => Undef | $zero | $empty_string, | ||
| 70 | 0 | ▶ | message => sub { "Must evaluate to false" }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 71 | 2 | ); | |
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | $meta->add_type( | ||
| 76 | name => 'HasApi', | ||
| 77 | parent => HasMethods[qw(api)], | ||
| 78 | 0 | ▶ | message => sub { "Must be an api object"; } |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 79 | 2 | ); | |
| 80 | |||
| 81 | 2 | __PACKAGE__->make_immutable; | |
| 82 | |||
| 83 | 2 | 1; | |