Skip to content

Commit 141487f

Browse files
committed
bring ProtoValue into the to_proto/from_proto fold
1 parent 42552ff commit 141487f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+226
-248
lines changed

src/viam/sdk/common/client_helper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ClientHelper {
5959
ProtoValue value = key->second;
6060
debug_key_ = *value.get<std::string>();
6161
}
62-
*request_.mutable_extra() = map_to_struct(extra);
62+
*request_.mutable_extra() = v2::to_proto(extra);
6363
return with(std::forward<RequestSetupCallable>(rsc));
6464
}
6565

src/viam/sdk/common/proto_value.cpp

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,6 @@ ProtoValue::ProtoValue(ProtoValue&& other) noexcept(proto_value_details::all_mov
4343
ProtoValue::ProtoValue(const ProtoValue& other)
4444
: vtable_(other.vtable_), self_(other.self_, other.vtable_) {}
4545

46-
ProtoValue::ProtoValue(const Value* value) // NOLINT(misc-no-recursion)
47-
: ProtoValue([](const Value& v) { // NOLINT(misc-no-recursion)
48-
switch (v.kind_case()) {
49-
case Value::KindCase::kBoolValue: {
50-
return ProtoValue(v.bool_value());
51-
}
52-
case Value::KindCase::kStringValue: {
53-
return ProtoValue(v.string_value());
54-
}
55-
case Value::KindCase::kNumberValue: {
56-
return ProtoValue(v.number_value());
57-
}
58-
case Value::KindCase::kListValue: {
59-
ProtoList vec;
60-
vec.reserve(v.list_value().values_size());
61-
for (const Value& list_val : v.list_value().values()) {
62-
vec.push_back(ProtoValue::from_proto(list_val));
63-
}
64-
65-
return ProtoValue(std::move(vec));
66-
}
67-
case Value::KindCase::kStructValue: {
68-
return ProtoValue(struct_to_map(v.struct_value()));
69-
}
70-
case Value::KindCase::KIND_NOT_SET:
71-
case Value::KindCase::kNullValue:
72-
default:
73-
return ProtoValue(nullptr);
74-
}
75-
}(*value)) {}
76-
7746
ProtoValue& ProtoValue::operator=(ProtoValue&& other) noexcept(
7847
proto_value_details::all_moves_noexcept{}) {
7948
ProtoValue(std::move(other)).swap(*this);
@@ -98,13 +67,6 @@ void ProtoValue::swap(ProtoValue& other) noexcept(proto_value_details::all_moves
9867
std::swap(vtable_, other.vtable_);
9968
}
10069

101-
template <typename Val>
102-
ProtoValue ProtoValue::from_proto(const Val& v) { // NOLINT(misc-no-recursion)
103-
return ProtoValue(&v);
104-
}
105-
106-
template ProtoValue ProtoValue::from_proto(const Value&);
107-
10870
ProtoValue::Kind ProtoValue::kind() const {
10971
return vtable_.kind();
11072
}
@@ -182,8 +144,8 @@ void ProtoValue::model<T>::move(void* self, void* dest) {
182144
}
183145

184146
template <typename T>
185-
void ProtoValue::model<T>::to_proto(void const* self, google::protobuf::Value* v) {
186-
viam::sdk::to_proto(static_cast<model const*>(self)->data, v);
147+
void ProtoValue::model<T>::to_value(void const* self, google::protobuf::Value* v) {
148+
viam::sdk::proto_value_details::to_value(static_cast<model const*>(self)->data, v);
187149
}
188150

189151
template <typename T>
@@ -254,53 +216,94 @@ void ProtoValue::storage::destruct(const ProtoValue::vtable& vtab) noexcept {
254216
vtab.dtor(this->get());
255217
}
256218

257-
void to_proto(std::nullptr_t, Value* v) {
219+
namespace proto_value_details {
220+
221+
void to_value(std::nullptr_t, Value* v) {
258222
v->set_null_value(::google::protobuf::NULL_VALUE);
259223
}
260224

261-
void to_proto(bool b, Value* v) {
225+
void to_value(bool b, Value* v) {
262226
v->set_bool_value(b);
263227
}
264228

265-
void to_proto(double d, Value* v) {
229+
void to_value(double d, Value* v) {
266230
v->set_number_value(d);
267231
}
268232

269-
void to_proto(std::string s, Value* v) {
233+
void to_value(std::string s, Value* v) {
270234
v->set_string_value(std::move(s));
271235
}
272236

273-
void to_proto(const ProtoList& vec, Value* v) {
237+
void to_value(const ProtoList& vec, Value* v) {
274238
::google::protobuf::ListValue l;
275239
for (const auto& val : vec) {
276-
*l.add_values() = to_proto(val);
240+
*l.add_values() = v2::to_proto(val);
277241
}
278242
*(v->mutable_list_value()) = std::move(l);
279243
}
280244

281-
void to_proto(const ProtoStruct& m, Value* v) {
282-
Struct s;
283-
map_to_struct(m, &s);
284-
285-
*(v->mutable_struct_value()) = std::move(s);
245+
void to_value(const ProtoStruct& m, Value* v) {
246+
*(v->mutable_struct_value()) = v2::to_proto(m);
247+
}
248+
249+
} // namespace proto_value_details
250+
251+
namespace proto_convert_details {
252+
253+
void to_proto<ProtoValue>::operator()(const ProtoValue& self, google::protobuf::Value* v) const {
254+
self.vtable_.to_value(self.self_.get(), v);
255+
}
256+
257+
ProtoValue from_proto<google::protobuf::Value>::operator()(
258+
const google::protobuf::Value* v) const { // NOLINT(misc-no-recursion)
259+
switch (v->kind_case()) {
260+
case Value::KindCase::kBoolValue: {
261+
return ProtoValue(v->bool_value());
262+
}
263+
case Value::KindCase::kStringValue: {
264+
return ProtoValue(v->string_value());
265+
}
266+
case Value::KindCase::kNumberValue: {
267+
return ProtoValue(v->number_value());
268+
}
269+
case Value::KindCase::kListValue: {
270+
ProtoList vec;
271+
vec.reserve(v->list_value().values_size());
272+
for (const Value& list_val : v->list_value().values()) {
273+
vec.push_back(v2::from_proto(list_val));
274+
}
275+
276+
return ProtoValue(std::move(vec));
277+
}
278+
case Value::KindCase::kStructValue: {
279+
return ProtoValue(v2::from_proto(v->struct_value()));
280+
}
281+
case Value::KindCase::KIND_NOT_SET:
282+
case Value::KindCase::kNullValue:
283+
default:
284+
return ProtoValue(nullptr);
285+
}
286286
}
287287

288-
void to_proto(const ProtoValue& t, Value* v) {
289-
t.vtable_.to_proto(t.self_.get(), v);
288+
void to_proto<ProtoStruct>::operator()(const ProtoStruct& self, google::protobuf::Struct* s) const {
289+
for (const auto& kv : self) {
290+
s->mutable_fields()->insert(
291+
google::protobuf::MapPair<std::string, Value>(kv.first, v2::to_proto(kv.second)));
292+
}
290293
}
291294

292-
void struct_to_map(Struct const* s, ProtoStruct& map) { // NOLINT(misc-no-recursion)
295+
ProtoStruct from_proto<google::protobuf::Struct>::operator()(
296+
const google::protobuf::Struct* s) const { // NOLINT(misc-no-recursion)
297+
ProtoStruct result;
298+
293299
for (const auto& val : s->fields()) {
294-
map.emplace(val.first, ProtoValue::from_proto(val.second));
300+
result.emplace(val.first, v2::from_proto(val.second));
295301
}
296-
}
297302

298-
void map_to_struct(const ProtoStruct& m, Struct* s) {
299-
for (const auto& kv : m) {
300-
s->mutable_fields()->insert(
301-
google::protobuf::MapPair<std::string, Value>(kv.first, to_proto(kv.second)));
302-
}
303+
return result;
303304
}
304305

306+
} // namespace proto_convert_details
307+
305308
} // namespace sdk
306309
} // namespace viam

src/viam/sdk/common/proto_value.hpp

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <utility>
88
#include <vector>
99

10+
#include <viam/sdk/common/proto_convert.hpp>
11+
1012
namespace google {
1113
namespace protobuf {
1214

@@ -52,6 +54,8 @@ struct all_moves_noexcept
5254
/// definition.
5355
class ProtoValue {
5456
public:
57+
friend proto_convert_details::to_proto<ProtoValue>;
58+
5559
/// @brief Type discriminator constants for possible values stored in a ProtoValue.
5660
enum Kind { k_null = 0, k_bool = 1, k_double = 2, k_string = 3, k_list = 4, k_struct = 5 };
5761

@@ -110,15 +114,6 @@ class ProtoValue {
110114

111115
void swap(ProtoValue& other) noexcept(proto_value_details::all_moves_noexcept{});
112116

113-
/// @brief Construct from proto value
114-
/// @note This method is trivially templated to insulate google::protobuf::Value from our
115-
/// API/ABI. It is meant to be called with no template parameters in a translation unit which
116-
/// includes <google/protobuf/struct.pb.h>
117-
template <typename Value = google::protobuf::Value>
118-
static ProtoValue from_proto(const Value& v); // NOLINT(misc-no-recursion)
119-
120-
friend void to_proto(const ProtoValue& t, google::protobuf::Value* v);
121-
122117
/// @name Value access API
123118
///@{
124119

@@ -178,7 +173,7 @@ class ProtoValue {
178173
void (*dtor)(void*);
179174
void (*copy)(void const*, void*);
180175
void (*move)(void*, void*);
181-
void (*to_proto)(void const*, google::protobuf::Value*);
176+
void (*to_value)(void const*, google::protobuf::Value*);
182177
Kind (*kind)();
183178
bool (*equal_to)(void const*, void const*, const vtable&);
184179
};
@@ -199,13 +194,13 @@ class ProtoValue {
199194
// non-noexcept pointer anyway
200195
static void move(void* self, void* dest);
201196

202-
static void to_proto(void const* self, google::protobuf::Value* v);
197+
static void to_value(void const* self, google::protobuf::Value* v);
203198

204199
static Kind kind() noexcept;
205200

206201
static bool equal_to(void const* self, void const* other, const vtable& other_vtable);
207202

208-
static constexpr vtable vtable_{dtor, copy, move, to_proto, kind, equal_to};
203+
static constexpr vtable vtable_{dtor, copy, move, to_value, kind, equal_to};
209204
T data;
210205
};
211206

@@ -320,55 +315,39 @@ extern template std::string&& ProtoValue::get_unchecked<std::string>() &&;
320315
extern template ProtoList&& ProtoValue::get_unchecked<ProtoList>() &&;
321316
extern template ProtoStruct&& ProtoValue::get_unchecked<ProtoStruct>() &&;
322317

323-
void to_proto(std::nullptr_t, google::protobuf::Value* v);
324-
void to_proto(bool b, google::protobuf::Value* v);
325-
void to_proto(double d, google::protobuf::Value* v);
326-
void to_proto(std::string s, google::protobuf::Value* v);
327-
void to_proto(const ProtoList& vec, google::protobuf::Value* v);
328-
void to_proto(const ProtoStruct& m, google::protobuf::Value* v);
329-
void to_proto(const ProtoValue& t, google::protobuf::Value* v);
330-
331-
void struct_to_map(google::protobuf::Struct const* s, ProtoStruct& m);
332-
void map_to_struct(const ProtoStruct& m, google::protobuf::Struct* s);
333-
334-
/// @brief Convert a type to a google::protobuf::Value.
335-
/// @note This method is trivially templated to insulate google::protobuf::Value from our
336-
/// API/ABI. It is meant to be called with no template parameters in a translation unit which
337-
/// includes <google/protobuf/struct.pb.h>
338-
template <typename Value = google::protobuf::Value>
339-
Value to_proto(const ProtoValue& proto_value) {
340-
Value v;
341-
to_proto(proto_value, &v);
342-
343-
return v;
344-
}
318+
namespace proto_convert_details {
345319

346-
/// @brief Convert a google::protobuf::Struct to a ProtoStruct.
347-
/// @note This method is trivially templated to insulate google::protobuf::Struct from our
348-
/// API/ABI. It is meant to be called with no template parameters in a translation unit which
349-
/// includes <google/protobuf/struct.pb.h>
350-
template <typename Struct = google::protobuf::Struct>
351-
ProtoStruct struct_to_map(const Struct& s) {
352-
ProtoStruct result;
353-
struct_to_map(&s, result);
320+
template <>
321+
struct to_proto<ProtoValue> {
322+
void operator()(const ProtoValue&, google::protobuf::Value*) const;
323+
};
354324

355-
return result;
356-
}
325+
template <>
326+
struct to_proto<ProtoStruct> {
327+
void operator()(const ProtoStruct&, google::protobuf::Struct*) const;
328+
};
357329

358-
/// @brief Convert a ProtoStruct to a google::protobuf::Struct.
359-
/// @note This method is trivially templated to insulate google::protobuf::Struct from our
360-
/// API/ABI. It is meant to be called with no template parameters in a translation unit which
361-
/// includes <google/protobuf/struct.pb.h>
362-
template <typename Struct = google::protobuf::Struct>
363-
Struct map_to_struct(const ProtoStruct& m) {
364-
Struct s;
365-
map_to_struct(m, &s);
330+
template <>
331+
struct from_proto<google::protobuf::Value> {
332+
ProtoValue operator()(const google::protobuf::Value*) const;
333+
};
366334

367-
return s;
368-
}
335+
template <>
336+
struct from_proto<google::protobuf::Struct> {
337+
ProtoStruct operator()(const google::protobuf::Struct*) const;
338+
};
339+
340+
} // namespace proto_convert_details
369341

370342
namespace proto_value_details {
371343

344+
void to_value(std::nullptr_t, google::protobuf::Value* v);
345+
void to_value(bool b, google::protobuf::Value* v);
346+
void to_value(double d, google::protobuf::Value* v);
347+
void to_value(std::string s, google::protobuf::Value* v);
348+
void to_value(const ProtoList& vec, google::protobuf::Value* v);
349+
void to_value(const ProtoStruct& m, google::protobuf::Value* v);
350+
372351
template <typename T>
373352
struct kind {};
374353

src/viam/sdk/common/service_helper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ServiceHelper : public ServiceHelperBase {
4949
}
5050

5151
auto getExtra() const {
52-
return request_->has_extra() ? struct_to_map(request_->extra()) : ProtoStruct{};
52+
return request_->has_extra() ? v2::from_proto(request_->extra()) : ProtoStruct{};
5353
}
5454

5555
private:

src/viam/sdk/components/private/arm_client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ void ArmClient::stop(const ProtoStruct& extra) {
8383

8484
ProtoStruct ArmClient::do_command(const ProtoStruct& command) {
8585
return make_client_helper(this, *stub_, &StubType::DoCommand)
86-
.with([&](auto& request) { *request.mutable_command() = map_to_struct(command); })
87-
.invoke([](auto& response) { return struct_to_map(response.result()); });
86+
.with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); })
87+
.invoke([](auto& response) { return v2::from_proto(response.result()); });
8888
}
8989

9090
Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) {

src/viam/sdk/components/private/arm_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ ::grpc::Status ArmServer::DoCommand(::grpc::ServerContext*,
9898
const ::viam::common::v1::DoCommandRequest* request,
9999
::viam::common::v1::DoCommandResponse* response) noexcept {
100100
return make_service_helper<Arm>("ArmServer::DoCommand", this, request)([&](auto&, auto& arm) {
101-
const ProtoStruct result = arm->do_command(struct_to_map(request->command()));
102-
*response->mutable_result() = map_to_struct(result);
101+
const ProtoStruct result = arm->do_command(v2::from_proto(request->command()));
102+
*response->mutable_result() = v2::to_proto(result);
103103
});
104104
}
105105

src/viam/sdk/components/private/base_client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ Base::properties BaseClient::get_properties(const ProtoStruct& extra) {
9999

100100
ProtoStruct BaseClient::do_command(const ProtoStruct& command) {
101101
return make_client_helper(this, *stub_, &StubType::DoCommand)
102-
.with([&](auto& request) { *request.mutable_command() = map_to_struct(command); })
103-
.invoke([](auto& response) { return struct_to_map(response.result()); });
102+
.with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); })
103+
.invoke([](auto& response) { return v2::from_proto(response.result()); });
104104
}
105105

106106
} // namespace impl

src/viam/sdk/components/private/base_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ ::grpc::Status BaseServer::DoCommand(grpc::ServerContext*,
106106
viam::common::v1::DoCommandResponse* response) noexcept {
107107
return make_service_helper<Base>(
108108
"BaseServer::DoCommand", this, request)([&](auto&, auto& base) {
109-
const ProtoStruct result = base->do_command(struct_to_map(request->command()));
110-
*response->mutable_result() = map_to_struct(result);
109+
const ProtoStruct result = base->do_command(v2::from_proto(request->command()));
110+
*response->mutable_result() = v2::to_proto(result);
111111
});
112112
}
113113

0 commit comments

Comments
 (0)