Skip to content

New codec setting API #389

@1st1

Description

@1st1

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:

  1. I don't like with_client_options API that I proposed.
  2. I don't like an option to somehow configure existing codecs.
  3. 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.

cc @fantix @tailhook

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions