[ 
https://issues.apache.org/jira/browse/MESOS-3580?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Klaus Ma updated MESOS-3580:
----------------------------
    Assignee:     (was: Klaus Ma)

> Refactor 'protobuf' to accept project function
> ----------------------------------------------
>
>                 Key: MESOS-3580
>                 URL: https://issues.apache.org/jira/browse/MESOS-3580
>             Project: Mesos
>          Issue Type: Bug
>          Components: stout
>            Reporter: Klaus Ma
>              Labels: refactor, stout
>
> During the code review of MESOS-3405, [~alex-mesos] realized that we use the 
> same pattern again and again, for example:
> {code}
>     JSON::Array array;
>     array.values.reserve(status.network_infos().size()); // MESOS-2353.
>     foreach (const NetworkInfo& info, status.network_infos()) {
>       array.values.push_back(model(info));
>     }
>     object.values["network_infos"] = std::move(array);
> {code}
> We cannot use newly added {{JSON::protobuf()}} here, because a different way 
> for rendering JSON from protobuf is used. Without digging deep inside, I know 
> three ways how we create a JSON out of a proto in our codebase:
> - wrap in {{JSON::Protobuf()}} for individual messages;
> - wrap in one of the model() family functions;
> - pass as it is for built-in types.
> The proposed conversion function covers one of the possible ways. How about 
> add one more convertion? Something like:
> {code}
> template <typename T>
> Array protobuf(const google::protobuf::RepeatedPtrField<T>& repeated,
>     const lambda::function<JSON::Object>(const T&)>& converter)
> {
>   static_assert(std::is_convertible<T*, google::protobuf::Message*>::value,
>                 "T must be a google::protobuf::Message");
>   JSON::Array array;
>   array.values.reserve(repeated.size());
>   foreach (const T& elem, repeated) {
>     array.values.push_back(converter(elem));
>   }
>   return array;
> }
> {code}
> Then the snippet above could be rewritten as:
> {code}
> object.values["network_infos"] = 
> std::move(JSON::protobuf(status.network_infos(),
>     [](const NetworkInfo& info) { return model(info); });
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to