Skip to content

Conversation

@arhamchopra
Copy link
Collaborator

@arhamchopra arhamchopra commented Jun 13, 2024

This PR adds postprocess_to_dict hook to the structs.
This hook is invoked by the to_dict method on the dict obtained from parsing a struct. It will allows users to customize the final dict generated by the to_dict method for each struct.

class A(Struct):
    replace_me: str
    data: int = 0
    def postprocess_to_dict(self, obj_dict):
        obj_dict['replace_me'] = 'replace_me'
        obj_dict['postprocess_invoked'] = True
        return obj_dict

a = A(replace_me='Dummy', data=1)
a.to_dict() == {'replace_me': 'replace_me', 'data': 1, 'postprocess_invoked': True}

@arhamchopra arhamchopra marked this pull request as ready for review June 13, 2024 19:00
@timkpaine timkpaine added the type: enhancement Issues and PRs related to improvements to existing features label Jun 13, 2024
AdamGlustein
AdamGlustein previously approved these changes Jun 14, 2024
@AdamGlustein
Copy link
Collaborator

AdamGlustein commented Jun 14, 2024

Note that the function was removed in #258 as no internal csp code called it and it was meant to be an internal function.

However, an external user reported that they were using the feature and this change broke their pipeline, so we're adding it back in.

@arhamchopra
Copy link
Collaborator Author

arhamchopra commented Jun 14, 2024

Note that the function was removed in #258 as no internal csp code called it and it was meant to be an internal function.

However, an external user reported that they were using the feature and this change broke their pipeline, so we're adding it back in.

Note the difference in the name postprocess_to_dict versus _postprocess_dict_to_python (in #258).
This method is now being exposed as a public hook so that users can override this method if needed.

Signed-off-by: Arham Chopra <arham.chopra@cubistsystematic.com>
@arhamchopra arhamchopra force-pushed the ac/add_postprocess_dict_hook branch from 5b8b122 to 5deccad Compare June 14, 2024 21:39
@arhamchopra arhamchopra changed the title Add __postprocess_to_dict__ hook for to_dict method in structs Add postprocess_to_dict hook for to_dict method in structs Jun 14, 2024
@arhamchopra arhamchopra merged commit a198332 into main Jul 8, 2024
@arhamchopra arhamchopra deleted the ac/add_postprocess_dict_hook branch July 8, 2024 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement Issues and PRs related to improvements to existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants