This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I have a message queue, with messages that have a well defined schema. Up until very recently, there has been only one message type, and therefore I've only needed one schema.
A recent change to the queue now allows for multiple different messages, with different schemas on the same queue.
Up to now, I've been handling the messages like:
for msg in queue:
loaded_msg = MessageSchema.Schema().load(msg) # if the message doesn't meet the schema, that's bad, and the test should fail immediately
# do stuff
Now that there are two, this fails, as the new schema isn't a sub, or superset of the current model.
My current approach to handle this is:
for msg in queue:
try:
loaded_msg = MessageSchema.Schema().load(msg)
except ValidationError as e:
try:
_ = NewMessageSchema.schema().load(msg)
continue # Don't care about NewMessages, but it should still match one of the schemas
except ValidationError:
raise e # message doesn't meet either schema, this is bad
# do stuff
Obviously, this isn't scalable if new message schemas are allowed on the queue. What's the best way to handle it?
Subreddit
Post Details
- Posted
- 8 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnpython...