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

delete callback #347

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions source/extensions/filters/http/transformation/inja_transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ TransformerInstance::TransformerInstance(ThreadLocal::Slot &tls, Envoy::Random::
env_.add_callback("word_count", 1, [](Arguments &args) {
return word_count_callback(args);
});
env_.add_callback("delete_key", 2, [](Arguments &args) {
return delete_key_callback(args);
});
}


Expand Down Expand Up @@ -492,6 +495,13 @@ json TransformerInstance::base64url_decode_callback(const inja::Arguments &args)
return Base64Url::decode(input);
}

json TransformerInstance::deletekey_callback(const inja::Arguments &args) {
auto json_input = args.at(0)->get<json::object_t>();
const std::string & json_key = args.at(1)->get_ref<const std::string &>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have advanced_templates set we will need to make a json pointer from the arg https://json.nlohmann.me/features/json_pointer/#introduction

json_input.erase(json_key);
return json_input;
}

json TransformerInstance::word_count_callback(const inja::Arguments &args) {
return json_word_count(args.at(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class TransformerInstance {
static nlohmann::json parse_metadata(const envoy::config::core::v3::Metadata* metadata,
const inja::Arguments &args);
static nlohmann::json word_count_callback(const inja::Arguments &args);
static nlohmann::json delete_key_callback(const inja::Arguments &args);
static int json_word_count(const nlohmann::json* str);
static int word_count(const std::string& str);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,22 @@ TEST_F(InjaTransformerTest, WordCountJSON) {
transformer.transform(headers, &headers, body, callbacks);
}

TEST_F(InjaTransformerTest, DeleteKey) {
Http::TestRequestHeaderMapImpl headers{{":method", "GET"}, {":path", "/foo"}};
TransformationTemplate transformation;

transformation.mutable_body()->set_text("{{delete_key( context() , \"a\")}}");

InjaTransformer transformer(transformation, rng_, google::protobuf::BoolValue(), tls_);

NiceMock<Http::MockStreamDecoderFilterCallbacks> callbacks;

auto test_string = "{\"a\": \"Hal, what's the meaning of life?\", \"b\": \"I don't know John\"}";
Buffer::OwnedImpl body(test_string);
transformer.transform(headers, &headers, body, callbacks);
EXPECT_EQ(body.toString(), "{\"b\":\"I don't know John\"}");
}

TEST_F(InjaTransformerTest, SubstringOutOfBounds) {
Http::TestRequestHeaderMapImpl headers{{":method", "GET"}, {":path", "/foo"}};
TransformationTemplate transformation;
Expand Down
Loading