Skip to content

Commit

Permalink
wasm: friendlier resolver API (envoyproxy#191)
Browse files Browse the repository at this point in the history
Signed-off-by: Kuat Yessenov <[email protected]>
  • Loading branch information
kyessenov authored and jplevyak committed Sep 16, 2019
1 parent fee28ed commit 16d41f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
28 changes: 23 additions & 5 deletions api/wasm/cpp/proxy_wasm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,26 @@ inline Optional<WasmDataPtr> getSelectorExpression(std::initializer_list<StringV
return std::make_unique<WasmData>(value_ptr, value_size);
}

inline bool getStringValue(std::initializer_list<StringView> parts, std::string* out) {
auto buf = getSelectorExpression(parts);
if (!buf.has_value()) {
return false;
}
out->assign(buf.value()->data(), buf.value()->size());
return true;
}

inline WasmResult getRequestProtocol(std::string *result) {
auto value = getSelectorExpression({"request_protocol"});
if (value.has_value()) {
result->assign(value.value()->data(), value.value()->size());
return WasmResult::Ok;
return getStringValue({"request_protocol"}, result) ? WasmResult::Ok : WasmResult::NotFound;
}

template <typename T> inline bool getValue(std::initializer_list<StringView> parts, T* out) {
auto buf = getSelectorExpression(parts);
if (!buf.has_value() || buf.value()->size() != sizeof(T)) {
return false;
}
return WasmResult::NotFound;
*out = *reinterpret_cast<const T *>(buf.value()->data());
return true;
}

// Metadata
Expand Down Expand Up @@ -579,6 +592,11 @@ inline WasmResult getMetadataStringValue(MetadataType type, StringView key,
return result;
}

// Requires that the value is a serialized google.protobuf.Value.
inline WasmResult setFilterState(StringView key, StringView value) {
return static_cast<WasmResult>(proxy_setState(key.data(), key.size(), value.data(), value.size()));
}

inline WasmResult setFilterStateValue(StringView key, const google::protobuf::Value& value) {
std::string output;
if (!value.SerializeToString(&output)) {
Expand Down
9 changes: 3 additions & 6 deletions source/extensions/common/wasm/null/sample_plugin/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ void PluginContext::onLog() {
{"metadata", "filter_metadata", "envoy.filters.http.wasm", "wasm_request_get_key"})
.value()
->toString());
auto responseCode = getSelectorExpression({"response", "code"}).value();
if (responseCode->size() == sizeof(int64_t)) {
char buf[sizeof(int64_t)];
responseCode->view().copy(buf, sizeof(int64_t), 0);
int64_t code = absl::bit_cast<int64_t>(buf);
logWarn("response.code: " + absl::StrCat(code));
int64_t responseCode;
if (getValue({"response", "code"}, &responseCode)) {
logWarn("response.code: " + absl::StrCat(responseCode));
}
logWarn("state: " + getSelectorExpression({"filter_state", "wasm_state"}).value()->toString());
} else {
Expand Down

0 comments on commit 16d41f2

Please sign in to comment.