Skip to content

Commit

Permalink
Introduce a Blob model field (#789)
Browse files Browse the repository at this point in the history
* Introduce a Blob model field

As models have to be JSON-serializable and byte
fields apparently don't fit in, let's introduce
a special model field type that could be used in
such situations.
Basic Blob serialization tests added.

OAMG-4306

* Bump leapp-framework version

The change adds new functionality, so bumping from
3.0 to 3.1
  • Loading branch information
fernflower authored Aug 19, 2022
1 parent 21ce771 commit ee2f1e8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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

0 comments on commit ee2f1e8

Please sign in to comment.