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

[Proposal] WIP: Add support for setDynamicdata (#15148) #15196

Closed
Closed
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
48 changes: 47 additions & 1 deletion source/extensions/common/wasm/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,14 @@ Context::findValue(absl::string_view name, Protobuf::Arena* arena, bool last) co
}
return {};
}

switch (part_token->second) {
case PropertyToken::METADATA:
if (info) {
return CelProtoWrapper::CreateMessage(&info->dynamicMetadata(), arena);
}
break;

case PropertyToken::REQUEST:
if (info) {
return CelValue::CreateMap(Protobuf::Arena::Create<Filters::Common::Expr::RequestWrapper>(
Expand Down Expand Up @@ -587,6 +588,7 @@ WasmResult Context::getProperty(absl::string_view path, std::string* result) {
end = start + path.size();
}
auto part = path.substr(start, end - start);

start = end + 1;

if (first) {
Expand Down Expand Up @@ -1125,13 +1127,57 @@ WasmResult Context::setProperty(absl::string_view path, absl::string_view value)
if (!stream_info) {
return WasmResult::NotFound;
}

bool first = true;
size_t start = 0;
std::string fist_key;
std::string second_key;

while (true) {
if (start >= path.size()) {
break;
}

size_t end = path.find('\0', start);
if (end == absl::string_view::npos) {
end = start + path.size();
}
auto part = path.substr(start, end - start);

start = end + 1;

if (first) {
// top-level identifier
first = false;
fist_key = part;
} else {
if (fist_key == "metadata") {
second_key = part;
ProtobufWkt::Struct metadata_val;
std::string json_string;
absl::StrAppend(&json_string, value);
try {
MessageUtil::loadFromJson(json_string, metadata_val);
} catch (EnvoyException& e) {
ENVOY_LOG(trace, "invalid JSON format");
return WasmResult::BadArgument;
}
stream_info->dynamicMetadata().mutable_filter_metadata()->clear();
stream_info->setDynamicMetadata(second_key, metadata_val);

return WasmResult::Ok;
}
}
}

std::string key;
absl::StrAppend(&key, CelStateKeyPrefix, path);
CelState* state;
if (stream_info->filterState()->hasData<CelState>(key)) {
state = &stream_info->filterState()->getDataMutable<CelState>(key);
} else {
const auto& it = rootContext()->state_prototypes_.find(path);

const CelStatePrototype& prototype =
it == rootContext()->state_prototypes_.end()
? Filters::Common::Expr::DefaultCelStatePrototype::get()
Expand Down