Polymorphic fields

Đang mở
#460 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
12/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
stream-processing

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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?

Ngôn ngữ chính
Python
Star
1.9k
Fork
205
Merge trung bình
2 giờ 2 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của faust-streaming/faust

Tất cả issue của faust-streaming/faust

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.