base.py
python
sha256:7281683f5c41e5d88b6d8811fbdafebd3e01a0c9dcd90975cfcb444ba71e8e81
docs: add local source-of-truth for musehub#225, #226, #227…
Sonnet 5
1 day ago
| 1 | """Shared Pydantic base with camelCase wire-format serialization.""" |
| 2 | |
| 3 | from pydantic import BaseModel, ConfigDict |
| 4 | |
| 5 | def to_camel(name: str) -> str: |
| 6 | """Convert snake_case to camelCase for JSON serialization.""" |
| 7 | parts = name.split("_") |
| 8 | return f"{parts[0]}{''.join(w.capitalize() for w in parts[1:])}" |
| 9 | |
| 10 | class CamelModel(BaseModel): |
| 11 | """Base model that serializes to camelCase on the wire. |
| 12 | |
| 13 | - Python code uses snake_case field names (PEP 8) |
| 14 | - JSON on the wire uses camelCase (web convention) |
| 15 | - ``model_dump()`` returns snake_case (internal use) |
| 16 | - ``model_dump(by_alias=True)`` returns camelCase (wire use) |
| 17 | """ |
| 18 | |
| 19 | model_config = ConfigDict( |
| 20 | alias_generator=to_camel, |
| 21 | populate_by_name=True, |
| 22 | ) |
File History
1 commit
sha256:7281683f5c41e5d88b6d8811fbdafebd3e01a0c9dcd90975cfcb444ba71e8e81
docs: add local source-of-truth for musehub#225, #226, #227…
Sonnet 5
1 day ago