toward better error messages
[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 {
21d0e759 69 my ($self, $value, $message) = @_;
70 $message = bless {message=>undef, level=>0}, 'MooseX::Types::Structured::Message'
71 unless $message;
7559b71f 72
21d0e759 73 $message->{level}++;
74
75 if ($self->_compiled_type_constraint->($value, $message)) {
d716430a 76 ## Everything is good, no error message to return
77 return undef;
7559b71f 78 } else {
d716430a 79 ## Whoops, need to figure out the right error message
21d0e759 80 my $args = Devel::PartialDump::dump($value);
81 if(my $messages = $message->{message}) {
82 my $level = $message->{level};
83 my $message_str = ref $messages ? join("\n".(" "x$level)."[+] ",reverse @$messages) : $messages;
84 $message->{level}--;
85
86 if($message->{level}) {
87 return $self->get_message($args);
88 } else {
89 return $self->get_message("$args, Internal Validation Error is: $message_str");
90 }
7559b71f 91 } else {
21d0e759 92 $message->{level}--;
93
7559b71f 94 return $self->get_message($args);
95 }
96 }
97};
98
8dbdca20 99=method generate_constraint_for ($type_constraints)
a30fa891 100
101Given some type constraints, use them to generate validation rules for an ref
102of values (to be passed at check time)
103
104=cut
105
106sub generate_constraint_for {
107 my ($self, $type_constraints) = @_;
108 return sub {
7559b71f 109 my $arg = shift @_;
a30fa891 110 my $constraint_generator = $self->constraint_generator;
7559b71f 111 my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
112 return $result;
a30fa891 113 };
114}
115
8dbdca20 116=method parameterize (@type_constraints)
a30fa891 117
118Given a ref of type constraints, create a structured type.
119
120=cut
121
122sub parameterize {
16aea7bf 123 my ($self, @type_constraints) = @_;
124 my $class = ref $self;
a30fa891 125 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
e327145a 126 my $constraint_generator = $self->__infer_constraint_generator;
67a8bc04 127
16aea7bf 128 return $class->new(
a30fa891 129 name => $name,
130 parent => $self,
131 type_constraints => \@type_constraints,
e327145a 132 constraint_generator => $constraint_generator,
133 );
134}
135
8dbdca20 136=method __infer_constraint_generator
e327145a 137
138This returns a CODEREF which generates a suitable constraint generator. Not
139user servicable, you'll never call this directly.
140
141=cut
142
143sub __infer_constraint_generator {
144 my ($self) = @_;
145 if($self->has_constraint_generator) {
146 return $self->constraint_generator;
147 } else {
148 return sub {
149 ## I'm not sure about this stuff but everything seems to work
67a8bc04 150 my $tc = shift @_;
151 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
8dbdca20 152 $self->constraint->($merged_tc, @_);
e327145a 153 };
8dbdca20 154 }
a30fa891 155}
156
8dbdca20 157=method compile_type_constraint
a30fa891 158
159hook into compile_type_constraint so we can set the correct validation rules.
160
161=cut
162
163around 'compile_type_constraint' => sub {
164 my ($compile_type_constraint, $self, @args) = @_;
8dbdca20 165
a30fa891 166 if($self->has_type_constraints) {
167 my $type_constraints = $self->type_constraints;
168 my $constraint = $self->generate_constraint_for($type_constraints);
8dbdca20 169 $self->_set_constraint($constraint);
a30fa891 170 }
171
172 return $self->$compile_type_constraint(@args);
173};
174
8dbdca20 175=method create_child_type
a4a88fef 176
177modifier to make sure we get the constraint_generator
178
179=cut
180
181around 'create_child_type' => sub {
182 my ($create_child_type, $self, %opts) = @_;
183 return $self->$create_child_type(
184 %opts,
190a34eb 185 constraint_generator => $self->__infer_constraint_generator,
a4a88fef 186 );
187};
188
8dbdca20 189=method is_a_type_of
a4a88fef 190
8dbdca20 191=method is_subtype_of
a4a88fef 192
8dbdca20 193=method equals
a4a88fef 194
16aea7bf 195Override the base class behavior.
196
197=cut
198
199sub equals {
200 my ( $self, $type_or_name ) = @_;
201 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
202
203 return unless $other->isa(__PACKAGE__);
8dbdca20 204
16aea7bf 205 return (
179af711 206 $self->parent->equals($other->parent)
16aea7bf 207 and
179af711 208 $self->type_constraints_equals($other)
16aea7bf 209 );
210}
211
179af711 212sub is_a_type_of {
213 my ( $self, $type_or_name ) = @_;
214 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
215
216 if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
179af711 217 if ( $self->parent->is_a_type_of($other->parent) ) {
179af711 218 return $self->_type_constraints_op_all($other, "is_a_type_of");
219 } elsif ( $self->parent->is_a_type_of($other) ) {
220 return 1;
221 # FIXME compare?
222 } else {
223 return 0;
224 }
225 } else {
226 return $self->SUPER::is_a_type_of($other);
227 }
228}
229
230sub is_subtype_of {
231 my ( $self, $type_or_name ) = @_;
179af711 232 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
179af711 233 if ( $other->isa(__PACKAGE__) ) {
234 if ( $other->type_constraints and $self->type_constraints ) {
235 if ( $self->parent->is_a_type_of($other->parent) ) {
236 return (
237 $self->_type_constraints_op_all($other, "is_a_type_of")
238 and
239 $self->_type_constraints_op_any($other, "is_subtype_of")
240 );
241 } elsif ( $self->parent->is_a_type_of($other) ) {
242 return 1;
243 # FIXME compare?
244 } else {
245 return 0;
246 }
247 } else {
248 if ( $self->type_constraints ) {
249 if ( $self->SUPER::is_subtype_of($other) ) {
250 return 1;
251 } else {
252 return;
253 }
254 } else {
255 return $self->parent->is_subtype_of($other->parent);
256 }
257 }
258 } else {
259 return $self->SUPER::is_subtype_of($other);
260 }
261}
262
8dbdca20 263=method type_constraints_equals
16aea7bf 264
67eec8f7 265Checks to see if the internal type constraints are equal.
16aea7bf 266
267=cut
268
269sub type_constraints_equals {
179af711 270 my ( $self, $other ) = @_;
271 $self->_type_constraints_op_all($other, "equals");
272}
273
274sub _type_constraints_op_all {
275 my ($self, $other, $op) = @_;
276
277 return unless $other->isa(__PACKAGE__);
278
16aea7bf 279 my @self_type_constraints = @{$self->type_constraints||[]};
280 my @other_type_constraints = @{$other->type_constraints||[]};
179af711 281
282 return unless @self_type_constraints == @other_type_constraints;
283
16aea7bf 284 ## Incoming ay be either arrayref or hashref, need top compare both
285 while(@self_type_constraints) {
286 my $self_type_constraint = shift @self_type_constraints;
179af711 287 my $other_type_constraint = shift @other_type_constraints;
8dbdca20 288
179af711 289 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
290 for $self_type_constraint, $other_type_constraint;
16aea7bf 291
fd29a93c 292 my $result = $self_type_constraint->$op($other_type_constraint);
293 return unless $result;
16aea7bf 294 }
8dbdca20 295
16aea7bf 296 return 1; ##If we get this far, everything is good.
297}
298
179af711 299sub _type_constraints_op_any {
300 my ($self, $other, $op) = @_;
301
302 return unless $other->isa(__PACKAGE__);
303
304 my @self_type_constraints = @{$self->type_constraints||[]};
305 my @other_type_constraints = @{$other->type_constraints||[]};
306
307 return unless @self_type_constraints == @other_type_constraints;
308
309 ## Incoming ay be either arrayref or hashref, need top compare both
310 while(@self_type_constraints) {
311 my $self_type_constraint = shift @self_type_constraints;
312 my $other_type_constraint = shift @other_type_constraints;
8dbdca20 313
179af711 314 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
315 for $self_type_constraint, $other_type_constraint;
8dbdca20 316
179af711 317 return 1 if $self_type_constraint->$op($other_type_constraint);
318 }
319
320 return 0;
321}
322
8dbdca20 323=method get_message
a4a88fef 324
797510e3 325Give you a better peek into what's causing the error. For now we stringify the
326incoming deep value with L<Devel::PartialDump> and pass that on to either your
327custom error message or the default one. In the future we'll try to provide a
328more complete stack trace of the actual offending elements
329
330=cut
331
332around 'get_message' => sub {
333 my ($get_message, $self, $value) = @_;
7559b71f 334 $value = Devel::PartialDump::dump($value)
335 if ref $value;
336 return $self->$get_message($value);
797510e3 337};
a4a88fef 338
a30fa891 339=head1 SEE ALSO
340
341The following modules or resources may be of interest.
342
343L<Moose>, L<Moose::Meta::TypeConstraint>
344
a30fa891 345=cut
346
b5deb412 347__PACKAGE__->meta->make_immutable(inline_constructor => 0);