package Types; use v5.34; use warnings; use Type::Utils qw( as coerce declare from via ); use Types::Common qw( Enum Str ); use Type::Library -base, -declare => qw( CreditType InvoiceType ); declare InvoiceType, as Enum [qw/ ACCPAY ACCREC /]; coerce InvoiceType, from Str, via { #warn "Trying to coerce $_\n"; my %types = ( Invoice => 'ACCREC', SupplierInvoice => 'ACCPAY', ); #my $ret = $types{$_}; #warn "Returning $ret\n"; return $types{$_}; } ; declare CreditType, as Enum [qw/ ACCPAYCREDIT ACCRECCREDIT /]; coerce CreditType, from Str, via { warn "Trying to coerce $_\n"; my %types = ( Credit => 'ACCRECCREDIT', SupplierCredit => 'ACCPAYCREDIT', ); my $ret = $types{$_}; warn "Returning $ret\n"; return $types{$_}; } ; 1; #### package MyApp; use v5.34; use warnings; use Moo; use Types qw/ InvoiceType CreditType /; has 'type' => ( is => 'ro', isa => InvoiceType | CreditType, required => 1, coerce => 1, ); sub run { my ($self) = @_; say "Running with ".$self->type; } 1; #### #!/opt/perl5/bin/perl use v5.34; use warnings; use Test::More; use Types qw( InvoiceType CreditType ); { subtest 'Type coercion' => sub { is InvoiceType->coerce('Invoice'), 'ACCREC', 'Can coerce a sales invoice'; is InvoiceType->coerce('SupplierInvoice'), 'ACCPAY', 'Can coerce a supplier invoice'; is CreditType->coerce('Credit'), 'ACCRECCREDIT', 'Can coerce a credit type'; is CreditType->coerce('SupplierCredit'), 'ACCPAYCREDIT', 'Can coerce a supplier credit type'; }; } { my $class = 'MyApp'; use_ok($class); subtest 'Class coercion' => sub { for my $t ( qw/Invoice SupplierInvoice Credit SupplierCredit / ) { note "Type=$t"; my $x = new_ok($class => [ type => $t ]); note "Now Type=".$x->type; } }; } done_testing; #### t/type.t .. # Subtest: Type coercion ok 1 - Can coerce a sales invoice ok 2 - Can coerce a supplier invoice ok 3 - Can coerce a credit type ok 4 - Can coerce a supplier credit type 1..4 ok 1 - Type coercion ok 2 - use MyApp; # Subtest: Class coercion # Type=Invoice ok 1 - An object of class 'MyApp' isa 'MyApp' # Type=SupplierInvoice ok 2 - An object of class 'MyApp' isa 'MyApp' # Type=Credit not ok 3 - MyApp->new() died # Failed test 'MyApp->new() died' # at t/type.t line 31. # Error was: Undef did not pass type constraint "InvoiceType|CreditType" (in $args->{"type"}) at /home/dpaikkos/spl/local/lib/perl5/Test/More.pm line 741 # "InvoiceType|CreditType" requires that the value pass "CreditType" or "InvoiceType" # Undef did not pass type constraint "InvoiceType" # "InvoiceType" is a subtype of "Enum["ACCPAY","ACCREC"]" # "Enum["ACCPAY","ACCREC"]" requires that the value is defined # Undef did not pass type constraint "CreditType"