|
1 | 1 | from .field import Field |
| 2 | +from .field_basic import FieldBasicType |
2 | 3 | from ..types import ( |
3 | 4 | Array, CompactArray, |
4 | 5 | UnsignedVarInt32, Int32, |
|
7 | 8 |
|
8 | 9 | class ApiArray(Field): |
9 | 10 | @classmethod |
10 | | - def parse_json(cls, json): |
| 11 | + def parse_inner_type(cls, json): |
| 12 | + if 'fields' in json: |
| 13 | + return |
| 14 | + type_str = cls.parse_array_type(json) |
| 15 | + if type_str is not None: |
| 16 | + inner_json = {**json, 'type': type_str} |
| 17 | + return FieldBasicType.parse_json(inner_json) |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def parse_array_type(cls, json): |
11 | 21 | if json['type'].startswith('[]'): |
12 | | - inner_type_str = json['type'][2:] |
13 | | - if inner_type_str.startswith('[]'): # this would be strange... |
14 | | - return None |
15 | | - inner_json = {**json, 'type': inner_type_str} |
16 | | - inner_type = super().parse_json(inner_json) |
17 | | - if inner_type is not None: |
18 | | - return cls(json, array_of=inner_type) |
| 22 | + type_str = json['type'][2:] |
| 23 | + assert not type_str.startswith('[]'), 'Unexpected double-array type: %s' % json['type'] |
| 24 | + return type_str |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def parse_json(cls, json): |
| 28 | + inner_type = cls.parse_inner_type(json) |
| 29 | + if inner_type is not None: |
| 30 | + return cls(json, array_of=inner_type) |
19 | 31 |
|
20 | 32 | def __init__(self, json, array_of=None): |
| 33 | + if array_of is None: |
| 34 | + array_of = self.parse_inner_type(json) |
| 35 | + assert array_of is not None, 'json does not contain a (simple) Array!' |
21 | 36 | super().__init__(json) |
22 | | - self.array_of = array_of # Field (ApiStruct or FieldBasicType) |
| 37 | + self.array_of = array_of # FieldBasicType |
23 | 38 |
|
24 | 39 | def is_array(self): |
25 | 40 | return True |
26 | 41 |
|
27 | | - def is_struct_array(self): |
28 | | - return self.array_of.is_struct() |
29 | | - |
30 | | - @property |
31 | | - def fields(self): |
32 | | - if self.is_struct_array(): |
33 | | - return self.array_of.fields |
34 | | - |
35 | | - def has_data_class(self): |
36 | | - return self.is_struct_array() |
37 | | - |
38 | | - @property |
39 | | - def data_class(self): |
40 | | - if self.has_data_class(): |
41 | | - return self.array_of.data_class |
42 | | - else: |
43 | | - raise ValueError('Non-struct field does not have a data_class!') |
44 | | - |
45 | | - def __call__(self, *args, **kw): |
46 | | - return self.data_class(*args, **kw) # pylint: disable=E1102 |
47 | | - |
48 | 42 | def to_schema(self, version, compact=False, tagged=False): |
49 | 43 | if not self.for_version_q(version) or self.tagged_field_q(version): |
50 | 44 | return None |
|
0 commit comments