Subject: | Provide method to inline an attribute type check |
I would like a method to inline an attribute type check. Currently, what I'm doing is this: https://github.com/haarg/MooX-TypeTiny/blob/master/lib/Method/Generate/Accessor/Role/TypeTiny.pm It has to do a number of things that feel like they should be part of Type::Tiny. ->inline_assert is close to what I need, but has a few problems.
First is that it won't work for non-inlinable types. If it could be provided with an inlinable string to use for the type, it could do similar to MooX::TypeTiny and return either $check->inline_check($value) or "${var}->check($value)". This seems like a useful option for any type check, not just for attributes.
Second is that it provides no way to pass in attribute information, such as attribute_name, attribute_step, or varname. The assertion class will pick them up from $Method::Generate::Accessor::CurrentAttribute, but I don't want to have to set that up before knowing if the type check passed.
Third is that Error::TypeTiny::Assertion->new will override any passed in attributes with values from $Method::Generate::Accessor::CurrentAttribute if it is set. In MooX::TypeTiny I have to localize $CurrentAttribute, otherwise E::TT::Assertion could pick up information if it was nested in a non-TT attribute validation.
Ideally, I would like to be able to have code something like this:
my ($type, $value, $attr_name, $var_name) = ...;
my $type_var = '$isa_check_for_'.sanitize_identifier($attr_name);
$captures->{$type_var} = \$type;
$check->inline_attribute_check($value, $type_var, $attr_name, $var_name, 'isa check');
I'm can work on the implementation of this if we can agree on a suitable API.