Verified Commit 9eb574f3 authored by Jakob Moser's avatar Jakob Moser
Browse files

Add a TypeDecorator for IBAN

This way, I can use the schwifty IBAN type in one of my SQL Alchemy classes. It will neatly be serialized to and deserialized from simple SQL strings, so I don't need to worry about that.
parent b6f5875f
Loading
Loading
Loading
Loading

poolpay/model/Iban.py

0 → 100644
+15 −0
Original line number Diff line number Diff line
import sqlalchemy.types as types
from schwifty import IBAN
from sqlalchemy.engine.interfaces import Dialect


class Iban(types.TypeDecorator[IBAN]):
    impl = types.String

    cache_ok = True

    def process_bind_param(self, value: IBAN | None, dialect: Dialect) -> str | None:
        return str(value) if value is not None else None

    def process_result_value(self, value: str | None, dialect: Dialect) -> IBAN | None:
        return IBAN(value) if value is not None else None