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, $value, $message_stack) = @_;
70 unless ($message_stack) {
71 $message_stack = MooseX::Types::Structured::MessageStack->new();
74 $message_stack->inc_level;
76 if ($self->_compiled_type_constraint->($value, $message_stack)) {
77 ## Everything is good, no error message to return
80 ## Whoops, need to figure out the right error message
81 my $args = Devel::PartialDump::dump($value);
82 $message_stack->dec_level;
83 if($message_stack->has_messages) {
84 if($message_stack->level) {
85 ## we are inside a deeply structured constraint
86 return $self->get_message($args);
88 my $message_str = $message_stack->as_string;
89 return $self->get_message("$args, Internal Validation Error is: $message_str");
92 return $self->get_message($args);
97 =method generate_constraint_for ($type_constraints)
99 Given some type constraints, use them to generate validation rules for an ref
100 of values (to be passed at check time)
104 sub generate_constraint_for {
105 my ($self, $type_constraints) = @_;
108 my $constraint_generator = $self->constraint_generator;
109 my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
114 =method parameterize (@type_constraints)
116 Given a ref of type constraints, create a structured type.
121 my ($self, @type_constraints) = @_;
122 my $class = ref $self;
123 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
124 my $constraint_generator = $self->__infer_constraint_generator;
129 type_constraints => \@type_constraints,
130 constraint_generator => $constraint_generator,
134 =method __infer_constraint_generator
136 This returns a CODEREF which generates a suitable constraint generator. Not
137 user servicable, you'll never call this directly.
141 sub __infer_constraint_generator {
143 if($self->has_constraint_generator) {
144 return $self->constraint_generator;
147 ## I'm not sure about this stuff but everything seems to work
149 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
150 $self->constraint->($merged_tc, @_);
155 =method compile_type_constraint
157 hook into compile_type_constraint so we can set the correct validation rules.
161 around 'compile_type_constraint' => sub {
162 my ($compile_type_constraint, $self, @args) = @_;
164 if($self->has_type_constraints) {
165 my $type_constraints = $self->type_constraints;
166 my $constraint = $self->generate_constraint_for($type_constraints);
167 $self->_set_constraint($constraint);
170 return $self->$compile_type_constraint(@args);
173 =method create_child_type
175 modifier to make sure we get the constraint_generator
179 around 'create_child_type' => sub {
180 my ($create_child_type, $self, %opts) = @_;
181 return $self->$create_child_type(
183 constraint_generator => $self->__infer_constraint_generator,
189 =method is_subtype_of
193 Override the base class behavior.
198 my ( $self, $type_or_name ) = @_;
199 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
201 return unless $other->isa(__PACKAGE__);
204 $self->parent->equals($other->parent)
206 $self->type_constraints_equals($other)
211 my ( $self, $type_or_name ) = @_;
212 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
214 if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
215 if ( $self->parent->is_a_type_of($other->parent) ) {
216 return $self->_type_constraints_op_all($other, "is_a_type_of");
217 } elsif ( $self->parent->is_a_type_of($other) ) {
224 return $self->SUPER::is_a_type_of($other);
229 my ( $self, $type_or_name ) = @_;
230 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
231 if ( $other->isa(__PACKAGE__) ) {
232 if ( $other->type_constraints and $self->type_constraints ) {
233 if ( $self->parent->is_a_type_of($other->parent) ) {
235 $self->_type_constraints_op_all($other, "is_a_type_of")
237 $self->_type_constraints_op_any($other, "is_subtype_of")
239 } elsif ( $self->parent->is_a_type_of($other) ) {
246 if ( $self->type_constraints ) {
247 if ( $self->SUPER::is_subtype_of($other) ) {
253 return $self->parent->is_subtype_of($other->parent);
257 return $self->SUPER::is_subtype_of($other);
261 =method type_constraints_equals
263 Checks to see if the internal type constraints are equal.
267 sub type_constraints_equals {
268 my ( $self, $other ) = @_;
269 $self->_type_constraints_op_all($other, "equals");
272 sub _type_constraints_op_all {
273 my ($self, $other, $op) = @_;
275 return unless $other->isa(__PACKAGE__);
277 my @self_type_constraints = @{$self->type_constraints||[]};
278 my @other_type_constraints = @{$other->type_constraints||[]};
280 return unless @self_type_constraints == @other_type_constraints;
282 ## Incoming ay be either arrayref or hashref, need top compare both
283 while(@self_type_constraints) {
284 my $self_type_constraint = shift @self_type_constraints;
285 my $other_type_constraint = shift @other_type_constraints;
287 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
288 for $self_type_constraint, $other_type_constraint;
290 my $result = $self_type_constraint->$op($other_type_constraint);
291 return unless $result;
294 return 1; ##If we get this far, everything is good.
297 sub _type_constraints_op_any {
298 my ($self, $other, $op) = @_;
300 return unless $other->isa(__PACKAGE__);
302 my @self_type_constraints = @{$self->type_constraints||[]};
303 my @other_type_constraints = @{$other->type_constraints||[]};
305 return unless @self_type_constraints == @other_type_constraints;
307 ## Incoming ay be either arrayref or hashref, need top compare both
308 while(@self_type_constraints) {
309 my $self_type_constraint = shift @self_type_constraints;
310 my $other_type_constraint = shift @other_type_constraints;
312 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
313 for $self_type_constraint, $other_type_constraint;
315 return 1 if $self_type_constraint->$op($other_type_constraint);
323 Give you a better peek into what's causing the error. For now we stringify the
324 incoming deep value with L<Devel::PartialDump> and pass that on to either your
325 custom error message or the default one. In the future we'll try to provide a
326 more complete stack trace of the actual offending elements
330 around 'get_message' => sub {
331 my ($get_message, $self, $value) = @_;
332 $value = Devel::PartialDump::dump($value)
334 return $self->$get_message($value);
339 The following modules or resources may be of interest.
341 L<Moose>, L<Moose::Meta::TypeConstraint>
345 __PACKAGE__->meta->make_immutable(inline_constructor => 0);