-
Notifications
You must be signed in to change notification settings - Fork 50
Description
I'd like to continue the discussion started in #388.
Paul shared a number of great thoughts in his comment #388 (comment) and later Fantix and I had a quick 1:1 call and naturally discussed the API a bit.
Here's what I think we should do:
- I don't like
with_client_optionsAPI that I proposed. - I don't like an option to somehow configure existing codecs.
- I do think that we have to have a way to assign custom codecs to base scalar types. One use-case is redefining the behavior for std::json in edgedb-python. Another - supporting various decimal libraries in edgedb-js.
I propose the following API:
A new method called client.with_codec(codec: Codec). Enables the passed codec for the new client instance the method returns. Usage:
client2 = client.with_codec(JsonUnpackingCodec)A new method called client.with_default_codecs(). Returns a new client instance with all codecs reset to the default mapping. We need this in the codegen - to reset all codecs before we run the query.
A new Codec interface:
class Codec:
@classmethod
def get_scalar_type_name(cls) -> str:
"""Return the name of a scalar type the codec is designed for"""
# e.g. return 'std::int16` or `acme::my_int`
@classmethod
def get_codegen_type(cls) -> str:
"""Return the Python type name for the return value type"""
# E.g. for JsonUnpackingCodec it would be `typing.Any`; for a hypothetical
# "MyDecimalLib" codec it would be `MyDecimalLib.Decimal`
# Perhaps this method should return a tuple[str, str]:
# first element: module to import
# second element: type name defined in that module
def encode(...)
def decode(...)New command-line option for codegen: --with-codec that would accept a fully qualified name
to a Python factory for the codec, e.g. mypackage.mytype.EdgeDBCodec.