Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the record-data building + post-processing in Objects API registration v2 #4851

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

sergei-maertens
Copy link
Member

@sergei-maertens sergei-maertens commented Nov 22, 2024

Part of #4431 - builds on top of #4850

Changes

Refactored the Objects API v2 registration data building/transformations after the mappings were implemented by @vaszig. I was also able to add more files to the type checker and finally some type definitions / hints could also be cleaned up, hurray!

This had been on my mind for a while when we were fixing the bugs specifically to the file upload types w/r to single/multiple uploads, and now the additional options lead to more 🍝 code in this registration handler, so I figured it was time to put my thoughts into code.

Copy link

codecov bot commented Nov 22, 2024

Codecov Report

Attention: Patch coverage is 96.77419% with 5 lines in your changes missing coverage. Please review.

Project coverage is 96.61%. Comparing base (a17cfef) to head (6c7d33b).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...ons/contrib/objects_api/submission_registration.py 92.18% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4851      +/-   ##
==========================================
- Coverage   96.61%   96.61%   -0.01%     
==========================================
  Files         749      751       +2     
  Lines       25617    25679      +62     
  Branches     3394     3395       +1     
==========================================
+ Hits        24750    24809      +59     
- Misses        604      608       +4     
+ Partials      263      262       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

The code was already starting to look like a rather fine Italian pasta
before the addressNL mapping because of component-specific handling of
values that was necessary, but now with the additional configuration
options specific to the component type, readability and maintainability
was in serious danger.

I've opted to look at the broader structure on what information we
need, what information we have and what it is exactly that we need
to do with the information:

* We know all the variable values (keys and value), coming from user
  input, derived from user input or entirely independent from user
  input.
* When we know the key of a variable and we know the variable source is
  a form component, we can look up the definition of the form
  component and pass it along as additional context
* We know some user input (file uploads) is processed differently _in
  the context of the objects API_ and we have data storage that allows
  us to translate
* We need to normalize some values to make them suitable for use in the
  Objects API, because a JSON Schema contract is used. Looking at you,
  file uploads with multiple=false.
* We may have additional mapping configuration for a specific variable,
  typically because the variable points to a particular component type
  with special requirements (looking at you, addressNL)
* We must be able to construct the record data for the objects API. The
  keys to be used are specified in the mapped variables configuration,
  namely the target paths.
* We must process all the mapped variables to construct the entire
  object to send to the API.
* We can map all kinds of variables (form, user defined, static,
  registration).

So, working backwards, what we needed was for each mapped variable to
make it return instructions which paths of the final object need to
be set to which value. This is solved with AssignmentSpec data classes,
they simply encode the {key: value} information in a well-typed manner.

Next, a single mapping may result in multiple assignments being done
to different parts of the object - this is the newest requirement from
AddressNL type. We can express that as a sequence of AssignmentSpecs.

Finally, we collect all this information, bundle it up and hand it over
to glom which constructs our actual data object, from the mappings.

That leaves us with the requirement of a thing that produces
AssignmentSpec instructions given a mapping configuration and a value.
Because we already know that the value must be transformed depending
on its meaning (=which component produced it), we also pass the
resolved component configuration along for additional context.

For good measure and better code organization, the bulk of the
implementation is moved into handlers/v2.py. More code will be moved
around if this passes the initial reviews.
@sergei-maertens sergei-maertens force-pushed the refactor/objects-api-v2-postprocessing branch from c88083a to 6c7d33b Compare November 25, 2024 10:59
@sergei-maertens sergei-maertens merged commit 43385cc into master Nov 25, 2024
33 checks passed
@sergei-maertens sergei-maertens deleted the refactor/objects-api-v2-postprocessing branch November 25, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants