szaszm commented on code in PR #1373:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1373#discussion_r929381281


##########
extensions/kubernetes/MetricsApi.cpp:
##########
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "MetricsApi.h"
+
+#include <memory>
+
+extern "C" {
+#include "include/generic.h"
+}
+
+#include "ApiClient.h"
+
+namespace {
+struct genericClient_t_deleter { void operator()(genericClient_t* ptr) const 
noexcept { genericClient_free(ptr); } };
+using genericClient_unique_ptr = std::unique_ptr<genericClient_t, 
genericClient_t_deleter>;
+
+struct char_deleter { void operator()(char* ptr) const noexcept { free(ptr); } 
};
+using char_unique_ptr = std::unique_ptr<char, char_deleter>;

Review Comment:
   You can use `utils::FreeDeleter` here. `char*` vs `void*` is not an issue, 
it gets converted and freed normally.



##########
extensions/kubernetes/MetricsFilter.cpp:
##########
@@ -0,0 +1,81 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "MetricsFilter.h"
+
+#include "rapidjson/document.h"
+#include "rapidjson/error/en.h"
+#include "rapidjson/stringbuffer.h"
+#include "rapidjson/writer.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::kubernetes::metrics {
+
+nonstd::expected<std::string, std::string> filter(const std::string& 
metrics_json, const std::function<bool(const kubernetes::ContainerInfo&)>& 
filter_function) {
+  rapidjson::Document document;
+  rapidjson::ParseResult parse_result = 
document.Parse<rapidjson::kParseStopWhenDoneFlag>(metrics_json.data());
+  if (parse_result.IsError()) {
+    return nonstd::make_unexpected(utils::StringUtils::join_pack("Error 
parsing the metrics received from the Kubernetes API at offset ",
+        std::to_string(parse_result.Offset()), ": ", 
rapidjson::GetParseError_En(parse_result.Code())));
+  }
+
+  if (!document.HasMember("items") || !document["items"].IsArray()) {
+    return nonstd::make_unexpected("Unexpected JSON received from the 
Kubernetes API: missing list of 'items'");

Review Comment:
   Using expected like this is fine, but it would be nicer if you introduced an 
error category and error codes for the extension, and used `std::error_code` 
instead. It's totally fine if you don't want to do it, just a thought.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to