|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/json_internal.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | + |
| 24 | +#include "iceberg/sort_order.h" |
| 25 | +#include "iceberg/transform.h" |
| 26 | +#include "iceberg/util/formatter.h" |
| 27 | + |
| 28 | +namespace iceberg { |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +constexpr std::string_view kTransform = "transform"; |
| 33 | +constexpr std::string_view kSourceId = "source-id"; |
| 34 | +constexpr std::string_view kDirection = "direction"; |
| 35 | +constexpr std::string_view kNullOrder = "null-order"; |
| 36 | + |
| 37 | +constexpr std::string_view kOrderId = "order-id"; |
| 38 | +constexpr std::string_view kFields = "fields"; |
| 39 | + |
| 40 | +// --- helper for safe JSON extraction --- |
| 41 | +template <typename T> |
| 42 | +expected<T, Error> GetJsonValue(const nlohmann::json& json, std::string_view key) { |
| 43 | + if (!json.contains(key)) { |
| 44 | + return unexpected<Error>({.kind = ErrorKind::kInvalidArgument, |
| 45 | + .message = "Missing key: " + std::string(key)}); |
| 46 | + } |
| 47 | + try { |
| 48 | + return json.at(key).get<T>(); |
| 49 | + } catch (const std::exception& ex) { |
| 50 | + return unexpected<Error>({.kind = ErrorKind::kInvalidArgument, |
| 51 | + .message = std::string("Failed to parse key: ") + |
| 52 | + key.data() + ", " + ex.what()}); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#define TRY_ASSIGN(json_value, expr) \ |
| 57 | + auto _tmp_##json_value = (expr); \ |
| 58 | + if (!_tmp_##json_value) return unexpected(_tmp_##json_value.error()); \ |
| 59 | + auto json_value = std::move(_tmp_##json_value.value()); |
| 60 | +} // namespace |
| 61 | + |
| 62 | +nlohmann::json ToJson(const SortField& sort_field) { |
| 63 | + nlohmann::json json; |
| 64 | + json[kTransform] = std::format("{}", *sort_field.transform()); |
| 65 | + json[kSourceId] = sort_field.source_id(); |
| 66 | + json[kDirection] = SortDirectionToString(sort_field.direction()); |
| 67 | + json[kNullOrder] = NullOrderToString(sort_field.null_order()); |
| 68 | + return json; |
| 69 | +} |
| 70 | + |
| 71 | +nlohmann::json ToJson(const SortOrder& sort_order) { |
| 72 | + nlohmann::json json; |
| 73 | + json[kOrderId] = sort_order.order_id(); |
| 74 | + |
| 75 | + nlohmann::json fields_json = nlohmann::json::array(); |
| 76 | + for (const auto& field : sort_order.fields()) { |
| 77 | + fields_json.push_back(ToJson(field)); |
| 78 | + } |
| 79 | + json[kFields] = fields_json; |
| 80 | + return json; |
| 81 | +} |
| 82 | + |
| 83 | +expected<std::unique_ptr<SortField>, Error> SortFieldFromJson( |
| 84 | + const nlohmann::json& json) { |
| 85 | + TRY_ASSIGN(transform_str, GetJsonValue<std::string>(json, kTransform)); |
| 86 | + TRY_ASSIGN(transform, TransformFunctionFromString(transform_str)); |
| 87 | + TRY_ASSIGN(source_id, GetJsonValue<int32_t>(json, kSourceId)); |
| 88 | + TRY_ASSIGN(direction_str, GetJsonValue<std::string>(json, kDirection)); |
| 89 | + TRY_ASSIGN(direction, SortDirectionFromString(direction_str)); |
| 90 | + TRY_ASSIGN(null_order_str, GetJsonValue<std::string>(json, kNullOrder)); |
| 91 | + TRY_ASSIGN(null_order, NullOrderFromString(null_order_str)); |
| 92 | + |
| 93 | + return std::make_unique<SortField>(source_id, std::move(transform), direction, |
| 94 | + null_order); |
| 95 | +} |
| 96 | + |
| 97 | +expected<std::unique_ptr<SortOrder>, Error> SortOrderFromJson( |
| 98 | + const nlohmann::json& json) { |
| 99 | + TRY_ASSIGN(order_id, GetJsonValue<int32_t>(json, kOrderId)); |
| 100 | + |
| 101 | + std::vector<SortField> sort_fields; |
| 102 | + for (const auto& field_json : json.at(kFields)) { |
| 103 | + TRY_ASSIGN(sort_field, SortFieldFromJson(field_json)); |
| 104 | + sort_fields.push_back(*sort_field); |
| 105 | + } |
| 106 | + |
| 107 | + return std::make_unique<SortOrder>(order_id, std::move(sort_fields)); |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace iceberg |
0 commit comments