Polymorphic fields

Aperta
#460 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
12/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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?

Lingua principale
Python
Stelle
1.9k
Fork
205
Merge medio
2h 2m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di faust-streaming/faust

Tutte le issue di faust-streaming/faust

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.