New codec setting API
- Dominant language
- Python
- Stars
- 415
- Forks
- 50
- PR merge metrics
- No merged PRs in 30d
Description
I'd like to continue the discussion started in https://github.com/edgedb/edgedb-python/pull/388.
Paul shared a number of great thoughts in his comment https://github.com/edgedb/edgedb-python/pull/388#issuecomment-1298428420 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:
```python
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:
```python
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
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.