Hans Dieter Pearcey wrote:
Hi Hans,

Thanks for your quick reply.
I followed your recommendations and used MX::Types::Structured and
'Dict'.
However, I get an error when I am calling my constructor ->new() :

Attribute (cut_types) does not pass the type constraint because:
Validation failed for 'ValidCutTypes' failed with value { blunt => [
"blunt" ] } at lib/Bio/Restriction/Analysis/FrameCutters.pm line 315
where as it is what I specified in my subtype definition?

and here is the code I wrote (followgin your advices):

use Moose;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw/Str Int ArrayRef HashRef/;
use MooseX::Types::Structured qw/Dict/;


subtype 'ValidCutTypes'
  => as Dict[
      cohesive => ArrayRef[Str],
      blunt    => ArrayRef[Str],
     ];

coerce 'ValidCutTypes'
  => from 'ArrayRef'
  => via {
      #Default value
      my $defaults = { cohesive => [qw/5' 3'/], blunt => [qw/blunt/] };
      my $hash = { };

      foreach my $type (@{$_}){
          throw Bio::Root::BadParameter(
              "Wrong type value '$type' [cohesive|blunt]!")
            unless($defaults->{$type});
          $hash->{$type} = $defaults->{$type};
      }
      #[ map { $_ => $defaults->{$_} } @$_ ];
      $hash;
  }
  => from 'HashRef'
  => via { ... }

even if I use '[map { .... } @$_ ]'.

The same probelm occured with passing a hash ref as :

->new(-cut_types => {cohesive => [qw/5'/]})

There's something I am not getting here.

Any clue?

Thanks

> Some notes:
> 
> * ArrayRef[Str] for the values in ValidCutTypes is just a guess based on your
>   existing defaults -- I don't know anything about your real problem domain.

What I would like is an attribute supporting as argument:

- arrayref which is transformed as a hashref of arrayref { $t =>
[qw/$r $g/] }
- hashref which is just check for keys and values and returned as a
hashref of array ref.

> * You don't need to check the keys returned from the ArrayRef coercion, 
> because
>   the Dict[] type constraint will already do that.
> * required => 1 is pretty much meaningless when you provide a default.
> 
> 
> hdp.


-- 
-------------------------
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-------------------------

Reply via email to