|
1 |
# authentic2 - versatile identity manager
|
|
2 |
# Copyright (C) 2022 Entr'ouvert
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU Affero General Public License as published
|
|
6 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU Affero General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU Affero General Public License
|
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
|
|
17 |
from rest_framework.parsers import JSONParser
|
|
18 |
|
|
19 |
FLATTEN_SEPARATOR = '/'
|
|
20 |
|
|
21 |
|
|
22 |
def _is_number(string):
|
|
23 |
if hasattr(string, 'isdecimal'):
|
|
24 |
return string.isdecimal() and [ord(c) < 256 for c in string]
|
|
25 |
else: # str PY2
|
|
26 |
return string.isdigit()
|
|
27 |
|
|
28 |
|
|
29 |
def _unflatten(d, separator=FLATTEN_SEPARATOR):
|
|
30 |
"""Transform:
|
|
31 |
|
|
32 |
{"a/b/0/x": "1234"}
|
|
33 |
|
|
34 |
into:
|
|
35 |
|
|
36 |
{"a": {"b": [{"x": "1234"}]}}
|
|
37 |
"""
|
|
38 |
if not isinstance(d, dict) or not d: # unflattening an empty dict has no sense
|
|
39 |
return d
|
|
40 |
|
|
41 |
# ok d is a dict
|
|
42 |
|
|
43 |
def map_digits(parts):
|
|
44 |
return [int(x) if _is_number(x) else x for x in parts]
|
|
45 |
|
|
46 |
keys = [(map_digits(key.split(separator)), key) for key in d]
|
|
47 |
keys.sort()
|
|
48 |
|
|
49 |
def set_path(path, orig_key, d, value, i=0):
|
|
50 |
assert path
|
|
51 |
|
|
52 |
key, tail = path[i], path[i + 1 :]
|
|
53 |
|
|
54 |
if not tail: # end of path, set thevalue
|
|
55 |
if isinstance(key, int):
|
|
56 |
assert isinstance(d, list)
|
|
57 |
if len(d) != key:
|
|
58 |
raise ValueError('incomplete array before %s' % orig_key)
|
|
59 |
d.append(value)
|
|
60 |
else:
|
|
61 |
assert isinstance(d, dict)
|
|
62 |
d[key] = value
|
|
63 |
else:
|
|
64 |
new = [] if isinstance(tail[0], int) else {}
|
|
65 |
|
|
66 |
if isinstance(key, int):
|
|
67 |
assert isinstance(d, list)
|
|
68 |
if len(d) < key:
|
|
69 |
raise ValueError(
|
|
70 |
'incomplete array before %s in %s'
|
|
71 |
% (separator.join(map(str, path[: i + 1])), orig_key)
|
|
72 |
)
|
|
73 |
if len(d) == key:
|
|
74 |
d.append(new)
|
|
75 |
else:
|
|
76 |
new = d[key]
|
|
77 |
else:
|
|
78 |
new = d.setdefault(key, new)
|
|
79 |
set_path(path, orig_key, new, value, i + 1)
|
|
80 |
|
|
81 |
# Is the first level an array or a dict ?
|
|
82 |
if isinstance(keys[0][0][0], int):
|
|
83 |
new = []
|
|
84 |
else:
|
|
85 |
new = {}
|
|
86 |
for path, key in keys:
|
|
87 |
value = d[key]
|
|
88 |
set_path(path, key, new, value)
|
|
89 |
return new
|
|
90 |
|
|
91 |
|
|
92 |
class UnflattenJSONParser(JSONParser):
|
|
93 |
def parse(self, *args, **kwargs):
|
|
94 |
result = super().parse(*args, **kwargs)
|
|
95 |
if isinstance(result, dict) and any('/' in key for key in result):
|
|
96 |
result = _unflatten(result)
|
|
97 |
return result
|