Skip to content

Commit 7470993

Browse files
authored
Add overloads for vector property paths. (#56)
Signed-off-by: John Plevyak <[email protected]>
1 parent 1b5f69c commit 7470993

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

proxy_wasm_api.h

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,33 @@ inline std::pair<uint32_t, WasmDataPtr> getStatus() {
531531
}
532532

533533
// Generic selector
534-
inline std::optional<WasmDataPtr> getProperty(std::initializer_list<std::string_view> parts) {
534+
inline std::optional<WasmDataPtr>
535+
getProperty(const std::initializer_list<std::string_view> &parts) {
536+
size_t size = 0;
537+
for (auto part : parts) {
538+
size += part.size() + 1; // null terminated string value
539+
}
540+
541+
char *buffer = static_cast<char *>(::malloc(size));
542+
char *b = buffer;
543+
544+
for (auto part : parts) {
545+
memcpy(b, part.data(), part.size());
546+
b += part.size();
547+
*b++ = 0;
548+
}
549+
550+
const char *value_ptr = nullptr;
551+
size_t value_size = 0;
552+
auto result = proxy_get_property(buffer, size, &value_ptr, &value_size);
553+
::free(buffer);
554+
if (result != WasmResult::Ok) {
555+
return {};
556+
}
557+
return std::make_unique<WasmData>(value_ptr, value_size);
558+
}
559+
560+
template <typename S> inline std::optional<WasmDataPtr> getProperty(const std::vector<S> &parts) {
535561
size_t size = 0;
536562
for (auto part : parts) {
537563
size += part.size() + 1; // null terminated string value
@@ -560,7 +586,8 @@ inline std::optional<WasmDataPtr> getProperty(std::initializer_list<std::string_
560586
// Durations are represented as int64 nanoseconds.
561587
// Timestamps are represented as int64 Unix nanoseconds.
562588
// Strings and bytes are represented as std::string.
563-
template <typename T> inline bool getValue(std::initializer_list<std::string_view> parts, T *out) {
589+
template <typename T>
590+
inline bool getValue(const std::initializer_list<std::string_view> &parts, T *out) {
564591
auto buf = getProperty(parts);
565592
if (!buf.has_value() || buf.value()->size() != sizeof(T)) {
566593
return false;
@@ -571,7 +598,39 @@ template <typename T> inline bool getValue(std::initializer_list<std::string_vie
571598

572599
// Specialization for bytes and string values
573600
template <>
574-
inline bool getValue<std::string>(std::initializer_list<std::string_view> parts, std::string *out) {
601+
inline bool getValue<std::string>(const std::initializer_list<std::string_view> &parts,
602+
std::string *out) {
603+
auto buf = getProperty(parts);
604+
if (!buf.has_value()) {
605+
return false;
606+
}
607+
out->assign(buf.value()->data(), buf.value()->size());
608+
return true;
609+
}
610+
611+
template <typename S, typename T> inline bool getValue(const std::vector<S> &parts, T *out) {
612+
auto buf = getProperty(parts);
613+
if (!buf.has_value() || buf.value()->size() != sizeof(T)) {
614+
return false;
615+
}
616+
*out = *reinterpret_cast<const T *>(buf.value()->data());
617+
return true;
618+
}
619+
620+
template <>
621+
inline bool getValue<std::string, std::string>(const std::vector<std::string> &parts,
622+
std::string *out) {
623+
auto buf = getProperty(parts);
624+
if (!buf.has_value()) {
625+
return false;
626+
}
627+
out->assign(buf.value()->data(), buf.value()->size());
628+
return true;
629+
}
630+
631+
template <>
632+
inline bool getValue<std::string_view, std::string>(const std::vector<std::string_view> &parts,
633+
std::string *out) {
575634
auto buf = getProperty(parts);
576635
if (!buf.has_value()) {
577636
return false;
@@ -582,7 +641,20 @@ inline bool getValue<std::string>(std::initializer_list<std::string_view> parts,
582641

583642
// Specialization for message types (including struct value for lists and maps)
584643
template <typename T>
585-
inline bool getMessageValue(std::initializer_list<std::string_view> parts, T *value_ptr) {
644+
inline bool getMessageValue(const std::initializer_list<std::string_view> &parts, T *value_ptr) {
645+
auto buf = getProperty(parts);
646+
if (!buf.has_value()) {
647+
return false;
648+
}
649+
if (buf.value()->size() == 0) {
650+
value_ptr = nullptr;
651+
return true;
652+
}
653+
return value_ptr->ParseFromArray(buf.value()->data(), buf.value()->size());
654+
}
655+
656+
template <typename S, typename T>
657+
inline bool getMessageValue(const std::vector<S> &parts, T *value_ptr) {
586658
auto buf = getProperty(parts);
587659
if (!buf.has_value()) {
588660
return false;

0 commit comments

Comments
 (0)