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

Introduce a Blob model field #789

Merged
merged 2 commits into from
Aug 19, 2022
Merged
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
28 changes: 28 additions & 0 deletions leapp/models/fields/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import copy
import datetime
import json
Expand Down Expand Up @@ -239,6 +240,33 @@ def _model_type(self):
return six.string_types + (six.binary_type,)


class Blob(BuiltinField):
"""
Blob field
"""
@property
def _model_type(self):
# NOTE(ivasilev) Both string_types and binary_types are necessary here to pass value checks on
# serialization/deserialization. Serialized JSON-friendly model will have a string type field (the base64
# ascii string) while deserialized model will have a binary type field.
return six.string_types + (six.binary_type,)

def _convert_to_model(self, value, name):
self._validate_model_value(value=value, name=name)
if value is None:
return None

return base64.b64decode(value)

def _convert_from_model(self, value, name):
self._validate_model_value(value=value, name=name)
if value is None:
return None

# NOTE(ivasilev) b64 encoding is always ascii, thus ascii decoding to get a string
return base64.b64encode(value).decode('ascii')


class DateTime(BuiltinField):
"""
DateTime field to handle datetime objects which are converted to the ISO format and parsed back from there
Expand Down
2 changes: 1 addition & 1 deletion packaging/leapp.spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# This is kind of help for more flexible development of leapp repository,
# so people do not have to wait for new official release of leapp to ensure
# it is installed/used the compatible one.
%global framework_version 3.0
%global framework_version 3.1

# IMPORTANT: everytime the requirements are changed, increment number by one
# - same for Provides in deps subpackage
Expand Down
12 changes: 12 additions & 0 deletions tests/scripts/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class WithNestedListModel(Model):
items = fields.List(fields.Model(BasicModel))


class WithBlobModel(Model):
topic = ModelTestTopic
message = fields.Blob()
can_be_empty = fields.Nullable(fields.Blob())


class AllFieldTypesModel(Model):
topic = ModelTestTopic
float_field = fields.Float(default=3.14)
Expand Down Expand Up @@ -74,6 +80,12 @@ def test_basic_model():
assert m.message == m2.message


def test_bytestring_model():
m = WithBlobModel(message=b'\xf3\xcf\xcf\xc2\xdd\xc5\xce\xc9\xc5')
m2 = WithBlobModel.create(m.dump())
assert m.message == m2.message


def test_string_list_model():
m = WithStringListModel(messages=['Some message'])
m2 = WithStringListModel.create(m.dump())
Expand Down