| 1 | package GraphQL::MaybeTypeCheck; | ||||||||||
| 2 | |||||||||||
| 3 | 22 | ▶ | use 5.014; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 22 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 22 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 22 | ▶ | use Attribute::Handlers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 22 | ▶ | use Devel::StrictMode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 22 | ▶ | use Import::Into; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | GraphQL::MaybeTypeCheck - Conditional type-checking at runtime | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | use GraphQL::MaybeTypeCheck; | ||||||||||
| 18 | |||||||||||
| 19 | method foo( | ||||||||||
| 20 | $arg1 Str, | ||||||||||
| 21 | $arg2 Int | ||||||||||
| 22 | ) :ReturnType(Map[Str, Int]) { | ||||||||||
| 23 | # ... | ||||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||
| 27 | |||||||||||
| 28 | This module B<optionally> enables type-checking in the caller as implemented by | ||||||||||
| 29 | L<Function::Parameters> and L<Return::Type> depending on whether L<Devel::StrictMode> | ||||||||||
| 30 | is activated. | ||||||||||
| 31 | |||||||||||
| 32 | =head3 C<Devel::StrictMode> ON | ||||||||||
| 33 | |||||||||||
| 34 | When L<Devel::StrictMode> is active, this module will import L<Function::Parameters> | ||||||||||
| 35 | into the caller with its default configuration. As of writing, this includes | ||||||||||
| 36 | checking both argument count and type. | ||||||||||
| 37 | |||||||||||
| 38 | When in strict mode this also C<require>s L<Return::Type> which registers the | ||||||||||
| 39 | C<ReturnType> attribute. | ||||||||||
| 40 | |||||||||||
| 41 | =head3 C<Devel::StrictMode> OFF | ||||||||||
| 42 | |||||||||||
| 43 | When strict mode is inactive this module still imports C<Function::Parameters> | ||||||||||
| 44 | into the caller however it sets C<fun> and C<method> to L<lax mode|Function::Parameters/function_lax> and disables | ||||||||||
| 45 | argument type checking. | ||||||||||
| 46 | |||||||||||
| 47 | This also installs a no-op C<ReturnType> attribute so the existing syntax isn't | ||||||||||
| 48 | broken. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | 864 | ▶ | sub ReturnType : ATTR(CODE) { | ||||||||
|
Subroutine
ReturnType: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 53 | 864 | my ($package, $symbol, $referent, $attr, $data) = @_; | |||||||||
| 54 | |||||||||||
| 55 | # If strict mode is enabled, wrap the sub so the return type is checked | ||||||||||
| 56 | 864 | if (STRICT) { | |||||||||
| 57 | 864 | ▶ | my %args = (@$data % 2) ? (scalar => @$data) : @$data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 864 | Return::Type->wrap_sub($referent, %args); | |||||||||
| 59 | } | ||||||||||
| 60 | 22 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 620 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||
| 63 | 620 | ▶ | return unless $_[0] eq __PACKAGE__; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 369 | my $caller = caller; | |||||||||
| 65 | { | ||||||||||
| 66 | 22 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 369 | push @{"${caller}::ISA"}, __PACKAGE__; | |||||||||
| 68 | } | ||||||||||
| 69 | 369 | if (STRICT) { | |||||||||
| 70 | 369 | Function::Parameters->import::into($caller, ':strict'); | |||||||||
| 71 | 369 | require Return::Type; | |||||||||
| 72 | } else { | ||||||||||
| 73 | Function::Parameters->import::into($caller, { | ||||||||||
| 74 | fun => {defaults => 'function_lax', check_argument_types => 0}, | ||||||||||
| 75 | method => {defaults => 'method_lax', check_argument_types => 0}, | ||||||||||
| 76 | }); | ||||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 22 | 1; | |||||||||