AI-Assisted Coding Lab · Upcast Versioned Payment Events

50:00left of 50:00

Upcast Versioned Payment Events

Twelve teams consume the payments event log, and each replays it in full when it rebuilds state. Consumers read through upcast, which brings any event to the latest schema. The README has the schema history, the rules, and the stakes. Build events.py with the assistant, and judge each suggestion by what it would do to every consumer and every replay.

events.py1 of 3

Files

events.py

Python · Editable
import json
LATEST = 3
class UnknownVersion(Exception):
"""A version this consumer can't read. The event is parked and replayed after an upgrade."""
def version_of(event):
"""Events written before March 2020 have no version field. They are v1."""
return event["version"]
def v1_to_v2(event):
"""v2 added currency. Every v1 event was charged in USD."""
raise NotImplementedError
def v2_to_v3(event):
"""v3 replaced the dollar amount, a float, with integer cents."""
raise NotImplementedError
UPCASTERS = {1: v1_to_v2, 2: v2_to_v3}
def upcast(event):
"""Brings an event up to LATEST. The event passed in is left unchanged."""
version = version_of(event)
while version < LATEST:
event = UPCASTERS[version](event)
version += 1
return event
def write(event):
"""Producers write at LATEST, as a JSON line."""
raise NotImplementedError

Test Output

No runs yet.

Assistant

Scripted
  1. Send the first prompt to start.

Next Prompt

Old events have no version field. Treat them as v1.