diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2024-01-31 10:36:25 +0100 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2024-01-31 17:00:52 +0100 |
commit | 948c12af73f357e9ed84d009b48a2ca6e6112b32 (patch) | |
tree | 86b3b79b37cd8a836999df77f20dd42734e5c1f4 | |
parent | 8a4a900f93a704f18d5489b682508840a8cda179 (diff) | |
download | justbuild-948c12af73f357e9ed84d009b48a2ca6e6112b32.tar.gz |
dispatch file parsing: Improve std::variant usage
-rw-r--r-- | src/buildtool/common/remote/remote_common.hpp | 64 | ||||
-rw-r--r-- | src/buildtool/serve_api/serve_service/target.cpp | 28 |
2 files changed, 39 insertions, 53 deletions
diff --git a/src/buildtool/common/remote/remote_common.hpp b/src/buildtool/common/remote/remote_common.hpp index f48f7fbc..70c1b6cb 100644 --- a/src/buildtool/common/remote/remote_common.hpp +++ b/src/buildtool/common/remote/remote_common.hpp @@ -75,69 +75,61 @@ ParseDispatch(std::string const& dispatch_info) noexcept -> std::variant< try { dispatch = nlohmann::json::parse(dispatch_info); } catch (std::exception const& e) { - return result_t( - std::in_place_index<0>, - fmt::format("Failed to parse endpoint configuration: {}", - e.what())); + return fmt::format("Failed to parse endpoint configuration: {}", + e.what()); } std::vector<std::pair<std::map<std::string, std::string>, ServerAddress>> parsed{}; try { if (not dispatch.is_array()) { - return result_t(std::in_place_index<0>, - fmt::format("Endpoint configuration has to be a " - "list of pairs, but found {}", - dispatch.dump())); + return fmt::format( + "Endpoint configuration has to be a " + "list of pairs, but found {}", + dispatch.dump()); } for (auto const& entry : dispatch) { if (not(entry.is_array() and entry.size() == 2)) { - return result_t( - std::in_place_index<0>, - fmt::format("Endpoint configuration has to be a list of " - "pairs, but found entry {}", - entry.dump())); + return fmt::format( + "Endpoint configuration has to be a list of " + "pairs, but found entry {}", + entry.dump()); } if (not entry[0].is_object()) { - return result_t(std::in_place_index<0>, - fmt::format("Property condition has to be " - "given as an object, but found {}", - entry[0].dump())); + return fmt::format( + "Property condition has to be " + "given as an object, but found {}", + entry[0].dump()); } std::map<std::string, std::string> props{}; for (auto const& [k, v] : entry[0].items()) { if (not v.is_string()) { - return result_t( - std::in_place_index<0>, - fmt::format("Property condition has to be given as an " - "object of strings but found {}", - entry[0].dump())); + return fmt::format( + "Property condition has to be given as an " + "object of strings but found {}", + entry[0].dump()); } props.emplace(k, v.template get<std::string>()); } if (not entry[1].is_string()) { - return result_t( - std::in_place_index<0>, - fmt::format("Endpoint has to be specified as string (in " - "the form host:port), but found {}", - entry[1].dump())); + return fmt::format( + "Endpoint has to be specified as string (in " + "the form host:port), but found {}", + entry[1].dump()); } auto endpoint = ParseAddress(entry[1].template get<std::string>()); if (not endpoint) { - return result_t(std::in_place_index<0>, - fmt::format("Failed to parse {} as endpoint.", - entry[1].dump())); + return fmt::format("Failed to parse {} as endpoint.", + entry[1].dump()); } parsed.emplace_back(props, *endpoint); } } catch (std::exception const& e) { - return result_t( - std::in_place_index<0>, - fmt::format("Failure analysing endpoint configuration {}: {}", - dispatch.dump(), - e.what())); + return fmt::format("Failure analysing endpoint configuration {}: {}", + dispatch.dump(), + e.what()); } // success! - return result_t(std::in_place_index<1>, std::move(parsed)); + return parsed; } #endif // INCLUDED_SRC_BUILDTOOL_COMMON_REMOTE_ADDRESS_HPP diff --git a/src/buildtool/serve_api/serve_service/target.cpp b/src/buildtool/serve_api/serve_service/target.cpp index 70373e05..04db8162 100644 --- a/src/buildtool/serve_api/serve_service/target.cpp +++ b/src/buildtool/serve_api/serve_service/target.cpp @@ -38,39 +38,33 @@ auto TargetService::GetDispatchList(ArtifactDigest const& dispatch_digest) -> std::variant<::grpc::Status, dispatch_t> { - using result_t = std::variant<::grpc::Status, dispatch_t>; // get blob from remote cas auto const& dispatch_info = Artifact::ObjectInfo{.digest = dispatch_digest, .type = ObjectType::File}; if (!local_api_->IsAvailable(dispatch_digest) and !remote_api_->RetrieveToCas({dispatch_info}, &*local_api_)) { - return result_t( - std::in_place_index<0>, - ::grpc::Status{::grpc::StatusCode::FAILED_PRECONDITION, - fmt::format("Could not retrieve from " - "remote-execution end point blob {}", - dispatch_info.ToString())}); + return ::grpc::Status{::grpc::StatusCode::FAILED_PRECONDITION, + fmt::format("Could not retrieve from " + "remote-execution endpoint blob {}", + dispatch_info.ToString())}; } // get blob content auto const& dispatch_str = local_api_->RetrieveToMemory(dispatch_info); if (not dispatch_str) { // this should not fail unless something really broke... - return result_t( - std::in_place_index<0>, - ::grpc::Status{ - ::grpc::StatusCode::INTERNAL, - fmt::format("Unexpected failure in retrieving blob {} from CAS", - dispatch_info.ToString())}); + return ::grpc::Status{ + ::grpc::StatusCode::INTERNAL, + fmt::format("Unexpected failure in retrieving blob {} from CAS", + dispatch_info.ToString())}; } // parse content auto parsed = ParseDispatch(*dispatch_str); if (parsed.index() == 0) { // pass the parsing error forward - return result_t(std::in_place_index<0>, - ::grpc::Status{::grpc::StatusCode::FAILED_PRECONDITION, - std::get<0>(parsed)}); + return ::grpc::Status{::grpc::StatusCode::FAILED_PRECONDITION, + std::get<0>(parsed)}; } - return result_t(std::in_place_index<1>, std::get<1>(parsed)); + return std::get<1>(parsed); } auto TargetService::ServeTarget( |