2727#include < nlohmann/json.hpp>
2828
2929#include " iceberg/catalog/rest/types.h"
30+ #include " iceberg/catalog/rest/validator.h"
3031#include " iceberg/json_internal.h"
3132#include " iceberg/table_identifier.h"
3233#include " iceberg/util/json_util_internal.h"
@@ -59,9 +60,77 @@ constexpr std::string_view kDestination = "destination";
5960constexpr std::string_view kMetadata = " metadata" ;
6061constexpr std::string_view kConfig = " config" ;
6162constexpr std::string_view kIdentifiers = " identifiers" ;
63+ constexpr std::string_view kOverrides = " overrides" ;
64+ constexpr std::string_view kDefaults = " defaults" ;
65+ constexpr std::string_view kEndpoints = " endpoints" ;
66+ constexpr std::string_view kMessage = " message" ;
67+ constexpr std::string_view kType = " type" ;
68+ constexpr std::string_view kCode = " code" ;
69+ constexpr std::string_view kStack = " stack" ;
70+ constexpr std::string_view kError = " error" ;
6271
6372} // namespace
6473
74+ nlohmann::json ToJson (const CatalogConfig& config) {
75+ nlohmann::json json;
76+ json[kOverrides ] = config.overrides ;
77+ json[kDefaults ] = config.defaults ;
78+ if (!config.endpoints .empty ()) {
79+ json[kEndpoints ] = config.endpoints ;
80+ }
81+ return json;
82+ }
83+
84+ Result<CatalogConfig> CatalogConfigFromJson (const nlohmann::json& json) {
85+ CatalogConfig config;
86+ ICEBERG_ASSIGN_OR_RAISE (
87+ config.overrides ,
88+ GetJsonValueOrDefault<decltype (config.overrides )>(json, kOverrides ));
89+ ICEBERG_ASSIGN_OR_RAISE (
90+ config.defaults , GetJsonValueOrDefault<decltype (config.defaults )>(json, kDefaults ));
91+ ICEBERG_ASSIGN_OR_RAISE (
92+ config.endpoints ,
93+ GetJsonValueOrDefault<std::vector<std::string>>(json, kEndpoints ));
94+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (config));
95+ return config;
96+ }
97+
98+ nlohmann::json ToJson (const ErrorModel& error) {
99+ nlohmann::json json;
100+ json[kMessage ] = error.message ;
101+ json[kType ] = error.type ;
102+ json[kCode ] = error.code ;
103+ if (!error.stack .empty ()) {
104+ json[kStack ] = error.stack ;
105+ }
106+ return json;
107+ }
108+
109+ Result<ErrorModel> ErrorModelFromJson (const nlohmann::json& json) {
110+ ErrorModel error;
111+ ICEBERG_ASSIGN_OR_RAISE (error.message , GetJsonValue<std::string>(json, kMessage ));
112+ ICEBERG_ASSIGN_OR_RAISE (error.type , GetJsonValue<std::string>(json, kType ));
113+ ICEBERG_ASSIGN_OR_RAISE (error.code , GetJsonValue<uint16_t >(json, kCode ));
114+ ICEBERG_ASSIGN_OR_RAISE (error.stack ,
115+ GetJsonValueOrDefault<std::vector<std::string>>(json, kStack ));
116+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (error));
117+ return error;
118+ }
119+
120+ nlohmann::json ToJson (const ErrorResponse& response) {
121+ nlohmann::json json;
122+ json[kError ] = ToJson (response.error );
123+ return json;
124+ }
125+
126+ Result<ErrorResponse> ErrorResponseFromJson (const nlohmann::json& json) {
127+ ErrorResponse response;
128+ ICEBERG_ASSIGN_OR_RAISE (auto error_json, GetJsonValue<nlohmann::json>(json, kError ));
129+ ICEBERG_ASSIGN_OR_RAISE (response.error , ErrorModelFromJson (error_json));
130+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (response));
131+ return response;
132+ }
133+
65134nlohmann::json ToJson (const CreateNamespaceRequest& request) {
66135 nlohmann::json json;
67136 json[kNamespace ] = request.namespace_ .levels ;
@@ -79,6 +148,7 @@ Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson(
79148 ICEBERG_ASSIGN_OR_RAISE (
80149 request.properties ,
81150 GetJsonValueOrDefault<decltype (request.properties )>(json, kProperties ));
151+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
82152 return request;
83153}
84154
@@ -102,6 +172,7 @@ Result<UpdateNamespacePropertiesRequest> UpdateNamespacePropertiesRequestFromJso
102172 request.removals , GetJsonValueOrDefault<std::vector<std::string>>(json, kRemovals ));
103173 ICEBERG_ASSIGN_OR_RAISE (
104174 request.updates , GetJsonValueOrDefault<decltype (request.updates )>(json, kUpdates ));
175+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
105176 return request;
106177}
107178
@@ -122,6 +193,7 @@ Result<RegisterTableRequest> RegisterTableRequestFromJson(const nlohmann::json&
122193 GetJsonValue<std::string>(json, kMetadataLocation ));
123194 ICEBERG_ASSIGN_OR_RAISE (request.overwrite ,
124195 GetJsonValueOrDefault<bool >(json, kOverwrite , false ));
196+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
125197 return request;
126198}
127199
@@ -139,6 +211,7 @@ Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json
139211 ICEBERG_ASSIGN_OR_RAISE (auto dest_json,
140212 GetJsonValue<nlohmann::json>(json, kDestination ));
141213 ICEBERG_ASSIGN_OR_RAISE (request.destination , TableIdentifierFromJson (dest_json));
214+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
142215 return request;
143216}
144217
@@ -165,6 +238,7 @@ Result<LoadTableResult> LoadTableResultFromJson(const nlohmann::json& json) {
165238 ICEBERG_ASSIGN_OR_RAISE (
166239 result.config , (GetJsonValueOrDefault<std::unordered_map<std::string, std::string>>(
167240 json, kConfig )));
241+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (result));
168242 return result;
169243}
170244
@@ -192,6 +266,7 @@ Result<ListNamespacesResponse> ListNamespacesResponseFromJson(
192266 ICEBERG_ASSIGN_OR_RAISE (auto ns, NamespaceFromJson (ns_json));
193267 response.namespaces .push_back (std::move (ns));
194268 }
269+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (response));
195270 return response;
196271}
197272
@@ -253,6 +328,7 @@ Result<UpdateNamespacePropertiesResponse> UpdateNamespacePropertiesResponseFromJ
253328 GetJsonValue<std::vector<std::string>>(json, kRemoved ));
254329 ICEBERG_ASSIGN_OR_RAISE (
255330 response.missing , GetJsonValueOrDefault<std::vector<std::string>>(json, kMissing ));
331+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (response));
256332 return response;
257333}
258334
@@ -279,6 +355,7 @@ Result<ListTablesResponse> ListTablesResponseFromJson(const nlohmann::json& json
279355 ICEBERG_ASSIGN_OR_RAISE (auto identifier, TableIdentifierFromJson (id_json));
280356 response.identifiers .push_back (std::move (identifier));
281357 }
358+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (response));
282359 return response;
283360}
284361
0 commit comments