wgtmac commented on code in PR #292:
URL: https://github.com/apache/iceberg-cpp/pull/292#discussion_r2493487375


##########
src/iceberg/catalog/rest/validator.cc:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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 "iceberg/catalog/rest/validator.h"
+
+#include <algorithm>
+#include <format>
+#include <ranges>
+
+#include "iceberg/catalog/rest/types.h"
+#include "iceberg/result.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg::rest {
+
+// Configuration and Error types
+
+Status Validator::Validate(const CatalogConfig& config) {
+  // TODO(Li Feiyang): Add an invalidEndpoint test that validates endpoint 
format.
+  // See:
+  // 
https://github.com/apache/iceberg/blob/main/core/src/test/java/org/apache/iceberg/rest/responses/TestConfigResponseParser.java#L164
+  // for reference.
+  return {};
+}
+
+Status Validator::Validate(const ErrorModel& error) {
+  if (error.message.empty() || error.type.empty()) {
+    return Invalid("Invalid error model: missing required fields");
+  }
+
+  if (error.code < 400 || error.code > 600) {
+    return Invalid("Invalid error model: code {} is out of range [400, 600]", 
error.code);
+  }
+
+  // stack is optional, no validation needed
+  return {};
+}
+
+// We don't validate the error field because ErrorModel::Validate has been 
called in the
+// FromJson.
+Status Validator::Validate(const ErrorResponse& response) { return {}; }
+
+// Namespace operations
+
+Status Validator::Validate(const ListNamespacesResponse& response) { return 
{}; }
+
+Status Validator::Validate(const CreateNamespaceRequest& request) { return {}; 
}
+
+Status Validator::Validate(const CreateNamespaceResponse& response) { return 
{}; }
+
+Status Validator::Validate(const GetNamespaceResponse& response) { return {}; }
+
+Status Validator::Validate(const UpdateNamespacePropertiesRequest& request) {
+  // keys in updates and removals must not overlap
+  if (request.removals.empty() || request.updates.empty()) {
+    return {};
+  }
+
+  auto sorted_removals =
+      request.removals |
+      std::views::transform([](const std::string& s) { return 
std::string_view{s}; }) |
+      std::ranges::to<std::vector>();
+  std::ranges::sort(sorted_removals);
+
+  auto sorted_update_keys =
+      request.updates | std::views::keys |
+      std::views::transform([](const std::string& s) { return 
std::string_view{s}; }) |
+      std::ranges::to<std::vector>();
+  std::ranges::sort(sorted_update_keys);
+
+  std::vector<std::string_view> common;
+  std::ranges::set_intersection(sorted_update_keys, sorted_removals,
+                                std::back_inserter(common));
+
+  if (!common.empty()) {
+    std::string keys;
+    for (size_t i = 0; i < common.size(); ++i) {
+      if (i) keys += ", ";
+      keys += common[i];
+    }
+
+    return Invalid(
+        "Invalid namespace update: cannot simultaneously set and remove keys: "
+        "[{}]",
+        keys);

Review Comment:
   nit: you can include `formatter_internal.h` to directly print `common`



##########
src/iceberg/catalog/rest/json_internal.cc:
##########
@@ -59,9 +60,76 @@ constexpr std::string_view kDestination = "destination";
 constexpr std::string_view kMetadata = "metadata";
 constexpr std::string_view kConfig = "config";
 constexpr std::string_view kIdentifiers = "identifiers";
+constexpr std::string_view kOverrides = "overrides";
+constexpr std::string_view kDefaults = "defaults";
+constexpr std::string_view kEndpoints = "endpoints";
+constexpr std::string_view kMessage = "message";
+constexpr std::string_view kType = "type";
+constexpr std::string_view kCode = "code";
+constexpr std::string_view kStack = "stack";
+constexpr std::string_view kError = "error";
 
 }  // namespace
 
+nlohmann::json ToJson(const CatalogConfig& config) {
+  nlohmann::json json;
+  json[kOverrides] = config.overrides;
+  json[kDefaults] = config.defaults;
+  SetContainerField(json, kEndpoints, config.endpoints);
+  return json;
+}
+
+Result<CatalogConfig> CatalogConfigFromJson(const nlohmann::json& json) {
+  CatalogConfig config;
+  ICEBERG_ASSIGN_OR_RAISE(
+      config.overrides,
+      GetJsonValueOrDefault<decltype(config.overrides)>(json, kOverrides));
+  ICEBERG_ASSIGN_OR_RAISE(
+      config.defaults, GetJsonValueOrDefault<decltype(config.defaults)>(json, 
kDefaults));
+  ICEBERG_ASSIGN_OR_RAISE(
+      config.endpoints,
+      GetJsonValueOrDefault<std::vector<std::string>>(json, kEndpoints));
+  ICEBERG_RETURN_UNEXPECTED(Validator::Validate(config));
+  return config;
+}
+
+nlohmann::json ToJson(const ErrorModel& error) {
+  nlohmann::json json;
+  json[kMessage] = error.message;
+  json[kType] = error.type;
+  json[kCode] = error.code;
+  SetContainerField(json, kStack, error.stack);
+  return json;
+}
+
+Result<ErrorModel> ErrorModelFromJson(const nlohmann::json& json) {
+  ErrorModel error;
+  // NOTE: Iceberg's Java implementation allows missing required fields 
(message, type,

Review Comment:
   @Fokko I don't think this is something serious but there is a slight 
inconsistency between Java impl and the spec.



##########
src/iceberg/test/meson.build:
##########
@@ -96,6 +96,12 @@ if get_option('rest').enabled()
     }
 endif
 
+cpp = meson.get_compiler('cpp')
+cpp_bigobj_args = []
+if cpp.get_id() == 'msvc'
+    iceberg_common_cpp_args += cpp.get_supported_arguments(['/bigobj'])

Review Comment:
   @WillAyd Do we need to apply the flag to a single source file only?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to