Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
- Fixed "namespace vs name" (RT#143917)
- Fixed substitution warning when "version" component is not provided in URI::PackageURL->from_string
  • Loading branch information
giterlizzi committed Aug 1, 2022
1 parent 12446e2 commit 76a18ff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
9 changes: 7 additions & 2 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Revision history for Perl extension URI::PackageURL.

1.10 2022-08-01
- Fixed "namespace vs name" (RT#143917)
- Fixed substitution warning when "version" component is not provided
in URI::PackageURL->from_string

1.02 2022-07-31
- Fix decode when "namespace" component is undef
- Fixed decode when "namespace" component is "undef"

1.01 2022-07-26
- Fix documentation and test prerequisite (JSON::PP)
- Fixed documentation and test prerequisite (JSON::PP)

1.00 2022-07-25
- First release of URI::PackageURL
34 changes: 13 additions & 21 deletions lib/URI/PackageURL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use constant DEBUG => $ENV{PURL_DEBUG};

use overload '""' => 'to_string', fallback => 1;

our $VERSION = '1.02';
our $VERSION = '1.10';

our @EXPORT = qw(encode_purl decode_purl);

Expand Down Expand Up @@ -56,19 +56,12 @@ sub normalize_components {

$components{type} = lc $components{type};

if ($components{type} eq 'pypi') {
$components{name} =~ s/_/-/g;
if ($components{type} eq 'cpan') {
$components{name} =~ s/-/::/g;
}

# CPAN: Split Perl "Namespace::Package" naming into "namespace" and "name" components
if ($components{type} eq 'cpan' && $components{name} =~ /::/) {

my @ns = split /::/, $components{name};
my $name = pop @ns;

$components{name} = $name;
$components{namespace} = join '::', @ns;

if ($components{type} eq 'pypi') {
$components{name} =~ s/_/-/g;
}

if (grep { $_ eq $components{type} } qw(bitbucket deb github golang hex npm pypi)) {
Expand Down Expand Up @@ -203,7 +196,7 @@ sub from_string {
# This is the version

my @s5 = split('@', $s4[1]);
$components{version} = url_decode($s5[1]);
$components{version} = url_decode($s5[1]) if ($s5[1]);


# Split the remainder once from right on '/'
Expand All @@ -214,7 +207,7 @@ sub from_string {
# This is the name

my @s6 = split('/', $s5[0], 2);
$components{name} = (scalar @s6 > 1) ? url_decode($s6[1]) : url_decode($s6[0]);
$components{name} = (scalar @s6 > 1) ? url_decode($s6[1]) : url_decode($s6[0]);


# Split the remainder on '/'
Expand All @@ -230,7 +223,6 @@ sub from_string {
$components{namespace} = join '/', map { url_decode($_) } @s7;
}


return $class->new(%components);

}
Expand Down Expand Up @@ -313,19 +305,19 @@ URI::PackageURL - Perl extension for Package URL (aka "purl")
# OO-interface
# Encode components in PackageURL string
$purl = URI::PackageURL->new(type => cpan, namespace => 'URI', name => 'PackageURL', version => 1.00');
$purl = URI::PackageURL->new(type => cpan, name => 'URI::PackageURL', version => '1.10');
say $purl; # pkg:cpan/URI/PackageURL@0.0.1
say $purl; # pkg:cpan/URI::PackageURL@1.10
# Parse PackageURL string
$purl = URI::PackageURL->from_string('pkg:cpan/URI/PackageURL@0.0.1');
$purl = URI::PackageURL->from_string('pkg:cpan/URI::PackageURL@1.10');
# exported funtions
$purl = decode_purl('pkg:cpan/URI/PackageURL@0.0.1');
$purl = decode_purl('pkg:cpan/URI::PackageURL@1.10');
say $purl->type; # cpan
$purl_string = encode_purl(type => cpan, namespace => 'URI', name => 'PackageURL', version => 1.00');
$purl_string = encode_purl(type => cpan, name => 'URI::PackageURL', version => '1.10');
=head1 DESCRIPTION
Expand Down Expand Up @@ -437,7 +429,7 @@ Helper method for JSON modules (L<JSON>, L<JSON::PP>, L<JSON::XS>, L<Mojo::JSON>
use Mojo::JSON qw(encode_json);
say encode_json($purl); # {"name":"PackageURL","namespace":"URI","qualifiers":null,"subpath":null,"type":"cpan","version":"0.0.1"}
say encode_json($purl); # {"name":"URI::PackageURL","namespace":null,"qualifiers":null,"subpath":null,"type":"cpan","version":"1.10"}
=back
Expand Down
4 changes: 2 additions & 2 deletions t/10-encode.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use Test::More;
use URI::PackageURL qw(encode_purl);

is(
encode_purl(type => 'cpan', name => 'URI::PackageURL', version => '1.00'),
'pkg:cpan/URI/PackageURL@1.00',
encode_purl(type => 'cpan', name => 'URI::PackageURL', version => '1.10'),
'pkg:cpan/URI::PackageURL@1.10',
'encode_purl()'
);

Expand Down
9 changes: 4 additions & 5 deletions t/20-decode.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Test::More;

use URI::PackageURL;

my $t1 = 'pkg:cpan/URI/PackageURL@1.00';
my $t1 = 'pkg:cpan/URI::PackageURL@1.10';
my $t2 = 'pkg:deb/debian/curl@7.50.3-1?arch=i386&distro=jessie';
my $t3 = 'pkg:golang/google.golang.org/genproto@abcdedf#googleapis/api/annotations';
my $t4 = 'pkg:docker/customer/dockerimage@sha256:244fd47e07d1004f0aed9c?repository_url=gcr.io';
Expand All @@ -16,10 +16,9 @@ subtest "Decode '$t1'" => sub {

my $purl = decode_purl($t1);

is($purl->type, 'cpan', 'Type');
is($purl->namespace, 'URI', 'Namespace');
is($purl->name, 'PackageURL', 'Name');
is($purl->version, '1.00', 'Version');
is($purl->type, 'cpan', 'Type');
is($purl->name, 'URI::PackageURL', 'Name');
is($purl->version, '1.10', 'Version');

is($purl->to_string, $t1, 'PackageURL');

Expand Down

0 comments on commit 76a18ff

Please sign in to comment.