@@ -1076,6 +1076,39 @@ class PythonGenerator : public BaseGenerator {
1076
1076
code += Indent + Indent + " return None\n\n " ;
1077
1077
}
1078
1078
1079
+ // Get the value of a vector's union member.
1080
+ void GetMemberOfVectorOfUnion (const StructDef &struct_def,
1081
+ const FieldDef &field,
1082
+ std::string *code_ptr) const {
1083
+ auto &code = *code_ptr;
1084
+ auto vectortype = field.value .type .VectorType ();
1085
+
1086
+ GenReceiver (struct_def, code_ptr);
1087
+ code += namer_.Method (field);
1088
+ code += " (self, j):" + OffsetPrefix (field);
1089
+ code += Indent + Indent + Indent + " x = self._tab.Vector(o)\n " ;
1090
+ code += Indent + Indent + Indent;
1091
+ code += " x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * " ;
1092
+ code += NumToString (InlineSize (vectortype)) + " \n " ;
1093
+ code += Indent + Indent + Indent;
1094
+ code += " x -= self._tab.Pos\n " ;
1095
+
1096
+ // TODO(rw): this works and is not the good way to it:
1097
+ bool is_native_table = TypeName (field) == " *flatbuffers.Table" ;
1098
+ if (is_native_table) {
1099
+ code +=
1100
+ Indent + Indent + Indent + " from flatbuffers.table import Table\n " ;
1101
+ } else if (parser_.opts .include_dependence_headers ) {
1102
+ code += Indent + Indent + Indent;
1103
+ code += " from " + GenPackageReference (field.value .type ) + " import " +
1104
+ TypeName (field) + " \n " ;
1105
+ }
1106
+ code += Indent + Indent + Indent + " obj = Table(bytearray(), 0)\n " ;
1107
+ code += Indent + Indent + Indent + GenGetter (field.value .type );
1108
+ code += " obj, x)\n " + Indent + Indent + Indent + " return obj\n " ;
1109
+ code += Indent + Indent + " return None\n\n " ;
1110
+ }
1111
+
1079
1112
// Get the value of a vector's non-struct member. Uses a named return
1080
1113
// argument to conveniently set the zero value for the result.
1081
1114
void GetMemberOfVectorOfNonStruct (const StructDef &struct_def,
@@ -1521,6 +1554,8 @@ class PythonGenerator : public BaseGenerator {
1521
1554
auto vectortype = field.value .type .VectorType ();
1522
1555
if (vectortype.base_type == BASE_TYPE_STRUCT) {
1523
1556
GetMemberOfVectorOfStruct (struct_def, field, code_ptr, imports);
1557
+ } else if (vectortype.base_type == BASE_TYPE_UNION) {
1558
+ GetMemberOfVectorOfUnion (struct_def, field, code_ptr);
1524
1559
} else {
1525
1560
GetMemberOfVectorOfNonStruct (struct_def, field, code_ptr);
1526
1561
if (parser_.opts .python_gen_numpy ) {
@@ -1780,6 +1815,9 @@ class PythonGenerator : public BaseGenerator {
1780
1815
import_list->insert (" import " + package_reference);
1781
1816
}
1782
1817
field_type = " Optional[List[" + field_type + " ]" ;
1818
+ } else if (base_type == BASE_TYPE_UNION) {
1819
+ GenUnionInit (field, field_type_ptr, import_list, import_typing_list);
1820
+ field_type = " Optional[List[" + field_type + " ]]" ;
1783
1821
} else {
1784
1822
field_type = " Optional[List[" +
1785
1823
GetBasePythonTypeForScalarAndString (base_type) + " ]]" ;
0 commit comments