🐛 fix(base.py): sort fields alphabetically before sorting by DIRECT_TYPES

The fields in the Template class were previously sorted by DIRECT_TYPES, which caused issues when fields had the same field_type. Sorting alphabetically first ensures that fields are sorted in a consistent manner before sorting by DIRECT_TYPES.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-23 15:53:48 -03:00
commit 02c6d77b64

View file

@ -20,12 +20,10 @@ class Template(BaseModel):
format_field_func(field, name) format_field_func(field, name)
def sort_fields(self): def sort_fields(self):
# sort fields so that fields that have .field_type in DIRECT_TYPES are first # first sort alphabetically
self.fields.sort( # then sort fields so that fields that have .field_type in DIRECT_TYPES are first
key=lambda x: DIRECT_TYPES.index(x.field_type) self.fields.sort(key=lambda x: x.name)
if x.field_type in DIRECT_TYPES self.fields.sort(key=lambda x: x.field_type in DIRECT_TYPES, reverse=False)
else 100
)
def to_dict(self, format_field_func=None): def to_dict(self, format_field_func=None):
self.process_fields(self.type_name, format_field_func) self.process_fields(self.type_name, format_field_func)