Issue 93 - Determine payload value type based on enum (Custom deserialize()) - #462
Draft
joaoag wants to merge 9 commits into
Draft
Issue 93 - Determine payload value type based on enum (Custom deserialize())#462joaoag wants to merge 9 commits into
deserialize())#462joaoag wants to merge 9 commits into
Conversation
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
…e:value (type) mapping Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
Signed-off-by: João Abbott-Gribben <joao.abbott.gribben@gmail.com>
joaoag
force-pushed
the
issue-93-parse-value-type
branch
from
July 10, 2026 20:48
3f419f1 to
0784992
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #462 +/- ##
==========================================
+ Coverage 83.36% 83.84% +0.47%
==========================================
Files 50 50
Lines 7354 7385 +31
==========================================
+ Hits 6131 6192 +61
+ Misses 1223 1193 -30 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Author
|
@stefanvi just a gentle nudge on this for when you get back from your holidays : ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
This PR isn’t meant to be a full implementation, just a POC with enough code to indicate the direction of travel and to understand if the approach acceptable in principle
Before this PR:
{"type":"PRICE","values":[1]}parses1asValue::Integer(1), so validation rejects a legitimate price because it's not formatted as a floatAfter this PR:
{"type":"PRICE","values":[1]}parses1asValue::Integer(1)and then normalises toValue::Number(1.0)so it passes validation.Rationale for approach
My taking from the discussions on Determine payload value type based on enum #93 and Validate event payload values match their type #124 especially the quoted comment from above, was that we wanted a way to address the class of bugs that would arise out of e.g. treating a whole number on the wire (e.g. 1) as an integer - because
#[serde(untagged)]tries theIntegervariant beforeNumber- when the payload'svalue_type(e.g.PRICE) actually expects a float.While I saw that there was talk about implementing this in a way that made invalid states unrepresentable, which I agree with in principle, I wanted to see if a more conservative / intermediate-step approach would also be acceptable, so I’ve created this POC for the custom deserialization normalising after parsing.
The approach in this PR is to accept that values may be put in the wrong types for a short time while introduces a mechanism to correct / normalize known mis-typings.
The PR also keeps DRY the relationship between the event kinds and the value types between the new normalize function and the existing validate function. I thought it’d be preferable for them to share the same source of truth, but appreciate that introduces tighter coupling which may not be desired - happy to keep two separate lists if it’s preferred.
Summary of changes
deserialize(), which:Valueas per existing enum + serde macronormalize_value()function and associated helper methodsEventTypevariants and their expectedValuetype/kind variants via a newEventType::expected_value()method returning aValueKind, consumed by bothnormalize_value()andvalidate_value()Outstanding work to be implemented if approach gets approved
SIMPLE,CTA2045_*) be normalized to an int?Misc. notes for reviewers
I removed most of the comments against the
EventTypes, only keeping ones which looked like they contained very specific domain knowledge - very happy to reinstate the others if neededNormalisation runs only on the
deserialize()path, so a hand-constructed EventValuesMap with an Integer under PRICE wouldn't be normalised and would failvalidate()- I assumed this was acceptable given the discussions, let me know if not.I considered having
expected_value()return anOption<ValueKind>and having anyEventTypes which have no constraints returnNone(instead of the current return ofAny) e.g. insideexpected_value()this arm would beControlSetpoint | Private(_) => None,- roughly the changes outlined below. Happy to implement this way, if it’s preferred.