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.
69 my $message = shift @_;
70 $message =~s/MooseX::Types::Structured:://g;
74 override 'validate' => sub {
75 my ($self, $value, $message_stack) = @_;
76 unless ($message_stack) {
77 $message_stack = MooseX::Types::Structured::MessageStack->new();
80 $message_stack->inc_level;
82 if ($self->_compiled_type_constraint->($value, $message_stack)) {
83 ## Everything is good, no error message to return
86 ## Whoops, need to figure out the right error message
87 my $args = Devel::PartialDump::dump($value);
88 $message_stack->dec_level;
89 if($message_stack->has_messages) {
90 if($message_stack->level) {
91 ## we are inside a deeply structured constraint
92 return $self->get_message($args);
94 my $message_str = $message_stack->as_string;
95 return _clean_message($self->get_message("$args, Internal Validation Error is: $message_str"));
98 return $self->get_message($args);
103 =method generate_constraint_for ($type_constraints)
105 Given some type constraints, use them to generate validation rules for an ref
106 of values (to be passed at check time)
110 sub generate_constraint_for {
111 my ($self, $type_constraints) = @_;
112 return $self->constraint_generator->($self, $type_constraints);
115 =method parameterize (@type_constraints)
117 Given a ref of type constraints, create a structured type.
122 my ($self, @type_constraints) = @_;
123 my $class = ref $self;
124 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
125 my $constraint_generator = $self->__infer_constraint_generator;
130 type_constraints => \@type_constraints,
131 constraint_generator => $constraint_generator,
135 =method __infer_constraint_generator
137 This returns a CODEREF which generates a suitable constraint generator. Not
138 user servicable, you'll never call this directly.
142 sub __infer_constraint_generator {
144 if($self->has_constraint_generator) {
145 return $self->constraint_generator;
148 ## I'm not sure about this stuff but everything seems to work
150 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
151 $self->constraint->($merged_tc, @_);
156 =method compile_type_constraint
158 hook into compile_type_constraint so we can set the correct validation rules.
162 around 'compile_type_constraint' => sub {
163 my ($compile_type_constraint, $self, @args) = @_;
165 if($self->has_type_constraints) {
166 my $type_constraints = $self->type_constraints;
167 my $constraint = $self->generate_constraint_for($type_constraints);
168 $self->_set_constraint($constraint);
171 return $self->$compile_type_constraint(@args);
174 =method create_child_type
176 modifier to make sure we get the constraint_generator
180 around 'create_child_type' => sub {
181 my ($create_child_type, $self, %opts) = @_;
182 return $self->$create_child_type(
184 constraint_generator => $self->__infer_constraint_generator,
190 =method is_subtype_of
194 Override the base class behavior.
199 my ( $self, $type_or_name ) = @_;
200 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
202 return unless $other->isa(__PACKAGE__);
205 $self->parent->equals($other->parent)
207 $self->type_constraints_equals($other)
212 my ( $self, $type_or_name ) = @_;
213 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
215 if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
216 if ( $self->parent->is_a_type_of($other->parent) ) {
217 return $self->_type_constraints_op_all($other, "is_a_type_of");
218 } elsif ( $self->parent->is_a_type_of($other) ) {
225 return $self->SUPER::is_a_type_of($other);
230 my ( $self, $type_or_name ) = @_;
231 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
232 if ( $other->isa(__PACKAGE__) ) {
233 if ( $other->type_constraints and $self->type_constraints ) {
234 if ( $self->parent->is_a_type_of($other->parent) ) {
236 $self->_type_constraints_op_all($other, "is_a_type_of")
238 $self->_type_constraints_op_any($other, "is_subtype_of")
240 } elsif ( $self->parent->is_a_type_of($other) ) {
247 if ( $self->type_constraints ) {
248 if ( $self->SUPER::is_subtype_of($other) ) {
254 return $self->parent->is_subtype_of($other->parent);
258 return $self->SUPER::is_subtype_of($other);
262 =method type_constraints_equals
264 Checks to see if the internal type constraints are equal.
268 sub type_constraints_equals {
269 my ( $self, $other ) = @_;
270 $self->_type_constraints_op_all($other, "equals");
273 sub _type_constraints_op_all {
274 my ($self, $other, $op) = @_;
276 return unless $other->isa(__PACKAGE__);
278 my @self_type_constraints = @{$self->type_constraints||[]};
279 my @other_type_constraints = @{$other->type_constraints||[]};
281 return unless @self_type_constraints == @other_type_constraints;
283 ## Incoming ay be either arrayref or hashref, need top compare both
284 while(@self_type_constraints) {
285 my $self_type_constraint = shift @self_type_constraints;
286 my $other_type_constraint = shift @other_type_constraints;
288 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
289 for $self_type_constraint, $other_type_constraint;
291 my $result = $self_type_constraint->$op($other_type_constraint);
292 return unless $result;
295 return 1; ##If we get this far, everything is good.
298 sub _type_constraints_op_any {
299 my ($self, $other, $op) = @_;
301 return unless $other->isa(__PACKAGE__);
303 my @self_type_constraints = @{$self->type_constraints||[]};
304 my @other_type_constraints = @{$other->type_constraints||[]};
306 return unless @self_type_constraints == @other_type_constraints;
308 ## Incoming ay be either arrayref or hashref, need top compare both
309 while(@self_type_constraints) {
310 my $self_type_constraint = shift @self_type_constraints;
311 my $other_type_constraint = shift @other_type_constraints;
313 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
314 for $self_type_constraint, $other_type_constraint;
316 return 1 if $self_type_constraint->$op($other_type_constraint);
324 Give you a better peek into what's causing the error. For now we stringify the
325 incoming deep value with L<Devel::PartialDump> and pass that on to either your
326 custom error message or the default one. In the future we'll try to provide a
327 more complete stack trace of the actual offending elements
331 around 'get_message' => sub {
332 my ($get_message, $self, $value) = @_;
333 $value = Devel::PartialDump::dump($value)
335 return $self->$get_message($value);
340 The following modules or resources may be of interest.
342 L<Moose>, L<Moose::Meta::TypeConstraint>
346 __PACKAGE__->meta->make_immutable(inline_constructor => 0);