Skip to content

Commit

Permalink
cherry-pick of envoyproxy/envoy-wasm#486
Browse files Browse the repository at this point in the history
Signed-off-by: Kuat Yessenov <kuat@google.com>
  • Loading branch information
kyessenov committed Apr 20, 2020
1 parent aba3cf3 commit 4c61d7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
20 changes: 13 additions & 7 deletions source/extensions/common/wasm/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,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 @@ -411,11 +411,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 @@ -542,7 +546,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 @@ -1066,12 +1070,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
12 changes: 8 additions & 4 deletions source/extensions/common/wasm/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,17 @@ class Context : public Logger::Loggable<Logger::Id::wasm>,
bool end_stream); // stream only

// CEL evaluation
virtual std::vector<const google::api::expr::runtime::CelFunction*>
std::vector<const google::api::expr::runtime::CelFunction*>
FindFunctionOverloads(absl::string_view) const override {
return {};
}
virtual absl::optional<google::api::expr::runtime::CelValue>
FindValue(absl::string_view name, Protobuf::Arena* arena) const override;
virtual bool IsPathUnknown(absl::string_view) const override { return false; }
absl::optional<google::api::expr::runtime::CelValue>
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 {
static const std::vector<google::api::expr::runtime::CelAttributePattern> empty;
Expand Down

0 comments on commit 4c61d7a

Please sign in to comment.