Spaces:
Sleeping
Sleeping
File size: 1,151 Bytes
e40294e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from solverforge_legacy.solver.score import HardSoftDecimalScore
from typing import Any
from pydantic import (
BaseModel,
ConfigDict,
PlainSerializer,
BeforeValidator,
ValidationInfo,
)
from pydantic.alias_generators import to_camel
class JsonDomainBase(BaseModel):
model_config = ConfigDict(
alias_generator=to_camel,
populate_by_name=True,
from_attributes=True,
serialize_by_alias=True, # Output camelCase in JSON responses
)
ScoreSerializer = PlainSerializer(
lambda score: str(score) if score is not None else None,
return_type=str | None
)
IdSerializer = PlainSerializer(
lambda item: item.id if item is not None else None,
return_type=str | None
)
IdListSerializer = PlainSerializer(
lambda items: [item.id for item in items],
return_type=list
)
def validate_score(v: Any, info: ValidationInfo) -> Any:
if isinstance(v, HardSoftDecimalScore) or v is None:
return v
if isinstance(v, str):
return HardSoftDecimalScore.parse(v)
raise ValueError('"score" should be a string')
ScoreValidator = BeforeValidator(validate_score)
|