base.py
python
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a
init: musehub initial commit
Human
171 days ago
| 1 | """Shared Pydantic base with camelCase wire-format serialization.""" |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | |
| 5 | from pydantic import BaseModel, ConfigDict |
| 6 | |
| 7 | |
| 8 | def to_camel(name: str) -> str: |
| 9 | """Convert snake_case to camelCase for JSON serialization.""" |
| 10 | parts = name.split("_") |
| 11 | return parts[0] + "".join(w.capitalize() for w in parts[1:]) |
| 12 | |
| 13 | |
| 14 | class CamelModel(BaseModel): |
| 15 | """Base model that serializes to camelCase on the wire. |
| 16 | |
| 17 | - Python code uses snake_case field names (PEP 8) |
| 18 | - JSON on the wire uses camelCase (web convention) |
| 19 | - ``model_dump()`` returns snake_case (internal use) |
| 20 | - ``model_dump(by_alias=True)`` returns camelCase (wire use) |
| 21 | """ |
| 22 | |
| 23 | model_config = ConfigDict( |
| 24 | alias_generator=to_camel, |
| 25 | populate_by_name=True, |
| 26 | ) |
File History
1 commit
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a
init: musehub initial commit
Human
171 days ago