Skip to content

Commit

Permalink
Support for reads of binary and binary set.
Browse files Browse the repository at this point in the history
  • Loading branch information
BikesAndBBQ committed Feb 22, 2013
1 parent 0ce11e6 commit b569757
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/Net/Amazon/DynamoDB.pm
Expand Up @@ -2404,24 +2404,53 @@ sub _format_item {
my $key_name = $table_ref->{ "${key}_key" };
my $key_type = $table_ref->{ attributes }->{ $key_name };
$formatted{ $key_name } = $from_ref->{ ucfirst( $key ). 'KeyElement' }->{ $key_type };

if ( $key_type eq 'B' ) {
$formatted{ $key_name } = decode_base64( $formatted{ $key_name } );
}
elsif ( $key_name = 'BS' ) {
$formatted{ $key_name } = [ map { decode_base64($_) } @{ $formatted{ $key_name } } ];
}
}
}
else {
if ( $self->derive_table() ) {
while ( my ( $key, $value ) = each %$from_ref ) {
$formatted{$key} = ( $value->{'S'} || $value->{'N'} || $value->{'NS'} || $value->{'SS'} );
}
if ( exists($value->{B}) ) {
$formatted{$key} = decode_base64($value->{B});
}
elsif ( exists($value->{BS}) ) {
$formatted{$key} = [ map { decode_base64($_) } @{ $value->{BS} } ];
}
else {
$formatted{$key} = ( $value->{'S'} || $value->{'N'} || $value->{'NS'} || $value->{'SS'} );
}
}
}
else {
while( my( $attrib, $type ) = each %{ $table_ref->{ attributes } } ) {
next unless defined $from_ref->{ $attrib };
$formatted{ $attrib } = $from_ref->{ $attrib }->{ $type };
if ( $type eq 'BS' ) {
$formatted{ $attrib } = $self->_decode_binary_set( $from_ref->{ $attrib }->{ $type } );
}
elsif ( $type eq 'B' ) {
$formatted{ $attrib } = decode_base64( $from_ref->{ $attrib }->{ $type } );
}
else {
$formatted{ $attrib } = $from_ref->{ $attrib }->{ $type };
}
}
}
}
return \%formatted;
}

sub _decode_binary_set {
my ( $self, $bs_ref ) = shift;

return [ map { decode_base64($_) } @$bs_ref ];
}


#
# _table_name
Expand Down
5 changes: 5 additions & 0 deletions t/01.t
Expand Up @@ -106,6 +106,10 @@ SKIP: {
my $data = pack("C*",map { $_ % 256 } 0..65526);
ok( $ddb->put_item( $table3 => { id => 1, data => $data } ), "Large binary entry in in $table3 created" );

# Get binary back
my $bin_read_ref = $ddb->get_item( $table3 => { id => 1 } );
ok( $bin_read_ref && $bin_read_ref->{ data } eq $data, 'Returned binary data matches' );

# read test
my $read_ref = $ddb->get_item( $table1 => { id => 1 } );
ok( $read_ref && $read_ref->{ id } == 1 && $read_ref->{ name } eq 'First entry', "First entry from $table1 read" );
Expand Down Expand Up @@ -157,6 +161,7 @@ SKIP: {
"Found 4 entries from $table1 and $table2 with batch get"
);


# clean up
foreach my $table( $table1, $table2, $table3 ) {
ok( $ddb->delete_table( $table ), "Table $table delete initialized" );
Expand Down

0 comments on commit b569757

Please sign in to comment.