how to create one polymorphic model

Abierto
#489 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
12/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python

Línea de trabajo

The request centres on the polymorphic_fields=True option of faust.Record and on passing such a model as value_type to app.topic, so start by finding where Record options and topic value deserialization are handled. Before any change, decide what the maintainers want: the issue asks whether this is feasible and never states a design, and it has been open for about 1,290 days with no decision. Done would mean a written design agreed in the thread, then a test that deserializes a type-tagged event into the matching subclass.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

I want to be able to process 1 event that can have multiple schemas based on type.
for example

class Event(faust.Record, serializer="json"):
    track_id: str

class InsertDataEvent(Event):
    file_path: str
    type: str = "INSERT"

class CreateTableEvent(Event):
    table_name: str
    type: str = "CREATE_TABLE"

class PolyEvent(faust.Record, polymorphic_fields=True):
     ...

And when I process the event, I want it to automatically be parsed by the type, and validated against the leaf class (AKA CreateTableEvent or InsertDataEvent).

ingestion_topic = app.topic("my-topic", key_type=str, value_type=PolyEvent)

@app.agent(channel=ingestion_topic)
async def process(stream) -> None:
    async for value in stream.events():
        event = value.value
        if event.type == "INSERT":
            insert_data(event.file_path)
        if event.type == "CREATE_TABLE":
            create_table(event.table_name)

Is this feasible? how would I write PolyEvent in that case?

Checklist

  • I have included information about relevant versions
  • I have verified that the issue persists when using the master branch of Faust.

Versions

  • Python version - 3.11.0
  • Faust version - 0.10.3
Lenguaje dominante
Python
Estrellas
1.9k
Forks
205
Merge medio
2 h 2 min
PR fusionados (30 d)
1

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de faust-streaming/faust

Todos los issues de faust-streaming/faust

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.