-
Notifications
You must be signed in to change notification settings - Fork 15
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
Inja transformation preserve escaped json #258
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6fcfde4
add option to json escape after rendering
jbohanon f240b92
use new inja feature to escape strings
jbohanon a0f835f
cleanup
jbohanon 0e887fd
add to_json callback to visualize options
jbohanon a3fa993
do strip " on callback
jbohanon 75e9bb2
use const json * arg directly instead of getting string and casting t…
jbohanon a25c18d
add proto comment; add integration tests and clean up tests
jbohanon 601ebe3
add changelog
jbohanon 46f2a5f
PR review feedback
jbohanon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
changelog: | ||
- type: DEPENDENCY_BUMP | ||
dependencyOwner: solo-io | ||
dependencyRepo: inja | ||
dependencyTag: v3.4.0-patch2 # not yet released DO NOT MERGE | ||
- type: NEW_FEATURE | ||
issueLink: https://github.com/solo-io/solo-projects/issues/5155 | ||
resolvesIssue: false | ||
description: >- | ||
Inja callback `raw_string` added to permit fine-grained escaping of input strings. | ||
Also, configuration added to escape all strings for a given transformation. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,9 @@ TransformerInstance::TransformerInstance(ThreadLocal::Slot &tls, Envoy::Random:: | |
env_.add_callback("replace_with_random", 2, [this](Arguments &args) { | ||
return replace_with_random_callback(args); | ||
}); | ||
env_.add_callback("raw_string", 1, [this](Arguments &args) { | ||
return raw_string_callback(args); | ||
}); | ||
} | ||
|
||
json TransformerInstance::header_callback(const inja::Arguments &args) const { | ||
|
@@ -343,6 +346,32 @@ std::string& TransformerInstance::random_for_pattern(const std::string& pattern) | |
return found->second; | ||
} | ||
|
||
json TransformerInstance::raw_string_callback(const inja::Arguments &args) const { | ||
// inja::Arguments is a std::vector<const json *>, so we can get the json | ||
// value from the args directly. | ||
const auto& input = args.at(0); | ||
nfuden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// make sure to bail if we're not working with a raw string value | ||
if(!input->is_string()) { | ||
return input->get_ref<const std::string&>(); | ||
} | ||
|
||
auto val = input->dump(); | ||
Comment on lines
+356
to
+361
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
// This block makes it such that a template must have surrounding " characters | ||
// around the raw string. This is reasonable since we expect the value we get out of the | ||
// context (body) to be placed in exactly as-is. HOWEVER, the behavior of the jinja | ||
// filter is such that the quotes added by .dumps() are left in. For that reason, | ||
// this callback is NOT named to_json to avoid confusion with that behavior. | ||
|
||
// strip the leading and trailing " characters that are added by dump() | ||
// if C++20 is adopted, val.starts_with and val.ends_with would clean this up a bit | ||
val = val.substr(0,1) == "\"" && val.substr(val.length()-1,1) == "\"" | ||
? val.substr(1, val.length()-2) | ||
: val; | ||
return val; | ||
nfuden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// parse calls Inja::Environment::parse which uses non-const references to member | ||
// data fields. This method is NOT SAFE to call outside of the InjaTransformer | ||
// constructor since doing so could cause Inja::Environment member fields to be | ||
|
@@ -372,12 +401,15 @@ InjaTransformer::InjaTransformer(const TransformationTemplate &transformation, | |
passthrough_body_(transformation.has_passthrough()), | ||
parse_body_behavior_(transformation.parse_body_behavior()), | ||
ignore_error_on_parse_(transformation.ignore_error_on_parse()), | ||
render_body_as_json_(transformation.render_body_as_json()), | ||
tls_(tls.allocateSlot()), | ||
instance_(std::make_unique<TransformerInstance>(*tls_, rng)) { | ||
if (advanced_templates_) { | ||
instance_->set_element_notation(inja::ElementNotation::Pointer); | ||
} | ||
|
||
instance_->set_escape_strings(render_body_as_json_); | ||
|
||
tls_->set([](Event::Dispatcher&) -> ThreadLocal::ThreadLocalObjectSharedPtr { | ||
return std::make_shared<ThreadLocalTransformerContext>(); | ||
}); | ||
|
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 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
Oops, something went wrong.
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.
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.
i think we should rename this. perhaps this has similar utility for folks who want the body rendered for, say, yaml
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.
say
escape_characters
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.
I like it. I renamed the field