Polymorphic fields

オープン
#460 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
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分
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

faust-streaming/faust のほかの issue

faust-streaming/faust の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。