zhjwpku commented on code in PR #64: URL: https://github.com/apache/iceberg-cpp/pull/64#discussion_r2036541375
########## src/iceberg/json_internal.cc: ########## @@ -0,0 +1,112 @@ +/* + * 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/json_internal.h" + +#include <format> + +#include <nlohmann/json.hpp> + +#include "iceberg/sort_order.h" +#include "iceberg/transform.h" +#include "iceberg/util/formatter.h" + +namespace iceberg { + +namespace { + +constexpr std::string_view kTransform = "transform"; +constexpr std::string_view kSourceId = "source-id"; +constexpr std::string_view kDirection = "direction"; +constexpr std::string_view kNullOrder = "null-order"; + +constexpr std::string_view kOrderId = "order-id"; +constexpr std::string_view kFields = "fields"; + +// --- helper for safe JSON extraction --- +template <typename T> +expected<T, Error> GetJsonValue(const nlohmann::json& json, std::string_view key) { + if (!json.contains(key)) { + return unexpected<Error>({.kind = ErrorKind::kInvalidArgument, + .message = "Missing key: " + std::string(key)}); + } + try { + return json.at(key).get<T>(); + } catch (const std::exception& ex) { + return unexpected<Error>({.kind = ErrorKind::kInvalidArgument, + .message = std::string("Failed to parse key: ") + + key.data() + ", " + ex.what()}); + } +} + +#define TRY_ASSIGN(json_value, expr) \ + auto _tmp_##json_value = (expr); \ + if (!_tmp_##json_value) return unexpected(_tmp_##json_value.error()); \ + auto json_value = std::move(_tmp_##json_value.value()); +} // namespace + +nlohmann::json ToJson(const SortField& sort_field) { Review Comment: I was thinking about adding ToJson and FromJson member functions to each Spec classes, but your way might be better, users don't need to touch the json ser/der functions, right? -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org