On 13/01/16 18:03, Peter Rabbitson via RT wrote:
Show quoted text> <URL:
https://rt.cpan.org/Ticket/Display.html?id=111159 >
>
> On 01/13/2016 06:49 PM, Robert Rothenberg via RT wrote:
>> Wed Jan 13 12:49:17 2016: Request 111159 was acted upon.
>> Transaction: Ticket created by RRWO
>> Queue: DBIx-Class
>> Subject: [WISHLIST] Use Type::Tiny types for specifying columns
>> Broken in: (no value)
>> Severity: (no value)
>> Owner: Nobody
>> Requestors: rrwo@cpan.org
>> Status: new
>> Ticket <URL:
https://rt.cpan.org/Ticket/Display.html?id=111159 >
>>
>>
>> It would be nice to be able to do something like
>>
>> __PACKAGE__->add_column( foo => Maybe[ Varchar[16] ] );
>>
>> or
>>
>> __PACKAGE__->add_column(
>> foo => {
>> isa => Maybe[ Varchar[16] ],
>> extra => { ... }
>> }
>> );
>>
>> This is actually not hard to do. I have some (prototype) code in the
>> Types::SQL distribution that does this.
>>
>> It makes the code easier to read, especially for developers who are already
>> using Moo/Moose for other parts of the code. (And object attributes that
>> depend on database columns can be given the same types.)
>>
>
> I have not looked at Types::SQL, so what follows is just a general
> observation.
>
> To stop the tendency of DBIC itself is becoming overly-monolithic, the
> general direction of such enhancements these days is to push them out of
> the core distrubution, and have them as standalone extra-sugar, akin to
> DBIx::Class::Candy. Is there a reason this can not be done in the same
> manner for the above (or even as a subset of ::Candy-like syntax)?
Fair point. I've also been in touch w/author of DBIC::Candy as well about
the idea.
Show quoted text> Note - I have not thought through the entire proposal as there is little
> code to go with it (and Types::SQL does things "in the other direction"
> so it's hard to reason about it from there). So please elaborate more /
> point out what am I missing.
Well, given a type like Maybe[Varchar[16]] we can extract the info for the
column definition:
data_type => 'varchar',
is_nullable => 1,
size => 16
The code in Types::SQL::Util
https://github.com/robrwo/Types-SQL/blob/master/lib/Types/SQL/Util.pm
has a function to generate the column information from Type::Tiny objects
(not just Types::SQL but also Str, Enum, Int).
Something like
use Types::SQL::Util;
...
# Where $info is the column_info
if ((blessed $info) && $info->isa('Type::Tiny')) {
$info = { isa => $info };
}
if (my $type = $info->{isa}) {
my %t_info = column_info_from_type( $type );
$info->{$_} //= $t_info{$_} for keys %t_info;
}