Pyish String representations of objects#581
Merged
Merged
Conversation
This was referenced Jan 14, 2021
Contributor
|
Interesting. Seems like a good approach. |
jasmith-hs
commented
Jan 19, 2021
| private int maxMacroRecursionDepth; | ||
| private boolean failOnUnknownTokens; | ||
| private boolean nestedInterpretationEnabled = true; | ||
| private boolean usePyishObjectMapper = false; |
Contributor
Author
There was a problem hiding this comment.
It is disabled by default, as it intentionally makes changes to the output. This may be changed to be enabled by default after a major release
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.
In Jinjava when a Map object is resolved as a string, the output differs from what the output would be using Jinja. Also, the output of collections differs from how Jinja handles them.
{{ {'foo': 'a', 'bar': 'b'} }}{'foo': 'a', 'bar': 'b'}{foo=a, bar=b}{{ ['a', 'b'] }}['a', 'b'][a, b]The current way that Jinjava maps objects to string values is not done like in python.
This PR adds a flexible solution to this problem by adding a config option to use a new
PyishObjectMapperclass to map objects to string values. When using this mapper, by default, all object classes will have theirtoString()method used to serialize them to python strings. However, by default, aMap, andCollectionobject will be serialized using Jackson's default JSON serialization as a container. This results in the string output matching up with the desired pythonic string representation. Other classes can be added to this as well if theirtoString()method does not produce a python string OR they can implement thePyishSerializableinterface.When a class implements the
PyishSerializableinterface, it will get a default methodtoPyishString()that callstoString()providing 2 options:toString()can be overridden (fromObjector whatever) to return a python string representationtoPyishString()can be overridden to return a python string representation if thetoString()method needs to remain untouched/different for other purposes.Here's a minimal example where when the class is converted to a string in Jinjava, it's shown as a dictionary with
first_name,last_nameincluded and without the email address:Some additional HubSpot specific usefulness, a row from the
{{ hubdb_table_rows(123456) }}function returns a result like:[{id=12345, createdAt=1542736419000, path='some_path', name='some name', 1='{type=string, value=column value}'}]. This is unintuitive because those aren't the names of the attribute as they are accessed in HubL. UsingPyishSerializablewould allow for the Jinjava string representation of this result to be like a python dictionary.@boulter @mattcoley I am curious what your thoughts are on this.