This is an automated email from the ASF dual-hosted git repository. chhsiao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mesos.git
commit f9cf1c5706093e0c05b2555e5bd03da4011dd5db Author: Chun-Hung Hsiao <chhs...@mesosphere.io> AuthorDate: Thu Apr 4 15:37:35 2019 -0700 Added helpers to evolve/devolve repeated CSI v0 `VolumeCapability`s. Review: https://reviews.apache.org/r/70399 --- src/csi/v0_utils.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- src/csi/v0_utils.hpp | 27 +++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/csi/v0_utils.cpp b/src/csi/v0_utils.cpp index a95d240..248e417 100644 --- a/src/csi/v0_utils.cpp +++ b/src/csi/v0_utils.cpp @@ -16,12 +16,25 @@ #include "csi/v0_utils.hpp" -#include <stout/unreachable.hpp> +using google::protobuf::RepeatedPtrField; namespace mesos { namespace csi { namespace v0 { +// Helper for repeated field devolving to `T1` from `T2`. +template <typename T1, typename T2> +RepeatedPtrField<T1> devolve(RepeatedPtrField<T2> from) +{ + RepeatedPtrField<T1> to; + foreach (const T2& value, from) { + *to.Add() = devolve(value); + } + + return to; +} + + types::VolumeCapability::BlockVolume devolve( const VolumeCapability::BlockVolume& block) { @@ -112,6 +125,26 @@ types::VolumeCapability devolve(const VolumeCapability& capability) } +RepeatedPtrField<types::VolumeCapability> devolve( + const RepeatedPtrField<VolumeCapability>& capabilities) +{ + return devolve<types::VolumeCapability>(capabilities); +} + + +// Helper for repeated field evolving to `T1` from `T2`. +template <typename T1, typename T2> +RepeatedPtrField<T1> evolve(RepeatedPtrField<T2> from) +{ + RepeatedPtrField<T1> to; + foreach (const T2& value, from) { + *to.Add() = evolve(value); + } + + return to; +} + + VolumeCapability::BlockVolume evolve( const types::VolumeCapability::BlockVolume& block) { @@ -197,6 +230,13 @@ VolumeCapability evolve(const types::VolumeCapability& capability) return result; } + +RepeatedPtrField<VolumeCapability> devolve( + const RepeatedPtrField<types::VolumeCapability>& capabilities) +{ + return evolve<VolumeCapability>(capabilities); +} + } // namespace v0 { } // namespace csi { } // namespace mesos { diff --git a/src/csi/v0_utils.hpp b/src/csi/v0_utils.hpp index 46a5f13..468b416 100644 --- a/src/csi/v0_utils.hpp +++ b/src/csi/v0_utils.hpp @@ -17,6 +17,8 @@ #ifndef __CSI_V0_UTILS_HPP__ #define __CSI_V0_UTILS_HPP__ +#include <google/protobuf/message.h> + #include <mesos/csi/types.hpp> #include <mesos/csi/v0.hpp> @@ -31,7 +33,8 @@ struct PluginCapabilities { PluginCapabilities() = default; - template <typename Iterable> PluginCapabilities(const Iterable& capabilities) + template <typename Iterable> + PluginCapabilities(const Iterable& capabilities) { foreach (const auto& capability, capabilities) { if (capability.has_service() && @@ -43,6 +46,10 @@ struct PluginCapabilities case PluginCapability::Service::CONTROLLER_SERVICE: controllerService = true; break; + + // NOTE: We avoid using a default clause for the following values in + // proto3's open enum to enable the compiler to detect missing enum + // cases for us. See: https://github.com/google/protobuf/issues/3917 case google::protobuf::kint32min: case google::protobuf::kint32max: UNREACHABLE(); @@ -81,6 +88,10 @@ struct ControllerCapabilities case ControllerServiceCapability::RPC::GET_CAPACITY: getCapacity = true; break; + + // NOTE: We avoid using a default clause for the following values in + // proto3's open enum to enable the compiler to detect missing enum + // cases for us. See: https://github.com/google/protobuf/issues/3917 case google::protobuf::kint32min: case google::protobuf::kint32max: UNREACHABLE(); @@ -100,7 +111,8 @@ struct NodeCapabilities { NodeCapabilities() = default; - template <typename Iterable> NodeCapabilities(const Iterable& capabilities) + template <typename Iterable> + NodeCapabilities(const Iterable& capabilities) { foreach (const auto& capability, capabilities) { if (capability.has_rpc() && @@ -111,6 +123,10 @@ struct NodeCapabilities case NodeServiceCapability::RPC::STAGE_UNSTAGE_VOLUME: stageUnstageVolume = true; break; + + // NOTE: We avoid using a default clause for the following values in + // proto3's open enum to enable the compiler to detect missing enum + // cases for us. See: https://github.com/google/protobuf/issues/3917 case google::protobuf::kint32min: case google::protobuf::kint32max: UNREACHABLE(); @@ -126,10 +142,17 @@ struct NodeCapabilities // Helpers to devolve CSI v0 protobufs to their unversioned counterparts. types::VolumeCapability devolve(const VolumeCapability& capability); +google::protobuf::RepeatedPtrField<types::VolumeCapability> devolve( + const google::protobuf::RepeatedPtrField<VolumeCapability>& capabilities); + // Helpers to evolve unversioned CSI protobufs to their v0 counterparts. VolumeCapability evolve(const types::VolumeCapability& capability); +google::protobuf::RepeatedPtrField<VolumeCapability> evolve( + const google::protobuf::RepeatedPtrField<types::VolumeCapability>& + capabilities); + } // namespace v0 { } // namespace csi { } // namespace mesos {