Skip to content

Commit

Permalink
add sorting to fields in schema_v1
Browse files Browse the repository at this point in the history
  • Loading branch information
mstechly authored May 16, 2024
1 parent 78827c0 commit e7f708a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/qref/_schema_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from pydantic import BaseModel, ConfigDict, Field, StringConstraints
from pydantic.json_schema import GenerateJsonSchema
from pydantic import AfterValidator

NAME_PATTERN = "[A-Za-z_][A-Za-z0-9_]*"
NAMESPACED_NAME_PATTERN = rf"{NAME_PATTERN}\.{NAME_PATTERN}"
Expand All @@ -32,6 +33,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))
connection_sorter = AfterValidator(sorter(lambda c: c.source))


class _PortV1(BaseModel):
name: Name
direction: Literal["input", "output", "through"]
Expand Down Expand Up @@ -70,14 +82,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], connection_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], name_sorter] = Field(default_factory=list)
meta: dict[str, Any] = Field(default_factory=dict)

model_config = ConfigDict(title="Routine")
Expand Down

0 comments on commit e7f708a

Please sign in to comment.