farm-ng-core
proto_reader_writer.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
19 
20 #include <google/protobuf/text_format.h>
21 #include <google/protobuf/util/json_util.h>
22 
23 #include <filesystem>
24 #include <fstream>
25 
26 namespace farm_ng {
27 
28 /// TODO: Add API docs.
29 
30 Expected<Success> writeProtobufToJsonFile(
31  std::filesystem::path const& path, google::protobuf::Message const& proto);
32 
33 Expected<Success> writeProtobufToBinaryFile(
34  std::filesystem::path const& path, google::protobuf::Message const& proto);
35 
36 Expected<std::string> readJsonStringFromJsonFile(
37  std::filesystem::path const& path);
38 
39 template <class TProtobufT>
41  std::filesystem::path const& path) {
42  FARM_TRY(std::string, json_string, readJsonStringFromJsonFile(path));
43  TProtobufT message;
44  google::protobuf::util::JsonParseOptions parse_options; // default
45  auto status = google::protobuf::util::JsonStringToMessage(
46  json_string, &message, parse_options);
47  if (!status.ok()) {
48  return FARM_UNEXPECTED("Failed to parse json string: {}", status);
49  }
50  return message;
51 }
52 
53 Expected<std::string> readBytesFromBinaryFile(
54  std::filesystem::path const& path);
55 
56 template <class TProtobufT>
58  std::filesystem::path const& path) {
59  TProtobufT message;
60  FARM_TRY(std::string, bytes, readBytesFromBinaryFile(path));
61  if (message.ParseFromString(bytes)) {
62  return FARM_UNEXPECTED("Failed to parse {}", path.string());
63  }
64  return message;
65 }
66 
67 } // namespace farm_ng
farm_ng::writeProtobufToJsonFile
auto writeProtobufToJsonFile(std::filesystem::path const &path, google::protobuf::Message const &proto) -> Expected< farm_ng::Success >
TODO: Add API docs.
Definition: proto_reader_writer.cpp:19
farm_ng
Definition: backtrace.cpp:102
farm_ng::readProtobufFromBinaryFile
Expected< TProtobufT > readProtobufFromBinaryFile(std::filesystem::path const &path)
Definition: proto_reader_writer.h:57
FARM_TRY
#define FARM_TRY(Type, var, expression)
Assigns *expression to var of Type, but returns error if there is one.
Definition: expected.h:91
logger.h
expected.h
farm_ng::readJsonStringFromJsonFile
auto readJsonStringFromJsonFile(std::filesystem::path const &path) -> Expected< std::string >
Definition: proto_reader_writer.cpp:56
message
cmake message("farm_ng_cmake_DIR" ${farm_ng_cmake_DIR}) include($
Definition: CMakeLists.txt:8
farm_ng::readBytesFromBinaryFile
auto readBytesFromBinaryFile(std::filesystem::path const &path) -> Expected< std::string >
Definition: proto_reader_writer.cpp:73
FARM_UNEXPECTED
#define FARM_UNEXPECTED(cstr,...)
Definition: expected.h:86
farm_ng::writeProtobufToBinaryFile
auto writeProtobufToBinaryFile(std::filesystem::path const &path, google::protobuf::Message const &proto) -> Expected< Success >
Definition: proto_reader_writer.cpp:39
farm_ng::readProtobufFromJsonFile
Expected< TProtobufT > readProtobufFromJsonFile(std::filesystem::path const &path)
Definition: proto_reader_writer.h:40
farm_ng::Expected
tl::expected< TT, TE > Expected
Definition: expected.h:37