Skip to content

Commit 07ff984

Browse files
authored
Raise an error on too long string attributes (#123)
1 parent de6b3ce commit 07ff984

File tree

4 files changed

+33
-57
lines changed

4 files changed

+33
-57
lines changed

tests/test_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import v3io.dataplane.output
2828
import v3io.dataplane.response
2929
import v3io.logger
30-
from v3io.dataplane.kv_large_string import LARGE_STRING_MIN_SIZE
3130

3231

3332
class Test(unittest.TestCase):
@@ -432,7 +431,6 @@ def _get_float_array():
432431
"array_with_ints": _get_int_array(),
433432
"array_with_floats": _get_float_array(),
434433
"now": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
435-
"large_string": "a" * 10 * LARGE_STRING_MIN_SIZE,
436434
}
437435
}
438436

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

449+
item = {item_key: {"large_string": "a" * 61200}}
450+
try:
451+
self._client.kv.put(
452+
container=self._container, table_path=self._path, key=item_key, attributes=item[item_key]
453+
)
454+
self.fail("Large string should have raised an exception")
455+
except AttributeError:
456+
pass
457+
451458
def test_kv(self):
452459
items = {
453460
"bob": {"age": 42, "feature": "mustache"},

v3io/dataplane/kv_large_string.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

v3io/dataplane/output.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import v3io.dataplane.kv_array
2020
import v3io.dataplane.kv_timestamp
21-
from v3io.dataplane.kv_large_string import is_large_bstring, large_bstring_to_string
2221

2322

2423
class Output(object):
@@ -34,14 +33,12 @@ def _decode_typed_attributes(self, typed_attributes):
3433
decoded_attribute = float(attribute_value)
3534
elif attribute_type == "B":
3635
decoded_attribute = base64.b64decode(attribute_value)
37-
if is_large_bstring(decoded_attribute):
38-
decoded_attribute = large_bstring_to_string(decoded_attribute)
39-
else:
40-
# try to decode as an array
41-
try:
42-
decoded_attribute = v3io.dataplane.kv_array.decode(decoded_attribute)
43-
except BaseException:
44-
pass
36+
37+
# try to decode as an array
38+
try:
39+
decoded_attribute = v3io.dataplane.kv_array.decode(decoded_attribute)
40+
except BaseException:
41+
pass
4542

4643
elif attribute_type == "S":
4744
if type(attribute_value) in [float, int]:

v3io/dataplane/request.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
import v3io.common.helpers
3030
import v3io.dataplane.kv_array
3131
import v3io.dataplane.kv_timestamp
32-
from v3io.dataplane.kv_large_string import (
33-
LARGE_STRING_MIN_SIZE,
34-
string_to_large_bstring,
35-
)
3632

3733
#
3834
# Request
@@ -418,19 +414,29 @@ def _to_base64(input):
418414

419415
def _dict_to_typed_attributes(d):
420416
typed_attributes = {}
421-
417+
max_string_length = 61199
422418
for key, value in future.utils.viewitems(d):
423419
attribute_type = type(value)
424420
type_value = None
425421

426-
if isinstance(value, future.utils.text_type) or isinstance(value, future.utils.string_types):
422+
if isinstance(value, future.utils.text_type):
423+
type_key = "S"
424+
type_value = value
425+
if len(value) > max_string_length:
426+
raise AttributeError(
427+
"Attribute {0} is too long({1} bytes) when max is {2} bytes".format(
428+
key, len(value), max_string_length
429+
)
430+
)
431+
elif isinstance(value, future.utils.string_types):
432+
type_key = "S"
427433
type_value = str(value)
428-
if len(value) > LARGE_STRING_MIN_SIZE:
429-
type_key = "B"
430-
type_value = string_to_large_bstring(type_value)
431-
type_value = base64.b64encode(type_value)
432-
else:
433-
type_key = "S"
434+
if len(type_value) > max_string_length:
435+
raise AttributeError(
436+
"Attribute {0} is too long({1} bytes) when max is {2} bytes".format(
437+
key, len(value), max_string_length
438+
)
439+
)
434440
elif attribute_type in [int, float]:
435441
type_key = "N"
436442
type_value = str(value)

0 commit comments

Comments
 (0)