TRecursive.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # 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. from __future__ import absolute_import
  13. from __future__ import division
  14. from __future__ import print_function
  15. from __future__ import unicode_literals
  16. from thrift.Thrift import TType
  17. TYPE_IDX = 1
  18. SPEC_ARGS_IDX = 3
  19. SPEC_ARGS_CLASS_REF_IDX = 0
  20. SPEC_ARGS_THRIFT_SPEC_IDX = 1
  21. def 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. def _fix_list_or_set(element_type):
  36. # For a list or set, the thrift_spec entry looks like,
  37. # (1, TType.LIST, 'lister', (TType.STRUCT, [RecList, None], False), None, ), # 1
  38. # so ``element_type`` will be,
  39. # (TType.STRUCT, [RecList, None], False)
  40. if element_type[0] == TType.STRUCT:
  41. element_type[1][1] = element_type[1][0].thrift_spec
  42. elif element_type[0] in (TType.LIST, TType.SET):
  43. _fix_list_or_set(element_type[1])
  44. elif element_type[0] == TType.MAP:
  45. _fix_map(element_type[1])
  46. def _fix_map(element_type):
  47. # For a map of key -> value type, ``element_type`` will be,
  48. # (TType.I16, None, TType.STRUCT, [RecMapBasic, None], False), None, )
  49. # which is just a normal struct definition.
  50. #
  51. # For a map of key -> list / set, ``element_type`` will be,
  52. # (TType.I16, None, TType.LIST, (TType.STRUCT, [RecMapList, None], False), False)
  53. # and we need to process the 3rd element as a list.
  54. #
  55. # For a map of key -> map, ``element_type`` will be,
  56. # (TType.I16, None, TType.MAP, (TType.I16, None, TType.STRUCT,
  57. # [RecMapMap, None], False), False)
  58. # and need to process 3rd element as a map.
  59. # Is the map key a struct?
  60. if element_type[0] == TType.STRUCT:
  61. element_type[1][1] = element_type[1][0].thrift_spec
  62. elif element_type[0] in (TType.LIST, TType.SET):
  63. _fix_list_or_set(element_type[1])
  64. elif element_type[0] == TType.MAP:
  65. _fix_map(element_type[1])
  66. # Is the map value a struct?
  67. if element_type[2] == TType.STRUCT:
  68. element_type[3][1] = element_type[3][0].thrift_spec
  69. elif element_type[2] in (TType.LIST, TType.SET):
  70. _fix_list_or_set(element_type[3])
  71. elif element_type[2] == TType.MAP:
  72. _fix_map(element_type[3])