Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

fixes to filter state getters/setters #486

Merged
merged 2 commits into from
Apr 20, 2020
Merged
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
20 changes: 13 additions & 7 deletions source/extensions/common/wasm/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ static absl::flat_hash_map<std::string, PropertyToken> property_tokens = {PROPER
#undef _PARI

absl::optional<google::api::expr::runtime::CelValue>
Context::FindValue(absl::string_view name, Protobuf::Arena* arena) const {
Context::findValue(absl::string_view name, Protobuf::Arena* arena, bool last) const {
using google::api::expr::runtime::CelValue;

const StreamInfo::StreamInfo* info = getConstRequestStreamInfo();
Expand All @@ -442,11 +442,15 @@ Context::FindValue(absl::string_view name, Protobuf::Arena* arena) const {
const WasmState* state;
if (info->filterState().hasData<WasmState>(key)) {
state = &info->filterState().getDataReadOnly<WasmState>(key);
} else if (info->upstreamFilterState()->hasData<WasmState>(key)) {
} else if (info->upstreamFilterState() &&
info->upstreamFilterState()->hasData<WasmState>(key)) {
state = &info->upstreamFilterState()->getDataReadOnly<WasmState>(key);
} else {
return {};
}
if (last) {
return CelValue::CreateBytes(&state->value());
}
return state->exprValue(arena);
}
return {};
Expand Down Expand Up @@ -573,7 +577,7 @@ WasmResult Context::getProperty(absl::string_view path, std::string* result) {
if (first) {
// top-level ident
first = false;
auto top_value = FindValue(part, &arena);
auto top_value = findValue(part, &arena, start >= path.size());
if (!top_value.has_value()) {
return WasmResult::NotFound;
}
Expand Down Expand Up @@ -1115,12 +1119,14 @@ WasmResult Context::setProperty(absl::string_view path, absl::string_view value)
state = &stream_info->filterState()->getDataMutable<WasmState>(key);
} else {
const auto& it = rootContext()->state_prototypes_.find(path);
auto state_ptr = std::make_unique<WasmState>(it == rootContext()->state_prototypes_.end()
? DefaultWasmStatePrototype::get()
: *it->second.get());
const WasmStatePrototype& prototype = it == rootContext()->state_prototypes_.end()
? DefaultWasmStatePrototype::get()
: *it->second.get();
auto state_ptr = std::make_unique<WasmState>(prototype);
state = state_ptr.get();
stream_info->filterState()->setData(key, std::move(state_ptr),
StreamInfo::FilterState::StateType::ReadOnly);
StreamInfo::FilterState::StateType::Mutable,
prototype.life_span_);
}
if (!state->setValue(value)) {
return WasmResult::BadArgument;
Expand Down
6 changes: 5 additions & 1 deletion source/extensions/common/wasm/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ class Context : public proxy_wasm::ContextBase,
return {};
}
absl::optional<google::api::expr::runtime::CelValue>
FindValue(absl::string_view name, Protobuf::Arena* arena) const override;
findValue(absl::string_view name, Protobuf::Arena* arena, bool last) const;
absl::optional<google::api::expr::runtime::CelValue>
FindValue(absl::string_view name, Protobuf::Arena* arena) const override {
return findValue(name, arena, false);
}
bool IsPathUnknown(absl::string_view) const override { return false; }
const std::vector<google::api::expr::runtime::CelAttributePattern>&
unknown_attribute_patterns() const override {
Expand Down