Skip to content

Commit

Permalink
changed separator mixed with mixed-dot
Browse files Browse the repository at this point in the history
  • Loading branch information
remigermain committed Oct 27, 2021
1 parent 51b6a3f commit 38c7272
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ Attributes where sub keys are other than full numbers are converted into Python
}
# with "mixed" separator option:
data = {
'the[0].chained.key[0].are.awesome[0][0]': 'im here !!'
'the[0]chained.key[0]are.awesome[0][0]': 'im here !!'
}
# with "mixed-dot" separator option (same as 'mixed' but without dot after list to object):
# with "mixed-dot" separator option (same as 'mixed' but with dot after list to object):
data = {
'the[0]chained.key[0]are.awesome[0][0]': 'im here !!'
'the[0].chained.key[0].are.awesome[0][0]': 'im here !!'
}
```

Expand All @@ -158,7 +158,7 @@ For this to work perfectly, you must follow the following rules:

- Each sub key need to be separate by brackets `[ ]` or dot `.` (depends of your options)

- For `mixed` options, brackets `[]` is for list, and dot `.` is for object
- For `mixed` or `mixed-dot` options, brackets `[]` is for list, and dot `.` is for object

- Don't put spaces between separators.

Expand All @@ -173,8 +173,8 @@ For this to work perfectly, you must follow the following rules:
# Separators:
# with bracket: article[0][title][authors][0]: "jhon doe"
# with dot: article.0.title.authors.0: "jhon doe"
# with mixed: article[0].title.authors[0]: "jhon doe"
# with mixed-dot: article[0]title.authors[0]: "jhon doe"
# with mixed: article[0]title.authors[0]: "jhon doe"
# with mixed-dot: article[0].title.authors[0]: "jhon doe"
'separator': 'bracket' or 'dot' or 'mixed' or 'mixed-dot', # default is bracket


Expand Down
11 changes: 5 additions & 6 deletions nested_multipart_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def _merge_options(self, options):
self.__is_mixed = True
elif self._options["separator"] == "mixed-dot":
self.__is_mixed_dot = True
self.__is_mixed = True
else:
self.__is_bracket = True
self._reg = re.compile(r"\[|\]")
Expand Down Expand Up @@ -76,13 +75,13 @@ def span(key, i):
elif key[i] == ']':
raise ValueError(
f"invalid format key '{full_keys}', not start with bracket at position {i + pos}")
elif (key[i] == '.' and not self.__is_mixed_dot) or (
self.__is_mixed_dot and (
elif (key[i] == '.' and self.__is_mixed_dot) or (
not self.__is_mixed_dot and (
(key[i] != '.' and last_is_list) or
(key[i] == '.' and not last_is_list)
)
):
if not self.__is_mixed_dot or not last_is_list:
if self.__is_mixed_dot or not last_is_list:
i += 1
idx = span(key, i)
keys.append(key[i: idx])
Expand All @@ -103,7 +102,7 @@ def split_key(self, key):
# reduce + filter are a hight cost so do manualy with for loop

# optimize by split with string func
if self.__is_mixed:
if self.__is_mixed or self.__is_mixed_dot:
return self.mixed_split(key)
if self.__is_dot:
length = 1
Expand Down Expand Up @@ -146,7 +145,7 @@ def set_type(self, dtc, key, value, full_keys, prev=None, last=False):
return key

def get_next_type(self, key):
if self.__is_mixed:
if self.__is_mixed or self.__is_mixed_dot:
return [] if isinstance(key, int) else {}
return [] if key.isdigit() else {}

Expand Down
38 changes: 19 additions & 19 deletions tests/test_mixed_dot_separator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import TestCase


class TestSettingsSeparatorMixed(TestCase):
class TestSettingsSeparatorMixedDot(TestCase):

def test_assign_duplicate_list(self):
data = {
Expand Down Expand Up @@ -32,8 +32,8 @@ def test_assign_duplicate_number_after_list(self):

def test_assign_nested_duplicate_number_after_list(self):
data = {
"title[0]sub[0]": 101,
"title[0]sub": 42,
"title[0].sub[0]": 101,
"title[0].sub": 42,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed-dot"})
Expand All @@ -49,8 +49,8 @@ def test_assign_nested_duplicate_number_after_list(self):

def test_assign_nested_duplicate_number_after_list2(self):
data = {
"title[0]sub": 42,
"title[0]sub[0]": 101,
"title[0].sub": 42,
"title[0].sub[0]": 101,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed-dot"})
Expand All @@ -66,8 +66,8 @@ def test_assign_nested_duplicate_number_after_list2(self):

def test_assign_nested_duplicate_number_after_dict(self):
data = {
"title[0]sub": 42,
"title[0]sub.title": 101,
"title[0].sub": 42,
"title[0].sub.title": 101,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed-dot"})
Expand All @@ -85,8 +85,8 @@ def test_assign_nested_duplicate_number_after_dict(self):

def test_assign_nested_duplicate_number_after_dict2(self):
data = {
"title[0]sub.title": 101,
"title[0]sub": 42,
"title[0].sub.title": 101,
"title[0].sub": 42,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed-dot"})
Expand Down Expand Up @@ -149,14 +149,14 @@ def test_real(self):
data = {
'title': 'title',
'date': "time",
'langs[0]id': "id",
'langs[0]title': 'title',
'langs[0]description': 'description',
'langs[0]language': "language",
'langs[1]id': "id1",
'langs[1]title': 'title1',
'langs[1]description': 'description1',
'langs[1]language': "language1"
'langs[0].id': "id",
'langs[0].title': 'title',
'langs[0].description': 'description',
'langs[0].language': "language",
'langs[1].id': "id1",
'langs[1].title': 'title1',
'langs[1].description': 'description1',
'langs[1].language': "language1"
}
parser = NestedParser(data, {"separator": "mixed-dot"})
self.assertTrue(parser.is_valid())
Expand Down Expand Up @@ -247,15 +247,15 @@ def test_mixed_invalid_object2(self):
def test_mixed_invalid_object3(self):
data = {
'title': 'lalal',
'article[0].op': 'lalal',
'article.op..': 'lalal',
}
parser = NestedParser(data, {"separator": "mixed-dot"})
self.assertFalse(parser.is_valid())

def test_mixed_invalid_object4(self):
data = {
'title': 'lalal',
'article.op..': 'lalal',
'article[0]op': 'lalal',
}
parser = NestedParser(data, {"separator": "mixed-dot"})
self.assertFalse(parser.is_valid())
Expand Down
40 changes: 20 additions & 20 deletions tests/test_mixed_separator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_assign_duplicate_list(self):
"title[0]": 101
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed", "separator": "mixed"})
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed"})
self.assertTrue(p.is_valid())
expected = {
"title": [101]
Expand All @@ -23,7 +23,7 @@ def test_assign_duplicate_number_after_list(self):
"title": 42,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed", "separator": "mixed"})
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed"})
self.assertTrue(p.is_valid())
expected = {
"title": 42
Expand All @@ -32,8 +32,8 @@ def test_assign_duplicate_number_after_list(self):

def test_assign_nested_duplicate_number_after_list(self):
data = {
"title[0].sub[0]": 101,
"title[0].sub": 42,
"title[0]sub[0]": 101,
"title[0]sub": 42,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed"})
Expand All @@ -49,8 +49,8 @@ def test_assign_nested_duplicate_number_after_list(self):

def test_assign_nested_duplicate_number_after_list2(self):
data = {
"title[0].sub": 42,
"title[0].sub[0]": 101,
"title[0]sub": 42,
"title[0]sub[0]": 101,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed"})
Expand All @@ -66,8 +66,8 @@ def test_assign_nested_duplicate_number_after_list2(self):

def test_assign_nested_duplicate_number_after_dict(self):
data = {
"title[0].sub": 42,
"title[0].sub.title": 101,
"title[0]sub": 42,
"title[0]sub.title": 101,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed"})
Expand All @@ -85,8 +85,8 @@ def test_assign_nested_duplicate_number_after_dict(self):

def test_assign_nested_duplicate_number_after_dict2(self):
data = {
"title[0].sub.title": 101,
"title[0].sub": 42,
"title[0]sub.title": 101,
"title[0]sub": 42,
}
p = NestedParser(
data, {"raise_duplicate": False, "assign_duplicate": True, "separator": "mixed"})
Expand Down Expand Up @@ -149,14 +149,14 @@ def test_real(self):
data = {
'title': 'title',
'date': "time",
'langs[0].id': "id",
'langs[0].title': 'title',
'langs[0].description': 'description',
'langs[0].language': "language",
'langs[1].id': "id1",
'langs[1].title': 'title1',
'langs[1].description': 'description1',
'langs[1].language': "language1"
'langs[0]id': "id",
'langs[0]title': 'title',
'langs[0]description': 'description',
'langs[0]language': "language",
'langs[1]id': "id1",
'langs[1]title': 'title1',
'langs[1]description': 'description1',
'langs[1]language': "language1"
}
parser = NestedParser(data, {"separator": "mixed"})
self.assertTrue(parser.is_valid())
Expand Down Expand Up @@ -247,15 +247,15 @@ def test_mixed_invalid_object2(self):
def test_mixed_invalid_object3(self):
data = {
'title': 'lalal',
'article.op..': 'lalal',
'article[0].op': 'lalal',
}
parser = NestedParser(data, {"separator": "mixed"})
self.assertFalse(parser.is_valid())

def test_mixed_invalid_object4(self):
data = {
'title': 'lalal',
'article[0]op': 'lalal',
'article.op..': 'lalal',
}
parser = NestedParser(data, {"separator": "mixed"})
self.assertFalse(parser.is_valid())
Expand Down

0 comments on commit 38c7272

Please sign in to comment.