Skip to content

Commit

Permalink
Raise an error on too long string attributes (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxtkr77 authored Jul 17, 2024
1 parent de6b3ce commit 07ff984
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 57 deletions.
11 changes: 9 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import v3io.dataplane.output
import v3io.dataplane.response
import v3io.logger
from v3io.dataplane.kv_large_string import LARGE_STRING_MIN_SIZE


class Test(unittest.TestCase):
Expand Down Expand Up @@ -432,7 +431,6 @@ def _get_float_array():
"array_with_ints": _get_int_array(),
"array_with_floats": _get_float_array(),
"now": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"large_string": "a" * 10 * LARGE_STRING_MIN_SIZE,
}
}

Expand All @@ -448,6 +446,15 @@ def _get_float_array():
for key in item[item_key]:
self._compare_item_types(item[item_key][key], response.output.item[key])

item = {item_key: {"large_string": "a" * 61200}}
try:
self._client.kv.put(
container=self._container, table_path=self._path, key=item_key, attributes=item[item_key]
)
self.fail("Large string should have raised an exception")
except AttributeError:
pass

def test_kv(self):
items = {
"bob": {"age": 42, "feature": "mustache"},
Expand Down
34 changes: 0 additions & 34 deletions v3io/dataplane/kv_large_string.py

This file was deleted.

15 changes: 6 additions & 9 deletions v3io/dataplane/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import v3io.dataplane.kv_array
import v3io.dataplane.kv_timestamp
from v3io.dataplane.kv_large_string import is_large_bstring, large_bstring_to_string


class Output(object):
Expand All @@ -34,14 +33,12 @@ def _decode_typed_attributes(self, typed_attributes):
decoded_attribute = float(attribute_value)
elif attribute_type == "B":
decoded_attribute = base64.b64decode(attribute_value)
if is_large_bstring(decoded_attribute):
decoded_attribute = large_bstring_to_string(decoded_attribute)
else:
# try to decode as an array
try:
decoded_attribute = v3io.dataplane.kv_array.decode(decoded_attribute)
except BaseException:
pass

# try to decode as an array
try:
decoded_attribute = v3io.dataplane.kv_array.decode(decoded_attribute)
except BaseException:
pass

elif attribute_type == "S":
if type(attribute_value) in [float, int]:
Expand Down
30 changes: 18 additions & 12 deletions v3io/dataplane/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
import v3io.common.helpers
import v3io.dataplane.kv_array
import v3io.dataplane.kv_timestamp
from v3io.dataplane.kv_large_string import (
LARGE_STRING_MIN_SIZE,
string_to_large_bstring,
)

#
# Request
Expand Down Expand Up @@ -418,19 +414,29 @@ def _to_base64(input):

def _dict_to_typed_attributes(d):
typed_attributes = {}

max_string_length = 61199
for key, value in future.utils.viewitems(d):
attribute_type = type(value)
type_value = None

if isinstance(value, future.utils.text_type) or isinstance(value, future.utils.string_types):
if isinstance(value, future.utils.text_type):
type_key = "S"
type_value = value
if len(value) > max_string_length:
raise AttributeError(
"Attribute {0} is too long({1} bytes) when max is {2} bytes".format(
key, len(value), max_string_length
)
)
elif isinstance(value, future.utils.string_types):
type_key = "S"
type_value = str(value)
if len(value) > LARGE_STRING_MIN_SIZE:
type_key = "B"
type_value = string_to_large_bstring(type_value)
type_value = base64.b64encode(type_value)
else:
type_key = "S"
if len(type_value) > max_string_length:
raise AttributeError(
"Attribute {0} is too long({1} bytes) when max is {2} bytes".format(
key, len(value), max_string_length
)
)
elif attribute_type in [int, float]:
type_key = "N"
type_value = str(value)
Expand Down

0 comments on commit 07ff984

Please sign in to comment.