lidavidm commented on code in PR #64: URL: https://github.com/apache/iceberg-cpp/pull/64#discussion_r2036418337
########## src/iceberg/transform.cc: ########## @@ -72,4 +72,13 @@ expected<ArrowArray, Error> IdentityTransformFunction::Transform( .message = "IdentityTransformFunction::Transform"}); } +expected<std::unique_ptr<TransformFunction>, Error> TransformFunctionFromString( Review Comment: Not related, but should we do something like define `iceberg::Result<T> = expected<T, iceberg::Error>`? ########## 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) \ Review Comment: We'll probably want to make a broader version of this macro. (Not necessarily in this PR) FWIW, you could steal the Arrow version, which uses a counter to autogenerate the name, so it can be used like `ARROW_ASSIGN_OR_RAISE(auto value, ...)` or `ARROW_ASSIGN_OR_RAISE(foo.bar, ...)` too -- 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