forked from EliLillyCo/LillyMol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e13ee26
commit 36334ac
Showing
6 changed files
with
235 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
syntax = "proto3"; | ||
|
||
package for_testing; | ||
|
||
message SubMessage { | ||
optional int32 i1 = 1; | ||
optional string str1 = 2; | ||
} | ||
|
||
message TestMessage { | ||
optional string str1 = 1; | ||
optional string str2 = 2; | ||
|
||
optional int32 i1 = 3; | ||
optional uint32 ui1 = 4; | ||
optional float x = 5; | ||
|
||
repeated int32 int_array = 6; | ||
repeated float float_array = 7; | ||
|
||
repeated string repeated_string = 8; | ||
|
||
optional SubMessage sub_message = 9; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#ifndef FOUNDATIONAL_IWMISC_TOML_SUPPORT_H_ | ||
#define FOUNDATIONAL_IWMISC_TOML_SUPPORT_H_ | ||
|
||
#include <iostream> | ||
#include <optional> | ||
#include <string> | ||
#include <sstream> | ||
|
||
#define TOML_EXCEPTIONS 0 // only necessary if you've left them enabled in your compiler | ||
#define TOML_ENABLE_FORMATTERS 1 | ||
|
||
#include "absl/status/status.h" | ||
|
||
#include "toml.hpp" | ||
#include "google/protobuf/util/json_util.h" | ||
|
||
#include "Foundational/iwstring/iwstring.h" | ||
|
||
namespace iwmisc { | ||
|
||
|
||
template <typename Proto> | ||
std::optional<Proto> | ||
TomlToProto(toml::table& tbl) { | ||
std::stringstream ss; | ||
ss << toml::json_formatter{tbl}; | ||
const std::string as_json = ss.str(); | ||
|
||
Proto proto; | ||
|
||
google::protobuf::util::JsonParseOptions options; | ||
auto status = google::protobuf::util::JsonStringToMessage(as_json, &proto, options); | ||
|
||
// cannot figure out how to do error checking properly. I never really understood Status | ||
// while I was at Google, still don't. | ||
if (status != absl::OkStatus()) { | ||
std::cerr << "TomlToProto:cannot parse JSON\n"; | ||
std::cerr << as_json << '\n'; | ||
return std::nullopt; | ||
} | ||
|
||
return proto; | ||
} | ||
|
||
template <typename Proto> | ||
std::optional<Proto> | ||
ReadTomlProto(const IWString& fname) { | ||
const std::string string_fname(fname.data(), fname.length()); | ||
|
||
auto as_toml = toml::parse_file(string_fname); | ||
if (! as_toml) { | ||
std::cerr << "ReadTomlProto:parse_file failed " << string_fname << '\n'; | ||
return std::nullopt; | ||
} | ||
|
||
return TomlToProto<Proto>(std::move(as_toml)); | ||
} | ||
|
||
template <typename Proto> | ||
std::optional<Proto> | ||
ParseFromToml(const std::string& toml_string) { | ||
auto as_toml = toml::parse(toml_string); | ||
if (! as_toml) { | ||
std::cerr << "ParseFromToml:Cannot parse as TOML\n"; | ||
std::cerr << toml_string << '\n'; | ||
return std::nullopt; | ||
} | ||
|
||
return TomlToProto<Proto>(std::move(as_toml)); | ||
} | ||
|
||
} | ||
|
||
#endif // FOUNDATIONAL_IWMISC_TOML_SUPPORT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Unit tests | ||
|
||
#include <optional> | ||
|
||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "google/protobuf/text_format.h" | ||
|
||
#include "Foundational/iwmisc/toml_support.h" | ||
|
||
#include "Foundational/iwmisc/for_testing.pb.h" | ||
|
||
namespace { | ||
|
||
using testing::ElementsAre; | ||
using testing::Eq; | ||
|
||
using for_testing::TestMessage; | ||
|
||
TEST(Test, TestStrings) { | ||
const std::string toml = R"( | ||
str1 = "hello" | ||
str2 = "world" | ||
)"; | ||
std::optional<TestMessage> msg = iwmisc::ParseFromToml<TestMessage>(toml); | ||
EXPECT_NE(msg, std::nullopt); | ||
|
||
EXPECT_EQ(msg->str1(), "hello"); | ||
EXPECT_EQ(msg->str2(), "world"); | ||
} | ||
|
||
|
||
TEST(Test, TestInt) { | ||
const std::string toml = R"( | ||
i1 = -12 | ||
)"; | ||
std::optional<TestMessage> msg = iwmisc::ParseFromToml<TestMessage>(toml); | ||
EXPECT_NE(msg, std::nullopt); | ||
|
||
EXPECT_EQ(msg->i1(), -12); | ||
} | ||
|
||
TEST(Test, TestUInt) { | ||
const std::string toml = R"( | ||
ui1 = 12 | ||
)"; | ||
std::optional<TestMessage> msg = iwmisc::ParseFromToml<TestMessage>(toml); | ||
EXPECT_NE(msg, std::nullopt); | ||
|
||
EXPECT_EQ(msg->ui1(), 12); | ||
} | ||
|
||
TEST(Test, TestFloat) { | ||
const std::string toml = R"( | ||
x = 12.0 | ||
)"; | ||
std::optional<TestMessage> msg = iwmisc::ParseFromToml<TestMessage>(toml); | ||
EXPECT_NE(msg, std::nullopt); | ||
|
||
EXPECT_FLOAT_EQ(msg->x(), static_cast<float>(12.0)); | ||
} | ||
|
||
TEST(Test, TestIntArray) { | ||
const std::string toml = R"( | ||
int_array = [1,2,3,4,5] | ||
)"; | ||
std::optional<TestMessage> msg = iwmisc::ParseFromToml<TestMessage>(toml); | ||
EXPECT_NE(msg, std::nullopt); | ||
|
||
EXPECT_THAT(msg->int_array(), ElementsAre(1,2,3,4,5)); | ||
} | ||
|
||
TEST(Test, TestSubMessage) { | ||
const std::string toml = R"( | ||
repeated_string = ["hello", "world"] | ||
[sub_message] | ||
i1 = 3 | ||
str1 = "vr46" | ||
)"; | ||
std::optional<TestMessage> msg = iwmisc::ParseFromToml<TestMessage>(toml); | ||
EXPECT_NE(msg, std::nullopt); | ||
|
||
EXPECT_THAT(msg->repeated_string(), ElementsAre("hello", "world")); | ||
EXPECT_THAT(msg->sub_message().i1(), Eq(3)); | ||
EXPECT_THAT(msg->sub_message().str1(), Eq("vr46")); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters