Skip to content

Commit

Permalink
Fix corner case of stringify-only overloaded values
Browse files Browse the repository at this point in the history
Just a trivial cleanup, uncovered during the de-Path::Class work
  • Loading branch information
ribasushi committed Aug 12, 2015
1 parent 8336115 commit 096ab90
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Revision history for DBIx::Class
->req_list_for([qw( rdbms_oracle ic_dt )]) bringing in the Oracle
specific DateTime::Format dependencies

* Fixes
- Fix corner case of stringify-only overloaded objects being used in
create()/populate()

* Misc
- Skip tests in a way more intelligent and speedy manner when optional
dependencies are missing
Expand Down
10 changes: 8 additions & 2 deletions lib/DBIx/Class/Row.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Scalar::Util 'blessed';
use List::Util 'first';
use Try::Tiny;
use DBIx::Class::Carp;
use SQL::Abstract 'is_literal_value';
use SQL::Abstract qw( is_literal_value is_plain_value );

###
### Internal method
Expand Down Expand Up @@ -1215,7 +1215,13 @@ sub store_column {
unless exists $self->{_column_data}{$column} || $self->result_source->has_column($column);
$self->throw_exception( "set_column called for ${column} without value" )
if @_ < 3;
return $self->{_column_data}{$column} = $value;

# stringify all refs explicitly, guards against overloaded objects
# with defined stringification AND fallback => 0 (ugh!)
$self->{_column_data}{$column} = ( length ref $value and is_plain_value( $value ) )
? "$value"
: $value
;
}

=head2 inflate_result
Expand Down
32 changes: 25 additions & 7 deletions t/100populate.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ use Test::Warn;
use lib qw(t/lib);
use DBICTest;
use DBIx::Class::_Util qw(sigwarn_silencer serialize);
use Path::Class::File ();
use Math::BigInt;
use List::Util qw/shuffle/;

{
package DBICTest::StringifiesOnly;
use overload
'""' => sub { $_[0]->[0] },
fallback => 0,
;
}
{
package DBICTest::StringifiesViaFallback;
use overload
'bool' => sub { $_[0]->[0] },
;
}

my $schema = DBICTest->init_schema();

# The map below generates stuff like:
Expand Down Expand Up @@ -388,8 +401,8 @@ warnings_like {
# the stringification has nothing to do with the artist name
# this is solely for testing consistency
my $fn = Path::Class::File->new ('somedir/somefilename.tmp');
my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp');
my $fn = bless [ 'somedir/somefilename.tmp' ], 'DBICTest::StringifiesOnly';
my $fn2 = bless [ 'somedir/someotherfilename.tmp' ], 'DBICTest::StringifiesViaFallback';
my $rank = Math::BigInt->new(42);
my $args = {
Expand Down Expand Up @@ -443,12 +456,17 @@ warnings_like {
};
# generate the AoH equivalent based on the AoAs above
# also generate the expected HRI output ( is_deeply is too smart for its own good )
for my $bag (values %$args) {
$bag->{AoH} = [];
$bag->{Expected} = [];
my @hdr = @{$bag->{AoA}[0]};
for my $v ( @{$bag->{AoA}}[1..$#{$bag->{AoA}}] ) {
push @{$bag->{AoH}}, my $h = {};
@{$h}{@hdr} = @$v;
push @{$bag->{Expected}}, my $hs = {};
@{$hs}{@hdr} = map { "$_" } @$v;
}
}
Expand All @@ -464,7 +482,7 @@ warnings_like {
$rs->populate($args->{$tst}{$type});
is_deeply(
$rs->all_hri,
$args->{$tst}{AoH},
$args->{$tst}{Expected},
"Populate() $tst in void context"
);
Expand All @@ -473,7 +491,7 @@ warnings_like {
my $dummy = $rs->populate($args->{$tst}{$type});
is_deeply(
$rs->all_hri,
$args->{$tst}{AoH},
$args->{$tst}{Expected},
"Populate() $tst in non-void context"
);
Expand All @@ -482,7 +500,7 @@ warnings_like {
my @dummy = $rs->populate($args->{$tst}{$type});
is_deeply(
$rs->all_hri,
$args->{$tst}{AoH},
$args->{$tst}{Expected},
"Populate() $tst in non-void context"
);
}
Expand All @@ -493,7 +511,7 @@ warnings_like {
is_deeply(
$rs->all_hri,
$args->{$tst}{AoH},
$args->{$tst}{Expected},
"Create() $tst"
);
}
Expand Down
2 changes: 1 addition & 1 deletion t/lib/DBICTest/Schema/Artist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ sub sqlt_deploy_hook {

sub store_column {
my ($self, $name, $value) = @_;
$value = 'X '.$value if ($name eq 'name' && $value && $value =~ /(X )?store_column test/);
$value = 'X '.$value if ($name eq 'name' && defined $value && $value =~ /(X )?store_column test/);
$self->next::method($name, $value);
}

Expand Down

0 comments on commit 096ab90

Please sign in to comment.