Skip to content

Commit

Permalink
Return empty purl fields in purl_to_lookups
Browse files Browse the repository at this point in the history
    * Add new test

Signed-off-by: Jono Yang <jyang@nexb.com>
  • Loading branch information
JonoYang committed Apr 24, 2023
1 parent d8a3235 commit d62d3c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/packageurl/contrib/django/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from packageurl import PackageURL


def purl_to_lookups(purl_str, encode=True):
def purl_to_lookups(purl_str, encode=True, with_empty_values=False, empty=None):
"""
Return a lookups dict built from the provided `purl` string.
Those lookups can be used as QuerySet filters.
Expand All @@ -41,8 +41,12 @@ def purl_to_lookups(purl_str, encode=True):
except ValueError:
return # Not a valid PackageURL

package_url_dict = package_url.to_dict(encode=encode)
return without_empty_values(package_url_dict)
package_url_dict = package_url.to_dict(encode=encode, empty=empty)

if with_empty_values:
return package_url_dict
else:
return without_empty_values(package_url_dict)


def without_empty_values(input_dict):
Expand Down
24 changes: 24 additions & 0 deletions tests/contrib/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ def test_purl_to_lookups_with_encode():
"version": "0",
"qualifiers": "arch=aarch64&distroversion=edge&reponame=main",
}


def test_purl_to_lookups_with_empty_values():
assert purl_to_lookups(purl_str="pkg:alpine/openssl", encode=True, with_empty_values=True) == {
"type": "alpine",
"namespace": None,
"name": "openssl",
"version": None,
"qualifiers": None,
"subpath": None,
}


def test_purl_to_lookups_with_empty_values_replaced():
assert purl_to_lookups(
purl_str="pkg:alpine/openssl", encode=True, with_empty_values=True, empty=""
) == {
"type": "alpine",
"namespace": "",
"name": "openssl",
"version": "",
"qualifiers": "",
"subpath": "",
}

0 comments on commit d62d3c1

Please sign in to comment.