-
Notifications
You must be signed in to change notification settings - Fork 169
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
Pyish String representations of objects #581
Merged
Merged
Conversation
This file contains 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
This was referenced Jan 14, 2021
Interesting. Seems like a good approach. |
jasmith-hs
commented
Jan 19, 2021
@@ -221,6 +227,7 @@ public ExecutionMode getExecutionMode() { | |||
private int maxMacroRecursionDepth; | |||
private boolean failOnUnknownTokens; | |||
private boolean nestedInterpretationEnabled = true; | |||
private boolean usePyishObjectMapper = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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
PyishObjectMapper
class 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
, andCollection
object 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 thePyishSerializable
interface.When a class implements the
PyishSerializable
interface, it will get a default methodtoPyishString()
that callstoString()
providing 2 options:toString()
can be overridden (fromObject
or 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_name
included 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. UsingPyishSerializable
would 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.