mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 20:15:34 +00:00
Add comparison operator to python objects under --gen-compare option (#7610)
* Add comparison operator to python code generator * Missing semi-colon * Regenerate test examples Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -1141,6 +1141,23 @@ class PythonGenerator : public BaseGenerator {
|
||||
code += "\n";
|
||||
}
|
||||
|
||||
void GenCompareOperator(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += GenIndents(1) + "def __eq__(self, other):";
|
||||
code += GenIndents(2) + "return type(self) == type(other)";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
// Wrties the comparison statement for this field.
|
||||
const auto field_field = namer_.Field(field);
|
||||
code += " and \\" + GenIndents(3) + "self." + field_field + " == " + "other." + field_field;
|
||||
}
|
||||
code += "\n";
|
||||
}
|
||||
|
||||
void GenUnPackForStruct(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
@@ -1623,6 +1640,10 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
InitializeFromObjForObject(struct_def, &code);
|
||||
|
||||
if (parser_.opts.gen_compare) {
|
||||
GenCompareOperator(struct_def, &code);
|
||||
}
|
||||
|
||||
GenUnPack(struct_def, &code);
|
||||
|
||||
if (struct_def.fixed) {
|
||||
|
||||
Reference in New Issue
Block a user