🐛 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)
def sort_fields(self):
# sort fields so that fields that have .field_type in DIRECT_TYPES are first
self.fields.sort(
key=lambda x: DIRECT_TYPES.index(x.field_type)
if x.field_type in DIRECT_TYPES
else 100
)
# first sort alphabetically
# then sort fields so that fields that have .field_type in DIRECT_TYPES are first
self.fields.sort(key=lambda x: x.name)
self.fields.sort(key=lambda x: x.field_type in DIRECT_TYPES, reverse=False)
def to_dict(self, format_field_func=None):
self.process_fields(self.type_name, format_field_func)