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 c7f4b46
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
17 changes: 13 additions & 4 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,15 @@ 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 All @@ -54,4 +61,6 @@ def without_empty_values(input_dict):
"""
empty_values = ([], (), {}, "", None)

return {key: value for key, value in input_dict.items() if value not in empty_values}
return {
key: value for key, value in input_dict.items() if value not in empty_values
}
31 changes: 31 additions & 0 deletions tests/contrib/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,34 @@ 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 c7f4b46

Please sign in to comment.