Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return empty purl fields in purl_to_lookups #117

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "",
}