DataStore-CAS-FS
view release on metacpan or search on metacpan
lib/DataStore/CAS/FS/DirCodec/Universal.pm view on Meta::CPAN
bytes of these filenames could be encoded as unicode code-points, this would
create an ambiguity with the names that actually were Unicode. Instead, I
wrap values which are intended to be a string of octets in an instance of
L<DataStore::CAS::Dir::InvalidUTF8>, which gets written into JSON as
C<{ "*InvalidUTF8*": $bytes_as_codepoints }>
Any attribute which contains bytes >= 0x80 and which does not have Perl's
unicode flag set will be encoded this way, so that it comes back as it went in.
However, since filenames are intended to be human-readable, they are decoded as
unicode strings when appropriate, even if they arrived as octets which just
happened to be valid UTF-8.
=head1 METHODS
=head2 encode
my $serialized= $class->encode( \@entries, \%metadata )
Serialize the given entries into a scalar.
lib/DataStore/CAS/FS/InvalidUTF8.pm view on Meta::CPAN
InvalidUTF8 can also safely pass through JSON, if the filter is added to the
JSON decoder, and "allow_blessed" is set on the encoder.
=head1 METHODS
=head2 decode_utf8
$string_or_ref= $class->decode_utf8( $byte_str )
If the $byte_str is valid UTF-8, this method returns the decoded perl unicode
string. If not, it returns the string wrapped in an instance of InvalidUTF8.
=head2 is_non_unicode
This method returns true, and can be used in tests like
if ($_->can('is_non_unicode')) { ... }
as a way of detecting InvalidUTF8 objects by API rather than class hierarchy.
t/32-dircodec-universal.t view on Meta::CPAN
sub dir_encode {
my ($entries, $meta)= @_;
$meta ||= {};
return DataStore::CAS::FS::DirCodec::Universal->encode($entries, $meta);
}
subtest empty_dir => sub {
my $hash= DataStore::CAS::FS::DirCodec->put($cas, 'universal', [], {});
my $file= $cas->get($hash);
is( $file->data, qq|CAS_Dir 09 universal\n{"metadata":{},\n "entries":[\n]}|, 'encode' );
isa_ok( my $decoded= DataStore::CAS::FS::DirCodec->load($file), 'DataStore::CAS::FS::Dir', 'decode' );
is( $decoded->iterator->(), undef, 'zero entries' );
done_testing;
};
subtest one_dirent => sub {
my @entries= (
{ type => 'file', name => 'test' }
);
my $hash= DataStore::CAS::FS::DirCodec->put($cas, 'universal', \@entries, {});
my $file= $cas->get($hash);
t/33-dircodec-minimal.t view on Meta::CPAN
$meta ||= {};
return DataStore::CAS::FS::DirCodec::Minimal->encode($entries, $meta);
}
my $cas= DataStore::CAS::Virtual->new();
subtest empty_dir => sub {
my $hash= DataStore::CAS::FS::DirCodec->put($cas, 'minimal', [], {});
my $file= $cas->get($hash);
is( $file->data, qq|CAS_Dir 00 \n\0|, 'encode' );
isa_ok( my $decoded= DataStore::CAS::FS::DirCodec->load($file), 'DataStore::CAS::FS::Dir', 'decode' );
is( $decoded->iterator->(), undef, 'zero entries' );
done_testing;
};
subtest one_dirent => sub {
my @entries= (
{ type => 'file', name => 'test', ref => undef }
);
my @expected= (
{ type => 'file', name => 'test' }
t/34-dircodec-unix.t view on Meta::CPAN
$meta ||= {};
return DataStore::CAS::FS::DirCodec::Unix->encode($entries, $meta);
}
my $cas= DataStore::CAS::Virtual->new();
subtest empty_dir => sub {
my $hash= DataStore::CAS::FS::DirCodec->put($cas, 'unix', [], {});
my $file= $cas->get($hash);
#is( $file->data, qq|CAS_Dir 04 unix\n\0|, 'encode' );
isa_ok( my $decoded= DataStore::CAS::FS::DirCodec->load($file), 'DataStore::CAS::FS::Dir', 'decode' );
is( $decoded->iterator->(), undef, 'zero entries' );
done_testing;
};
subtest one_dirent => sub {
my @entries= (
{ type => 'file', name => 'test', ref => undef }
);
my @expected= (
{ type => 'file', name => 'test' }
( run in 0.362 second using v1.01-cache-2.11-cpan-26ccb49234f )