-
Notifications
You must be signed in to change notification settings - Fork 86
feat: add config, error and validation for rest types #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,13 @@ | |
| #include "iceberg/catalog/rest/json_internal.h" | ||
|
|
||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "iceberg/catalog/rest/types.h" | ||
| #include "iceberg/catalog/rest/validator.h" | ||
| #include "iceberg/json_internal.h" | ||
| #include "iceberg/table_identifier.h" | ||
| #include "iceberg/util/json_util_internal.h" | ||
|
|
@@ -59,9 +59,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, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Fokko I don't think this is something serious but there is a slight inconsistency between Java impl and the spec. |
||
| // code) during deserialization, which deviates from the REST spec. We enforce strict | ||
| // validation here. | ||
| ICEBERG_ASSIGN_OR_RAISE(error.message, GetJsonValue<std::string>(json, kMessage)); | ||
| ICEBERG_ASSIGN_OR_RAISE(error.type, GetJsonValue<std::string>(json, kType)); | ||
| ICEBERG_ASSIGN_OR_RAISE(error.code, GetJsonValue<uint32_t>(json, kCode)); | ||
| ICEBERG_ASSIGN_OR_RAISE(error.stack, | ||
| GetJsonValueOrDefault<std::vector<std::string>>(json, kStack)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(error)); | ||
| return error; | ||
| } | ||
|
|
||
| nlohmann::json ToJson(const ErrorResponse& response) { | ||
| nlohmann::json json; | ||
| json[kError] = ToJson(response.error); | ||
| return json; | ||
| } | ||
|
|
||
| Result<ErrorResponse> ErrorResponseFromJson(const nlohmann::json& json) { | ||
| ErrorResponse response; | ||
| ICEBERG_ASSIGN_OR_RAISE(auto error_json, GetJsonValue<nlohmann::json>(json, kError)); | ||
| ICEBERG_ASSIGN_OR_RAISE(response.error, ErrorModelFromJson(error_json)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(response)); | ||
| return response; | ||
| } | ||
|
|
||
| nlohmann::json ToJson(const CreateNamespaceRequest& request) { | ||
| nlohmann::json json; | ||
| json[kNamespace] = request.namespace_.levels; | ||
|
|
@@ -77,6 +144,7 @@ Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson( | |
| ICEBERG_ASSIGN_OR_RAISE( | ||
| request.properties, | ||
| GetJsonValueOrDefault<decltype(request.properties)>(json, kProperties)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(request)); | ||
| return request; | ||
| } | ||
|
|
||
|
|
@@ -94,6 +162,7 @@ Result<UpdateNamespacePropertiesRequest> UpdateNamespacePropertiesRequestFromJso | |
| request.removals, GetJsonValueOrDefault<std::vector<std::string>>(json, kRemovals)); | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| request.updates, GetJsonValueOrDefault<decltype(request.updates)>(json, kUpdates)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(request)); | ||
| return request; | ||
| } | ||
|
|
||
|
|
@@ -114,6 +183,7 @@ Result<RegisterTableRequest> RegisterTableRequestFromJson(const nlohmann::json& | |
| GetJsonValue<std::string>(json, kMetadataLocation)); | ||
| ICEBERG_ASSIGN_OR_RAISE(request.overwrite, | ||
| GetJsonValueOrDefault<bool>(json, kOverwrite, false)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(request)); | ||
| return request; | ||
| } | ||
|
|
||
|
|
@@ -131,6 +201,7 @@ Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json | |
| ICEBERG_ASSIGN_OR_RAISE(auto dest_json, | ||
| GetJsonValue<nlohmann::json>(json, kDestination)); | ||
| ICEBERG_ASSIGN_OR_RAISE(request.destination, TableIdentifierFromJson(dest_json)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(request)); | ||
| return request; | ||
| } | ||
|
|
||
|
|
@@ -177,6 +248,7 @@ Result<ListNamespacesResponse> ListNamespacesResponseFromJson( | |
| ICEBERG_ASSIGN_OR_RAISE(auto ns, NamespaceFromJson(ns_json)); | ||
| response.namespaces.push_back(std::move(ns)); | ||
| } | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(response)); | ||
| return response; | ||
| } | ||
|
|
||
|
|
@@ -232,6 +304,7 @@ Result<UpdateNamespacePropertiesResponse> UpdateNamespacePropertiesResponseFromJ | |
| response.removed, GetJsonValueOrDefault<std::vector<std::string>>(json, kRemoved)); | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| response.missing, GetJsonValueOrDefault<std::vector<std::string>>(json, kMissing)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(response)); | ||
| return response; | ||
| } | ||
|
|
||
|
|
@@ -256,6 +329,7 @@ Result<ListTablesResponse> ListTablesResponseFromJson(const nlohmann::json& json | |
| ICEBERG_ASSIGN_OR_RAISE(auto identifier, TableIdentifierFromJson(id_json)); | ||
| response.identifiers.push_back(std::move(identifier)); | ||
| } | ||
| ICEBERG_RETURN_UNEXPECTED(Validator::Validate(response)); | ||
| return response; | ||
| } | ||
|
|
||
|
|
@@ -265,6 +339,9 @@ Result<ListTablesResponse> ListTablesResponseFromJson(const nlohmann::json& json | |
| return Model##FromJson(json); \ | ||
| } | ||
|
|
||
| ICEBERG_DEFINE_FROM_JSON(CatalogConfig) | ||
| ICEBERG_DEFINE_FROM_JSON(ErrorModel) | ||
| ICEBERG_DEFINE_FROM_JSON(ErrorResponse) | ||
| ICEBERG_DEFINE_FROM_JSON(ListNamespacesResponse) | ||
| ICEBERG_DEFINE_FROM_JSON(CreateNamespaceRequest) | ||
| ICEBERG_DEFINE_FROM_JSON(CreateNamespaceResponse) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * 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 "iceberg/catalog/rest/types.h" | ||
| #include "iceberg/result.h" | ||
| #include "iceberg/util/formatter_internal.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 {}; } | ||
HeartLinked marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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 extract_and_sort = [](const auto& container, auto key_extractor) { | ||
| std::vector<std::string_view> result; | ||
| result.reserve(container.size()); | ||
| for (const auto& item : container) { | ||
| result.push_back(std::string_view{key_extractor(item)}); | ||
| } | ||
| std::ranges::sort(result); | ||
| return result; | ||
| }; | ||
|
|
||
| auto sorted_removals = | ||
| extract_and_sort(request.removals, [](const auto& s) -> const auto& { return s; }); | ||
| auto sorted_update_keys = extract_and_sort( | ||
| request.updates, [](const auto& pair) -> const auto& { return pair.first; }); | ||
|
|
||
| std::vector<std::string_view> common; | ||
| std::ranges::set_intersection(sorted_removals, sorted_update_keys, | ||
| std::back_inserter(common)); | ||
|
|
||
| if (!common.empty()) { | ||
| return Invalid( | ||
| "Invalid namespace update: cannot simultaneously set and remove keys: {}", | ||
| common); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| Status Validator::Validate(const UpdateNamespacePropertiesResponse& response) { | ||
| return {}; | ||
| } | ||
|
|
||
| // Table operations | ||
|
|
||
| Status Validator::Validate(const ListTablesResponse& response) { return {}; } | ||
|
|
||
| Status Validator::Validate(const LoadTableResult& result) { | ||
| if (!result.metadata) { | ||
| return Invalid("Invalid metadata: null"); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| Status Validator::Validate(const RegisterTableRequest& request) { | ||
| if (request.name.empty()) { | ||
| return Invalid("Missing table name"); | ||
| } | ||
|
|
||
| if (request.metadata_location.empty()) { | ||
| return Invalid("Empty metadata location"); | ||
| } | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| Status Validator::Validate(const RenameTableRequest& request) { | ||
| ICEBERG_RETURN_UNEXPECTED(Validate(request.source)); | ||
| ICEBERG_RETURN_UNEXPECTED(Validate(request.destination)); | ||
| return {}; | ||
| } | ||
|
|
||
| Status Validator::Validate(const TableIdentifier& identifier) { | ||
| if (identifier.name.empty()) { | ||
| return Invalid("Invalid table identifier: missing table name"); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| } // namespace iceberg::rest | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.