blob: aed696ad9934a13aee1cd2f24235a994a15f5477 [file] [log] [blame]
Eric Connerc34653f2017-06-21 03:34:12 +02001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10# See the License for the specific language governing permissions and
11# limitations under the License.
12
Eric Connerc34653f2017-06-21 03:34:12 +020013from thrift.Thrift import TType
14
15TYPE_IDX = 1
16SPEC_ARGS_IDX = 3
17SPEC_ARGS_CLASS_REF_IDX = 0
18SPEC_ARGS_THRIFT_SPEC_IDX = 1
19
20
21def fix_spec(all_structs):
22 """Wire up recursive references for all TStruct definitions inside of each thrift_spec."""
23 for struc in all_structs:
24 spec = struc.thrift_spec
25 for thrift_spec in spec:
26 if thrift_spec is None:
27 continue
28 elif thrift_spec[TYPE_IDX] == TType.STRUCT:
29 other = thrift_spec[SPEC_ARGS_IDX][SPEC_ARGS_CLASS_REF_IDX].thrift_spec
30 thrift_spec[SPEC_ARGS_IDX][SPEC_ARGS_THRIFT_SPEC_IDX] = other
31 elif thrift_spec[TYPE_IDX] in (TType.LIST, TType.SET):
32 _fix_list_or_set(thrift_spec[SPEC_ARGS_IDX])
33 elif thrift_spec[TYPE_IDX] == TType.MAP:
34 _fix_map(thrift_spec[SPEC_ARGS_IDX])
35
36
37def _fix_list_or_set(element_type):
Eric Connerb56ead32017-07-06 21:38:05 -070038 # For a list or set, the thrift_spec entry looks like,
Eric Connerc34653f2017-06-21 03:34:12 +020039 # (1, TType.LIST, 'lister', (TType.STRUCT, [RecList, None], False), None, ), # 1
40 # so ``element_type`` will be,
41 # (TType.STRUCT, [RecList, None], False)
42 if element_type[0] == TType.STRUCT:
43 element_type[1][1] = element_type[1][0].thrift_spec
44 elif element_type[0] in (TType.LIST, TType.SET):
45 _fix_list_or_set(element_type[1])
46 elif element_type[0] == TType.MAP:
47 _fix_map(element_type[1])
48
49
50def _fix_map(element_type):
51 # For a map of key -> value type, ``element_type`` will be,
52 # (TType.I16, None, TType.STRUCT, [RecMapBasic, None], False), None, )
53 # which is just a normal struct definition.
54 #
55 # For a map of key -> list / set, ``element_type`` will be,
56 # (TType.I16, None, TType.LIST, (TType.STRUCT, [RecMapList, None], False), False)
57 # and we need to process the 3rd element as a list.
Eric Connerb56ead32017-07-06 21:38:05 -070058 #
Eric Connerc34653f2017-06-21 03:34:12 +020059 # For a map of key -> map, ``element_type`` will be,
Eric Connerb56ead32017-07-06 21:38:05 -070060 # (TType.I16, None, TType.MAP, (TType.I16, None, TType.STRUCT,
Eric Connerc34653f2017-06-21 03:34:12 +020061 # [RecMapMap, None], False), False)
62 # and need to process 3rd element as a map.
63
64 # Is the map key a struct?
65 if element_type[0] == TType.STRUCT:
66 element_type[1][1] = element_type[1][0].thrift_spec
67 elif element_type[0] in (TType.LIST, TType.SET):
68 _fix_list_or_set(element_type[1])
69 elif element_type[0] == TType.MAP:
70 _fix_map(element_type[1])
71
72 # Is the map value a struct?
73 if element_type[2] == TType.STRUCT:
74 element_type[3][1] = element_type[3][0].thrift_spec
75 elif element_type[2] in (TType.LIST, TType.SET):
76 _fix_list_or_set(element_type[3])
77 elif element_type[2] == TType.MAP:
78 _fix_map(element_type[3])