Polymorphic fields

未关闭
#460 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
12/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
python

调研方向

The issue centres on the polymorphic_fields=True option of Faust's Record class and the _blessed_key attribute, plus the from_data override in the reporter's snippet; the payload names no specific files. Decide first whether a custom blessed key format is meant to be supported, since the issue asks that question rather than requesting a specific change. Done would mean a maintainer records that decision, with either a documented extension point or a closed issue.

由索引模型根据 Issue 内容生成。

描述

question

I am in the process of converting a legacy system that was using an in-house streaming package to Faust. We had our own "polymorphic typing" implementation that made messages look like this:

{
    "class": "MessageClassType",
    "data": {"id": "1234", "other_field": "4321", ...}
}

I've been trying to match this to Faust's Polymorphic fields, but I'm stuck. From the documentation, I cannot understand if this feature should be used only when Faust has produced messages and added the blessed key, or if I can customize this. I am currently not yet producing messages using Faust, so it's unclear if Faust will always add the blessed key to the messages.

So far, I was able to come up with something like this:

class MyMessage(Record, polymorphic_fields=True, serializer='avroschema'):
    _blessed_key = 'blessed_key'

    class_: str

    @classmethod
    def from_data(cls, data: Mapping, **kwargs):
        if 'class' in data:
            data = {**data, "class_": data['class'], 'blessed_key': {"ns": data['class']}}
        return super().from_data(data, **kwargs)

class PolymorphicMessageField(Record):
    id: str
    other_field: str


class MyMessagePolymorphic(MyMessage, namespace='MyMessagePolymorphic'):
    class_ = "MyMessagePolymorphic"
    data: PolymorphicMessageField

This seems to be working. If I receive a message with the contents {"class": "MyMessagePolymorphic", "data": {"id": "1234", "other_field": "4321"}}, I am able to check the type on both the message (matching MyMessagePolymorphic), and the data field (matching PolymorphicMessageField).

However, this seems overly clunky for a couple reasons:

  • I am specifying the blessed key by overriding the underscore attribute _blessed_key.
  • I need to set the namespace to the same class name which causes repetition, otherwise Faust automatically appends the module name into it.
  • It is necessarily the case that the blessed key will be a dictionary with the class name inside a ns field, so I also need to override the from_data method.

Is there a better or more supported way to accomplish this?

主要语言
Python
星标
1.9k
派生
205
平均合并
2 小时 2 分钟
30 天内合并 PR
1

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

faust-streaming/faust 的其他 Issue

查看 faust-streaming/faust 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。