from decimal import Decimal
from datetime import date

from bson import UUID_SUBTYPE, Decimal128
from bson.codec_options import TypeEncoder, TypeRegistry, CodecOptions


class DecimalEncoder(TypeEncoder):
    """ 高精度数编码器 """
    python_type = Decimal
    
    def transform_python(self, value: Decimal) -> Decimal128:
        return Decimal128(str(value))
    

class DateEncoder(TypeEncoder):
    """ 日期编码器 """
    python_type = date
    
    def transform_python(self, value: date) -> str:
        return str(value)


codec_options = CodecOptions(
    type_registry=TypeRegistry([
        DecimalEncoder(),
        DateEncoder(),
    ]),
    uuid_representation=UUID_SUBTYPE,
)
