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

Enable packages metadata to be float when int is expected (gh#uyuni-project/uyuni#9613) #9726

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions python/spacewalk/server/importlib/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3360,16 +3360,21 @@ def _buildDatabaseValue(row, fieldsHash):
def _buildExternalValue(dict, entry, tableObj):
# updates dict with values from entry
# entry is a hash-like object (non-db)
for f, datatype in list(tableObj.getFields().items()):
if f in dict:
for field, datatype in list(tableObj.getFields().items()):
if field in dict:
# initialized somewhere else
continue
# Get the attribute's name
attr = tableObj.getObjectAttribute(f)
attr = tableObj.getObjectAttribute(field)
# Sanitize the value according to its datatype
if attr not in entry:
entry[attr] = None
dict[f] = sanitizeValue(entry[attr], datatype)
try:
dict[field] = sanitizeValue(entry[attr], datatype)
except ValueError as e:
raise ValueError(
f"Cannot sanitize value from {field}={entry[attr]} to {type(datatype)}"
) from e


# pylint: disable-next=invalid-name
Expand Down
27 changes: 17 additions & 10 deletions python/spacewalk/server/importlib/backendLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,7 @@ def query(self, values):

def sanitizeValue(value, datatype):
if isinstance(datatype, DBstring):
if value is None or value == "":
return None # we really want to preserve Nones
# and not depend on Oracle converting
# empty strings to NULLs -- PostgreSQL
# does not do this
if len(value) > datatype.limit:
value = value[: datatype.limit]
# ignore incomplete characters created after truncating
return value
return _sanitize_dbstring(value, datatype)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this into a separate function so that sonarcloud won't fail with method complexity being too high.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we use classes, but then don't do this with the classes. Would be too big to change that here, but it's something we could think about changing.

if isinstance(datatype, DBblob):
if value is None:
value = ""
Expand All @@ -518,7 +510,22 @@ def sanitizeValue(value, datatype):
if isinstance(datatype, DBdate):
return str(value)[:10]
if isinstance(datatype, DBint):
return int(value)
try:
return int(float(value))
except ValueError as e:
raise ValueError(f"Cannot convert {value} to int") from e
return value


def _sanitize_dbstring(value, datatype):
if value is None or value == "":
return None # we really want to preserve Nones
# and not depend on Oracle converting
# empty strings to NULLs -- PostgreSQL
# does not do this
if len(value) > datatype.limit:
value = value[: datatype.limit]
# ignore incomplete characters created after truncating
return value


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Cast float pkg metadata to int (gh#uyuni-project/uyuni#9613)