|
3 | 3 | #include <memory> |
4 | 4 |
|
5 | 5 | #include <boost/log/trivial.hpp> |
6 | | -#include <google/protobuf/descriptor.h> |
7 | 6 |
|
8 | 7 | #include <viam/api/common/v1/common.pb.h> |
9 | 8 | #include <viam/api/module/v1/module.pb.h> |
|
14 | 13 | namespace viam { |
15 | 14 | namespace sdk { |
16 | 15 |
|
17 | | -viam::module::v1::HandlerMap HandlerMap_::to_proto() const { |
18 | | - viam::module::v1::HandlerMap proto; |
19 | | - for (const auto& h : this->handles_) { |
| 16 | +void HandlerMap_::add_model(Model model, const RPCSubtype& subtype) { |
| 17 | + handles_[subtype].push_back(std::move(model)); |
| 18 | +} |
| 19 | + |
| 20 | +const std::unordered_map<RPCSubtype, std::vector<Model>>& HandlerMap_::handles() const { |
| 21 | + return handles_; |
| 22 | +} |
| 23 | + |
| 24 | +std::ostream& operator<<(std::ostream& os, const HandlerMap_& hm) { |
| 25 | + for (const auto& kv : hm.handles()) { |
| 26 | + os << "API: " << kv.first.api().to_string() << '\n'; |
| 27 | + for (const Model& model : kv.second) { |
| 28 | + os << "\tModel: " << model.to_string() << '\n'; |
| 29 | + } |
| 30 | + } |
| 31 | + return os; |
| 32 | +} |
| 33 | + |
| 34 | +namespace proto_convert_details { |
| 35 | + |
| 36 | +void to_proto<HandlerMap_>::operator()(const HandlerMap_& self, |
| 37 | + module::v1::HandlerMap* proto) const { |
| 38 | + for (const auto& h : self.handles()) { |
20 | 39 | viam::module::v1::HandlerDefinition hd; |
21 | 40 | for (const auto& model : h.second) { |
22 | | - const std::string m = model.to_string(); |
23 | | - *hd.mutable_models()->Add() = m; |
| 41 | + *hd.mutable_models()->Add() = model.to_string(); |
24 | 42 | } |
| 43 | + |
25 | 44 | viam::robot::v1::ResourceRPCSubtype rpc_subtype; |
26 | | - const Name name(h.first.api(), "", ""); |
27 | | - const viam::common::v1::ResourceName resource_name = v2::to_proto(name); |
28 | | - *rpc_subtype.mutable_subtype() = resource_name; |
| 45 | + |
| 46 | + *rpc_subtype.mutable_subtype() = v2::to_proto(Name(h.first.api(), "", "")); |
29 | 47 | *rpc_subtype.mutable_proto_service() = h.first.proto_service_name(); |
30 | 48 | *hd.mutable_subtype() = rpc_subtype; |
31 | | - *proto.add_handlers() = hd; |
32 | | - } |
33 | | - |
34 | | - return proto; |
35 | | -}; |
36 | 49 |
|
37 | | -HandlerMap_::HandlerMap_() {} |
| 50 | + *proto->add_handlers() = hd; |
| 51 | + } |
| 52 | +} |
38 | 53 |
|
39 | | -// NOLINTNEXTLINE(readability-const-return-type) |
40 | | -const HandlerMap_ HandlerMap_::from_proto(const viam::module::v1::HandlerMap& proto) { |
| 54 | +HandlerMap_ from_proto<module::v1::HandlerMap>::operator()( |
| 55 | + const module::v1::HandlerMap* proto) const { |
41 | 56 | HandlerMap_ hm; |
42 | 57 |
|
43 | 58 | const google::protobuf::RepeatedPtrField<viam::module::v1::HandlerDefinition>& handlers = |
44 | | - proto.handlers(); |
| 59 | + proto->handlers(); |
45 | 60 |
|
46 | 61 | for (const auto& handler : handlers) { |
47 | | - std::vector<Model> models; |
48 | 62 | const viam::common::v1::ResourceName name = handler.subtype().subtype(); |
49 | 63 | const API api(name.namespace_(), name.type(), name.subtype()); |
50 | | - const google::protobuf::DescriptorPool* pool = |
51 | | - google::protobuf::DescriptorPool::generated_pool(); |
52 | | - const google::protobuf::ServiceDescriptor* sd = pool->FindServiceByName(name.type()); |
53 | | - const RPCSubtype handle(api, *sd); |
| 64 | + const RPCSubtype handle(api); |
54 | 65 | for (const auto& mod : handler.models()) { |
55 | 66 | try { |
56 | | - const Model model = Model::from_str(mod); |
57 | | - models.push_back(model); |
58 | | - } catch (std::string error) { // NOLINT |
59 | | - BOOST_LOG_TRIVIAL(error) << "Error processing model " + mod; |
| 67 | + hm.add_model(Model::from_str(mod), handle); |
| 68 | + } catch (const std::exception& ex) { |
| 69 | + BOOST_LOG_TRIVIAL(error) << "Error " << ex.what() << " processing model " + mod; |
60 | 70 | } |
61 | 71 | } |
62 | | - hm.handles_.emplace(handle, models); |
63 | 72 | } |
64 | 73 |
|
65 | 74 | return hm; |
66 | 75 | } |
67 | 76 |
|
68 | | -void HandlerMap_::add_model(Model model, const RPCSubtype& subtype) { |
69 | | - handles_[subtype].push_back(std::move(model)); |
70 | | -} |
71 | | - |
72 | | -std::ostream& operator<<(std::ostream& os, const HandlerMap_& hm) { |
73 | | - for (const auto& kv : hm.handles_) { |
74 | | - os << "API: " << kv.first.api().to_string() << '\n'; |
75 | | - for (const Model& model : kv.second) { |
76 | | - os << "\tModel: " << model.to_string() << '\n'; |
77 | | - } |
78 | | - } |
79 | | - return os; |
80 | | -} |
| 77 | +} // namespace proto_convert_details |
81 | 78 |
|
82 | 79 | } // namespace sdk |
83 | 80 | } // namespace viam |
0 commit comments