all tests passing again after move to message stack
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Meta / TypeConstraint / Structured.pm
CommitLineData
59deb858 1package ## Hide from PAUSE
2 MooseX::Meta::TypeConstraint::Structured;
8dbdca20 3# ABSTRACT: MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
a30fa891 4
5use Moose;
797510e3 6use Devel::PartialDump;
a30fa891 7use Moose::Util::TypeConstraints ();
16aea7bf 8use MooseX::Meta::TypeCoercion::Structured;
a30fa891 9extends 'Moose::Meta::TypeConstraint';
10
a30fa891 11
12=head1 DESCRIPTION
13
14A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
15such a way as that they are all applied to an incoming list of arguments. The
16idea here is that a Type Constraint could be something like, "An Int followed by
17an Int and then a Str" and that this could be done so with a declaration like:
18
19 Tuple[Int,Int,Str]; ## Example syntax
8dbdca20 20
a30fa891 21So a structure is a list of Type constraints (the "Int,Int,Str" in the above
22example) which are intended to function together.
23
8dbdca20 24=attr type_constraints
a30fa891 25
26A list of L<Moose::Meta::TypeConstraint> objects.
27
28=cut
29
30has 'type_constraints' => (
31 is=>'ro',
32 isa=>'Ref',
33 predicate=>'has_type_constraints',
34);
35
8dbdca20 36=attr constraint_generator
a30fa891 37
38A subref or closure that contains the way we validate incoming values against
39a set of type constraints.
40
41=cut
42
e327145a 43has 'constraint_generator' => (
44 is=>'ro',
45 isa=>'CodeRef',
46 predicate=>'has_constraint_generator',
47);
a30fa891 48
c988e3f2 49has coercion => (
50 is => 'ro',
51 isa => 'Object',
52 builder => '_build_coercion',
53);
54
c988e3f2 55sub _build_coercion {
56 my ($self) = @_;
57 return MooseX::Meta::TypeCoercion::Structured->new(
16aea7bf 58 type_constraint => $self,
c988e3f2 59 );
60}
16aea7bf 61
8dbdca20 62=method validate
7559b71f 63
64Messing with validate so that we can support niced error messages.
8dbdca20 65
7559b71f 66=cut
67
68override 'validate' => sub {
9448ea2c 69 my ($self, $value, $message_stack) = @_;
70 unless ($message_stack) {
71 $message_stack = MooseX::Types::Structured::MessageStack->new();
72 }
7559b71f 73
9448ea2c 74 $message_stack->inc_level;
21d0e759 75
9448ea2c 76 if ($self->_compiled_type_constraint->($value, $message_stack)) {
d716430a 77 ## Everything is good, no error message to return
78 return undef;
7559b71f 79 } else {
d716430a 80 ## Whoops, need to figure out the right error message
21d0e759 81 my $args = Devel::PartialDump::dump($value);
9448ea2c 82 $message_stack->dec_level;
83 if($message_stack->has_messages) {
84 if($message_stack->level) {
85 ## we are inside a deeply structured constraint
21d0e759 86 return $self->get_message($args);
87 } else {
9448ea2c 88 my $message_str = $message_stack->as_string;
21d0e759 89 return $self->get_message("$args, Internal Validation Error is: $message_str");
90 }
7559b71f 91 } else {
92 return $self->get_message($args);
93 }
94 }
95};
96
8dbdca20 97=method generate_constraint_for ($type_constraints)
a30fa891 98
99Given some type constraints, use them to generate validation rules for an ref
100of values (to be passed at check time)
101
102=cut
103
104sub generate_constraint_for {
105 my ($self, $type_constraints) = @_;
106 return sub {
7559b71f 107 my $arg = shift @_;
a30fa891 108 my $constraint_generator = $self->constraint_generator;
7559b71f 109 my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
110 return $result;
a30fa891 111 };
112}
113
8dbdca20 114=method parameterize (@type_constraints)
a30fa891 115
116Given a ref of type constraints, create a structured type.
117
118=cut
119
120sub parameterize {
16aea7bf 121 my ($self, @type_constraints) = @_;
122 my $class = ref $self;
a30fa891 123 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
e327145a 124 my $constraint_generator = $self->__infer_constraint_generator;
67a8bc04 125
16aea7bf 126 return $class->new(
a30fa891 127 name => $name,
128 parent => $self,
129 type_constraints => \@type_constraints,
e327145a 130 constraint_generator => $constraint_generator,
131 );
132}
133
8dbdca20 134=method __infer_constraint_generator
e327145a 135
136This returns a CODEREF which generates a suitable constraint generator. Not
137user servicable, you'll never call this directly.
138
139=cut
140
141sub __infer_constraint_generator {
142 my ($self) = @_;
143 if($self->has_constraint_generator) {
144 return $self->constraint_generator;
145 } else {
146 return sub {
147 ## I'm not sure about this stuff but everything seems to work
67a8bc04 148 my $tc = shift @_;
149 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
8dbdca20 150 $self->constraint->($merged_tc, @_);
e327145a 151 };
8dbdca20 152 }
a30fa891 153}
154
8dbdca20 155=method compile_type_constraint
a30fa891 156
157hook into compile_type_constraint so we can set the correct validation rules.
158
159=cut
160
161around 'compile_type_constraint' => sub {
162 my ($compile_type_constraint, $self, @args) = @_;
8dbdca20 163
a30fa891 164 if($self->has_type_constraints) {
165 my $type_constraints = $self->type_constraints;
166 my $constraint = $self->generate_constraint_for($type_constraints);
8dbdca20 167 $self->_set_constraint($constraint);
a30fa891 168 }
169
170 return $self->$compile_type_constraint(@args);
171};
172
8dbdca20 173=method create_child_type
a4a88fef 174
175modifier to make sure we get the constraint_generator
176
177=cut
178
179around 'create_child_type' => sub {
180 my ($create_child_type, $self, %opts) = @_;
181 return $self->$create_child_type(
182 %opts,
190a34eb 183 constraint_generator => $self->__infer_constraint_generator,
a4a88fef 184 );
185};
186
8dbdca20 187=method is_a_type_of
a4a88fef 188
8dbdca20 189=method is_subtype_of
a4a88fef 190
8dbdca20 191=method equals
a4a88fef 192
16aea7bf 193Override the base class behavior.
194
195=cut
196
197sub equals {
198 my ( $self, $type_or_name ) = @_;
199 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
200
201 return unless $other->isa(__PACKAGE__);
8dbdca20 202
16aea7bf 203 return (
179af711 204 $self->parent->equals($other->parent)
16aea7bf 205 and
179af711 206 $self->type_constraints_equals($other)
16aea7bf 207 );
208}
209
179af711 210sub is_a_type_of {
211 my ( $self, $type_or_name ) = @_;
212 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
213
214 if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
179af711 215 if ( $self->parent->is_a_type_of($other->parent) ) {
179af711 216 return $self->_type_constraints_op_all($other, "is_a_type_of");
217 } elsif ( $self->parent->is_a_type_of($other) ) {
218 return 1;
219 # FIXME compare?
220 } else {
221 return 0;
222 }
223 } else {
224 return $self->SUPER::is_a_type_of($other);
225 }
226}
227
228sub is_subtype_of {
229 my ( $self, $type_or_name ) = @_;
179af711 230 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
179af711 231 if ( $other->isa(__PACKAGE__) ) {
232 if ( $other->type_constraints and $self->type_constraints ) {
233 if ( $self->parent->is_a_type_of($other->parent) ) {
234 return (
235 $self->_type_constraints_op_all($other, "is_a_type_of")
236 and
237 $self->_type_constraints_op_any($other, "is_subtype_of")
238 );
239 } elsif ( $self->parent->is_a_type_of($other) ) {
240 return 1;
241 # FIXME compare?
242 } else {
243 return 0;
244 }
245 } else {
246 if ( $self->type_constraints ) {
247 if ( $self->SUPER::is_subtype_of($other) ) {
248 return 1;
249 } else {
250 return;
251 }
252 } else {
253 return $self->parent->is_subtype_of($other->parent);
254 }
255 }
256 } else {
257 return $self->SUPER::is_subtype_of($other);
258 }
259}
260
8dbdca20 261=method type_constraints_equals
16aea7bf 262
67eec8f7 263Checks to see if the internal type constraints are equal.
16aea7bf 264
265=cut
266
267sub type_constraints_equals {
179af711 268 my ( $self, $other ) = @_;
269 $self->_type_constraints_op_all($other, "equals");
270}
271
272sub _type_constraints_op_all {
273 my ($self, $other, $op) = @_;
274
275 return unless $other->isa(__PACKAGE__);
276
16aea7bf 277 my @self_type_constraints = @{$self->type_constraints||[]};
278 my @other_type_constraints = @{$other->type_constraints||[]};
179af711 279
280 return unless @self_type_constraints == @other_type_constraints;
281
16aea7bf 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;
179af711 285 my $other_type_constraint = shift @other_type_constraints;
8dbdca20 286
179af711 287 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
288 for $self_type_constraint, $other_type_constraint;
16aea7bf 289
fd29a93c 290 my $result = $self_type_constraint->$op($other_type_constraint);
291 return unless $result;
16aea7bf 292 }
8dbdca20 293
16aea7bf 294 return 1; ##If we get this far, everything is good.
295}
296
179af711 297sub _type_constraints_op_any {
298 my ($self, $other, $op) = @_;
299
300 return unless $other->isa(__PACKAGE__);
301
302 my @self_type_constraints = @{$self->type_constraints||[]};
303 my @other_type_constraints = @{$other->type_constraints||[]};
304
305 return unless @self_type_constraints == @other_type_constraints;
306
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;
8dbdca20 311
179af711 312 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
313 for $self_type_constraint, $other_type_constraint;
8dbdca20 314
179af711 315 return 1 if $self_type_constraint->$op($other_type_constraint);
316 }
317
318 return 0;
319}
320
8dbdca20 321=method get_message
a4a88fef 322
797510e3 323Give you a better peek into what's causing the error. For now we stringify the
324incoming deep value with L<Devel::PartialDump> and pass that on to either your
325custom error message or the default one. In the future we'll try to provide a
326more complete stack trace of the actual offending elements
327
328=cut
329
330around 'get_message' => sub {
331 my ($get_message, $self, $value) = @_;
7559b71f 332 $value = Devel::PartialDump::dump($value)
333 if ref $value;
334 return $self->$get_message($value);
797510e3 335};
a4a88fef 336
a30fa891 337=head1 SEE ALSO
338
339The following modules or resources may be of interest.
340
341L<Moose>, L<Moose::Meta::TypeConstraint>
342
a30fa891 343=cut
344
b5deb412 345__PACKAGE__->meta->make_immutable(inline_constructor => 0);