From ff7c67e0c89e79ca2f1c99f107c6a0cfc02cc29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=C2=A0St=C4=99ch=C5=82y?= Date: Tue, 21 May 2024 15:04:01 -0400 Subject: [PATCH] fix: add sorting to fields in schema_v1 (#15) * add sorting to fields in schema_v1 * fix: isort * fix: update sorter for linked_params * fix: change connection_sorter to source_sorter --- src/qref/_schema_v1.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/qref/_schema_v1.py b/src/qref/_schema_v1.py index f394724..f0a1b65 100644 --- a/src/qref/_schema_v1.py +++ b/src/qref/_schema_v1.py @@ -18,7 +18,7 @@ from typing import Annotated, Any, Literal, Optional, Union -from pydantic import BaseModel, ConfigDict, Field, StringConstraints +from pydantic import AfterValidator, BaseModel, ConfigDict, Field, StringConstraints from pydantic.json_schema import GenerateJsonSchema NAME_PATTERN = "[A-Za-z_][A-Za-z0-9_]*" @@ -32,6 +32,17 @@ _Value = Union[int, float, str] +def sorter(key): + def _inner(v): + return sorted(v, key=key) + + return _inner + + +name_sorter = AfterValidator(sorter(lambda p: p.name)) +source_sorter = AfterValidator(sorter(lambda c: c.source)) + + class _PortV1(BaseModel): name: Name direction: Literal["input", "output", "through"] @@ -70,14 +81,14 @@ class RoutineV1(BaseModel): """ name: Name - children: list[RoutineV1] = Field(default_factory=list) + children: Annotated[list[RoutineV1], name_sorter] = Field(default_factory=list) type: Optional[str] = None - ports: list[_PortV1] = Field(default_factory=list) - resources: list[_ResourceV1] = Field(default_factory=list) - connections: list[_ConnectionV1] = Field(default_factory=list) + ports: Annotated[list[_PortV1], name_sorter] = Field(default_factory=list) + resources: Annotated[list[_ResourceV1], name_sorter] = Field(default_factory=list) + connections: Annotated[list[_ConnectionV1], source_sorter] = Field(default_factory=list) input_params: list[Name] = Field(default_factory=list) local_variables: list[str] = Field(default_factory=list) - linked_params: list[_ParamLinkV1] = Field(default_factory=list) + linked_params: Annotated[list[_ParamLinkV1], source_sorter] = Field(default_factory=list) meta: dict[str, Any] = Field(default_factory=dict) model_config = ConfigDict(title="Routine")