Skip to content

Commit

Permalink
wasm: support list lookup in get_property (#13483)
Browse files Browse the repository at this point in the history
Signed-off-by: Kuat Yessenov <kuat@google.com>
  • Loading branch information
kyessenov authored Oct 12, 2020
1 parent a0f31ee commit 65c2d2a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions source/extensions/common/wasm/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,16 @@ WasmResult Context::getProperty(absl::string_view path, std::string* result) {
return WasmResult::InternalFailure;
}
}
} else if (value.IsList()) {
auto& list = *value.ListOrDie();
int idx = 0;
if (!absl::SimpleAtoi(part, &idx)) {
return WasmResult::NotFound;
}
if (idx < 0 || idx >= list.size()) {
return WasmResult::NotFound;
}
value = list[idx];
} else {
return WasmResult::NotFound;
}
Expand Down
14 changes: 14 additions & 0 deletions test/extensions/filters/http/wasm/test_data/test_cpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ void TestRootContext::onTick() {
logDebug("missing node metadata");
}
logDebug(std::string("onTick ") + value);

std::string list_value;
if (!getValue({"node", "metadata", "wasm_node_list_key", "0"}, &list_value)) {
logDebug("missing node metadata list value");
}
if (list_value != "wasm_node_get_value") {
logWarn("unexpected list value: " + list_value);
}
if (getValue({"node", "metadata", "wasm_node_list_key", "bad_key"}, &list_value)) {
logDebug("unexpected list value for a bad_key");
}
if (getValue({"node", "metadata", "wasm_node_list_key", "1"}, &list_value)) {
logDebug("unexpected list value outside the range");
}
} else if (test_ == "property") {
uint64_t t;
if (WasmResult::Ok != proxy_get_current_time_nanoseconds(&t)) {
Expand Down
2 changes: 2 additions & 0 deletions test/extensions/filters/http/wasm/wasm_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,8 @@ TEST_P(WasmHttpFilterTest, Metadata) {
ProtobufWkt::Value node_val;
node_val.set_string_value("wasm_node_get_value");
(*node_data.mutable_metadata()->mutable_fields())["wasm_node_get_key"] = node_val;
(*node_data.mutable_metadata()->mutable_fields())["wasm_node_list_key"] =
ValueUtil::listValue({node_val});
EXPECT_CALL(local_info_, node()).WillRepeatedly(ReturnRef(node_data));
EXPECT_CALL(rootContext(),
log_(spdlog::level::debug, Eq(absl::string_view("onTick wasm_node_get_value"))));
Expand Down

0 comments on commit 65c2d2a

Please sign in to comment.