mr_pb.h
1 // Copyright 2019, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 
5 #pragma once
6 
7 #include <google/protobuf/message.h>
8 
9 #include "mr/do_context.h"
10 
11 namespace mr3 {
12 
13 struct PB_Serializer {
14  using Message = google::protobuf::Message;
15 
16  static std::string To(bool is_binary, const Message* msg);
17 
18  // Need std::string on stack because of json2pb which requires mutable string for insitu parsing.
19  static bool From(bool is_binary, std::string tmp, Message* res);
20 };
21 
22 template <typename PB>
23 class RecordTraits<PB, std::enable_if_t<std::is_base_of<google::protobuf::Message, PB>::value>>
24  : public PB_Serializer {
25  public:
26  static std::string Serialize(bool is_binary, const PB& doc) {
27  return PB_Serializer::To(is_binary, &doc);
28  }
29 
30  static bool Parse(bool is_binary, std::string tmp, PB* res) {
31  return PB_Serializer::From(is_binary, std::move(tmp), res);
32  }
33 
34  static std::string TypeName() {
35  PB msg;
36  return msg.GetTypeName();
37  }
38 };
39 } // namespace mr3