Skip to content

Commit

Permalink
fix: add sorting to fields in schema_v1 (#15)
Browse files Browse the repository at this point in the history
* add sorting to fields in schema_v1

* fix: isort

* fix: update sorter for linked_params

* fix: change connection_sorter to source_sorter
  • Loading branch information
mstechly authored May 21, 2024
1 parent ac2aae7 commit ff7c67e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/qref/_schema_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_]*"
Expand All @@ -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"]
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit ff7c67e

Please sign in to comment.