mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 04:21:13 +00:00
Decode bytes to strings in Python Object API (#8551)
This commit is contained in:
@@ -2028,6 +2028,20 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
void GenUnPackForString(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
|
||||
code += GenIndents(2) + "self." + field_field + " = " + struct_var + "." +
|
||||
field_method + "()";
|
||||
code += GenIndents(2) + "if self." + field_field + " is not None:";
|
||||
code += GenIndents(3) + "self." + field_field + " = self." + field_field +
|
||||
".decode('utf-8')";
|
||||
}
|
||||
|
||||
void GenUnPackForScalar(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
@@ -2070,6 +2084,14 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BASE_TYPE_STRING: {
|
||||
if (parser_.opts.python_decode_obj_api_strings) {
|
||||
GenUnPackForString(struct_def, field, &code);
|
||||
} else {
|
||||
GenUnPackForScalar(struct_def, field, &code);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: GenUnPackForScalar(struct_def, field, &code);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user