🚀 feat(base.py): add sorting of fields based on DIRECT_TYPES

The `sort_fields` method has been added to the `Template` class to sort fields based on the `DIRECT_TYPES` constant. Fields that have a `field_type` in `DIRECT_TYPES` are sorted first, followed by the remaining fields. This ensures that fields that have a direct type are processed first, which is important for the correct functioning of the template.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-23 09:13:22 -03:00
commit a23c53dd14

View file

@ -3,6 +3,7 @@ from typing import Callable, Optional, Union
from pydantic import BaseModel
from langflow.template.field.base import TemplateField
from langflow.utils.constants import DIRECT_TYPES
class Template(BaseModel):
@ -18,8 +19,17 @@ class Template(BaseModel):
for field in self.fields:
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
)
def to_dict(self, format_field_func=None):
self.process_fields(self.type_name, format_field_func)
self.sort_fields()
result = {field.name: field.to_dict() for field in self.fields}
result["_type"] = self.type_name # type: ignore
return result