1 package ## Hide from PAUSE
2 MooseX::Meta::TypeConstraint::Structured;
3 # ABSTRACT: MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
6 use Devel::PartialDump;
7 use Moose::Util::TypeConstraints ();
8 use MooseX::Meta::TypeCoercion::Structured;
9 extends 'Moose::Meta::TypeConstraint';
14 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
15 such a way as that they are all applied to an incoming list of arguments. The
16 idea here is that a Type Constraint could be something like, "An Int followed by
17 an Int and then a Str" and that this could be done so with a declaration like:
19 Tuple[Int,Int,Str]; ## Example syntax
21 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
22 example) which are intended to function together.
24 =attr type_constraints
26 A list of L<Moose::Meta::TypeConstraint> objects.
30 has 'type_constraints' => (
33 predicate=>'has_type_constraints',
36 =attr constraint_generator
38 A subref or closure that contains the way we validate incoming values against
39 a set of type constraints.
43 has 'constraint_generator' => (
46 predicate=>'has_constraint_generator',
52 builder => '_build_coercion',
57 return MooseX::Meta::TypeCoercion::Structured->new(
58 type_constraint => $self,
64 Messing with validate so that we can support niced error messages.
68 override 'validate' => sub {
69 my ($self, @args) = @_;
70 my $message = bless {message=>undef}, 'MooseX::Types::Structured::Message';
72 if ($self->_compiled_type_constraint->(@args, $message)) {
73 ## Everything is good, no error message to return
76 ## Whoops, need to figure out the right error message
77 my $args = Devel::PartialDump::dump(@args);
78 if(my $message = $message->{message}) {
79 return $self->get_message("$args, Internal Validation Error is: $message");
81 return $self->get_message($args);
86 =method generate_constraint_for ($type_constraints)
88 Given some type constraints, use them to generate validation rules for an ref
89 of values (to be passed at check time)
93 sub generate_constraint_for {
94 my ($self, $type_constraints) = @_;
97 my $constraint_generator = $self->constraint_generator;
98 my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
103 =method parameterize (@type_constraints)
105 Given a ref of type constraints, create a structured type.
110 my ($self, @type_constraints) = @_;
111 my $class = ref $self;
112 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
113 my $constraint_generator = $self->__infer_constraint_generator;
118 type_constraints => \@type_constraints,
119 constraint_generator => $constraint_generator,
123 =method __infer_constraint_generator
125 This returns a CODEREF which generates a suitable constraint generator. Not
126 user servicable, you'll never call this directly.
130 sub __infer_constraint_generator {
132 if($self->has_constraint_generator) {
133 return $self->constraint_generator;
136 ## I'm not sure about this stuff but everything seems to work
138 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
139 $self->constraint->($merged_tc, @_);
144 =method compile_type_constraint
146 hook into compile_type_constraint so we can set the correct validation rules.
150 around 'compile_type_constraint' => sub {
151 my ($compile_type_constraint, $self, @args) = @_;
153 if($self->has_type_constraints) {
154 my $type_constraints = $self->type_constraints;
155 my $constraint = $self->generate_constraint_for($type_constraints);
156 $self->_set_constraint($constraint);
159 return $self->$compile_type_constraint(@args);
162 =method create_child_type
164 modifier to make sure we get the constraint_generator
168 around 'create_child_type' => sub {
169 my ($create_child_type, $self, %opts) = @_;
170 return $self->$create_child_type(
172 constraint_generator => $self->__infer_constraint_generator,
178 =method is_subtype_of
182 Override the base class behavior.
187 my ( $self, $type_or_name ) = @_;
188 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
190 return unless $other->isa(__PACKAGE__);
193 $self->parent->equals($other->parent)
195 $self->type_constraints_equals($other)
200 my ( $self, $type_or_name ) = @_;
201 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
203 if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
204 if ( $self->parent->is_a_type_of($other->parent) ) {
205 return $self->_type_constraints_op_all($other, "is_a_type_of");
206 } elsif ( $self->parent->is_a_type_of($other) ) {
213 return $self->SUPER::is_a_type_of($other);
218 my ( $self, $type_or_name ) = @_;
219 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
220 if ( $other->isa(__PACKAGE__) ) {
221 if ( $other->type_constraints and $self->type_constraints ) {
222 if ( $self->parent->is_a_type_of($other->parent) ) {
224 $self->_type_constraints_op_all($other, "is_a_type_of")
226 $self->_type_constraints_op_any($other, "is_subtype_of")
228 } elsif ( $self->parent->is_a_type_of($other) ) {
235 if ( $self->type_constraints ) {
236 if ( $self->SUPER::is_subtype_of($other) ) {
242 return $self->parent->is_subtype_of($other->parent);
246 return $self->SUPER::is_subtype_of($other);
250 =method type_constraints_equals
252 Checks to see if the internal type constraints are equal.
256 sub type_constraints_equals {
257 my ( $self, $other ) = @_;
258 $self->_type_constraints_op_all($other, "equals");
261 sub _type_constraints_op_all {
262 my ($self, $other, $op) = @_;
264 return unless $other->isa(__PACKAGE__);
266 my @self_type_constraints = @{$self->type_constraints||[]};
267 my @other_type_constraints = @{$other->type_constraints||[]};
269 return unless @self_type_constraints == @other_type_constraints;
271 ## Incoming ay be either arrayref or hashref, need top compare both
272 while(@self_type_constraints) {
273 my $self_type_constraint = shift @self_type_constraints;
274 my $other_type_constraint = shift @other_type_constraints;
276 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
277 for $self_type_constraint, $other_type_constraint;
279 my $result = $self_type_constraint->$op($other_type_constraint);
280 return unless $result;
283 return 1; ##If we get this far, everything is good.
286 sub _type_constraints_op_any {
287 my ($self, $other, $op) = @_;
289 return unless $other->isa(__PACKAGE__);
291 my @self_type_constraints = @{$self->type_constraints||[]};
292 my @other_type_constraints = @{$other->type_constraints||[]};
294 return unless @self_type_constraints == @other_type_constraints;
296 ## Incoming ay be either arrayref or hashref, need top compare both
297 while(@self_type_constraints) {
298 my $self_type_constraint = shift @self_type_constraints;
299 my $other_type_constraint = shift @other_type_constraints;
301 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
302 for $self_type_constraint, $other_type_constraint;
304 return 1 if $self_type_constraint->$op($other_type_constraint);
312 Give you a better peek into what's causing the error. For now we stringify the
313 incoming deep value with L<Devel::PartialDump> and pass that on to either your
314 custom error message or the default one. In the future we'll try to provide a
315 more complete stack trace of the actual offending elements
319 around 'get_message' => sub {
320 my ($get_message, $self, $value) = @_;
321 $value = Devel::PartialDump::dump($value)
323 return $self->$get_message($value);
328 The following modules or resources may be of interest.
330 L<Moose>, L<Moose::Meta::TypeConstraint>
334 __PACKAGE__->meta->make_immutable(inline_constructor => 0);