-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(robot-server): Store Pydantic objects as JSON instead of pic…
…kles, take 2 (#14355)
- Loading branch information
1 parent
927c049
commit 2e58cfe
Showing
6 changed files
with
109 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Store Pydantic objects in the SQL database.""" | ||
|
||
from typing import Type, TypeVar | ||
from pydantic import BaseModel, parse_raw_as | ||
|
||
|
||
_BaseModelT = TypeVar("_BaseModelT", bound=BaseModel) | ||
|
||
|
||
def pydantic_to_json(obj: BaseModel) -> str: | ||
"""Serialize a Pydantic object for storing in the SQL database.""" | ||
return obj.json( | ||
# by_alias and exclude_none should match how | ||
# FastAPI + Pydantic + our customizations serialize these objects | ||
by_alias=True, | ||
exclude_none=True, | ||
) | ||
|
||
|
||
def json_to_pydantic(model: Type[_BaseModelT], json: str) -> _BaseModelT: | ||
"""Parse a Pydantic object stored in the SQL database.""" | ||
return parse_raw_as(model, json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters