Skip to content

Commit

Permalink
refactor: replace dataclasses with msgspec
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Sep 16, 2024
1 parent 4c67520 commit f1f639d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 57 deletions.
15 changes: 1 addition & 14 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ litestar = { version = "^2.11.0", extras = ['jinja'] }
redis = { extras = ["hiredis"], version = "^5.0.8" }
uvicorn = { version = "^0.30.6" }
pydash = "^8.0.3"
dacite = "^1.8.1"
uuid-utils = "^0.9.0"
frozendict = "^2.4.4"
regex = "^2024.9.11"
Expand Down
45 changes: 15 additions & 30 deletions server/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Annotated, Any

import litestar
import msgspec
from litestar import params
from litestar.exceptions import NotFoundException
from litestar.response import Redirect, Template
Expand Down Expand Up @@ -283,58 +284,42 @@ async def show_user_review(
)


@router
@litestar.get("/api/count", opt=disable_cookies_opt)
async def count_handler(
patch_type: Annotated[PatchType, params.Parameter(query="type")] = PatchType.Subject,
patch_state_filter: Annotated[
StateFilter, params.Parameter(query="state")
] = StateFilter.Pending,
subject_id: int | None = None,
) -> dict[str, Any]:
table = f"view_{patch_type}_patch"

where, arg = patch_state_filter.to_sql(index=1)
args = [arg]

if subject_id is not None:
where += " AND subject_id = $2"
args.append(subject_id)

total: int = await pg.fetchval(f"select count(1) from {table} where {where}", *args)

return {"count": total}


@dataclass(slots=True, kw_only=True, frozen=True)
class PatchListItem:
class PendingSubject:
id: uuid.UUID
subject_id: int


class PendingSubjects(msgspec.Struct):
data: list[PendingSubject]


@router
@litestar.get("/api/subject/pending", opt=disable_cookies_opt)
async def current_pending_list() -> dict[str, Any]:
async def current_pending_list() -> PendingSubjects:
rows = await pg.fetch(
"select id,subject_id from view_subject_patch where state = $1",
PatchState.Pending,
)

return {"data": [PatchListItem(id=row[0], subject_id=row[1]) for row in rows]}
return PendingSubjects(data=[PendingSubject(id=row[0], subject_id=row[1]) for row in rows])


@dataclass(slots=True, kw_only=True, frozen=True)
class PendingEpisodeListItem:
class PendingEpisode(msgspec.Struct):
id: uuid.UUID
episode_id: int


class PendingEpisodes(msgspec.Struct):
data: list[PendingEpisode]


@router
@litestar.get("/api/episode/pending", opt=disable_cookies_opt)
async def current_pending_episode_list() -> dict[str, Any]:
async def current_pending_episode_list() -> PendingEpisodes:
rows = await pg.fetch(
"select id,episode_id from view_episode_patch where state = $1",
PatchState.Pending,
)

return {"data": [PendingEpisodeListItem(id=row[0], episode_id=row[1]) for row in rows]}
return PendingEpisodes(data=[PendingEpisode(id=row[0], episode_id=row[1]) for row in rows])
17 changes: 6 additions & 11 deletions server/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import enum
from dataclasses import dataclass
from datetime import datetime
from typing import Any, TypeVar
from uuid import UUID

from dacite import from_dict
import msgspec


class PatchState(enum.IntEnum):
Expand All @@ -17,8 +16,7 @@ class PatchState(enum.IntEnum):
T = TypeVar("T")


@dataclass(frozen=True, kw_only=True, slots=True)
class PatchBase:
class PatchBase(msgspec.Struct, kw_only=True, frozen=True):
id: UUID
state: int
from_user_id: int
Expand All @@ -33,11 +31,10 @@ class PatchBase:
@classmethod
def from_dict(cls: type[T], d: Any) -> T:
# will remove extra fields and do field level instance checking
return from_dict(cls, d)
return msgspec.convert(d, cls)


@dataclass(frozen=True, kw_only=True, slots=True)
class SubjectPatch(PatchBase):
class SubjectPatch(PatchBase, kw_only=True, frozen=True):
subject_id: int
subject_type: int

Expand All @@ -53,8 +50,7 @@ class SubjectPatch(PatchBase):
nsfw: bool | None


@dataclass(frozen=True, kw_only=True, slots=True)
class EpisodePatch(PatchBase):
class EpisodePatch(PatchBase, kw_only=True, frozen=True):
episode_id: int
ep: int | None = None

Expand All @@ -74,8 +70,7 @@ class EpisodePatch(PatchBase):
description: str | None


@dataclass(frozen=True, slots=True, kw_only=True)
class Wiki:
class Wiki(msgspec.Struct, kw_only=True):
name: str
infobox: str
summary: str
Expand Down
2 changes: 1 addition & 1 deletion taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tasks:

server:
cmds:
- watchfiles -target-type=command --filter python 'uvicorn server.app:app --env-file .env --no-access-log' server
- watchfiles --target-type=command --filter python 'uvicorn server.app:app --env-file .env --no-access-log' server

run:
dotenv: [ .env ]
Expand Down

0 comments on commit f1f639d

Please sign in to comment.