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 ;
@@ -77,6 +146,7 @@ Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson(
77146 ICEBERG_ASSIGN_OR_RAISE (
78147 request.properties ,
79148 GetJsonValueOrDefault<decltype (request.properties )>(json, kProperties ));
149+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
80150 return request;
81151}
82152
@@ -94,6 +164,7 @@ Result<UpdateNamespacePropertiesRequest> UpdateNamespacePropertiesRequestFromJso
94164 request.removals , GetJsonValueOrDefault<std::vector<std::string>>(json, kRemovals ));
95165 ICEBERG_ASSIGN_OR_RAISE (
96166 request.updates , GetJsonValueOrDefault<decltype (request.updates )>(json, kUpdates ));
167+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
97168 return request;
98169}
99170
@@ -114,6 +185,7 @@ Result<RegisterTableRequest> RegisterTableRequestFromJson(const nlohmann::json&
114185 GetJsonValue<std::string>(json, kMetadataLocation ));
115186 ICEBERG_ASSIGN_OR_RAISE (request.overwrite ,
116187 GetJsonValueOrDefault<bool >(json, kOverwrite , false ));
188+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
117189 return request;
118190}
119191
@@ -131,6 +203,7 @@ Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json
131203 ICEBERG_ASSIGN_OR_RAISE (auto dest_json,
132204 GetJsonValue<nlohmann::json>(json, kDestination ));
133205 ICEBERG_ASSIGN_OR_RAISE (request.destination , TableIdentifierFromJson (dest_json));
206+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (request));
134207 return request;
135208}
136209
@@ -177,6 +250,7 @@ Result<ListNamespacesResponse> ListNamespacesResponseFromJson(
177250 ICEBERG_ASSIGN_OR_RAISE (auto ns, NamespaceFromJson (ns_json));
178251 response.namespaces .push_back (std::move (ns));
179252 }
253+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (response));
180254 return response;
181255}
182256
@@ -232,6 +306,7 @@ Result<UpdateNamespacePropertiesResponse> UpdateNamespacePropertiesResponseFromJ
232306 response.removed , GetJsonValueOrDefault<std::vector<std::string>>(json, kRemoved ));
233307 ICEBERG_ASSIGN_OR_RAISE (
234308 response.missing , GetJsonValueOrDefault<std::vector<std::string>>(json, kMissing ));
309+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (response));
235310 return response;
236311}
237312
@@ -256,6 +331,7 @@ Result<ListTablesResponse> ListTablesResponseFromJson(const nlohmann::json& json
256331 ICEBERG_ASSIGN_OR_RAISE (auto identifier, TableIdentifierFromJson (id_json));
257332 response.identifiers .push_back (std::move (identifier));
258333 }
334+ ICEBERG_RETURN_UNEXPECTED (Validator::Validate (response));
259335 return response;
260336}
261337
@@ -265,6 +341,9 @@ Result<ListTablesResponse> ListTablesResponseFromJson(const nlohmann::json& json
265341 return Model##FromJson (json); \
266342 }
267343
344+ ICEBERG_DEFINE_FROM_JSON (CatalogConfig)
345+ ICEBERG_DEFINE_FROM_JSON (ErrorModel)
346+ ICEBERG_DEFINE_FROM_JSON (ErrorResponse)
268347ICEBERG_DEFINE_FROM_JSON (ListNamespacesResponse)
269348ICEBERG_DEFINE_FROM_JSON (CreateNamespaceRequest)
270349ICEBERG_DEFINE_FROM_JSON (CreateNamespaceResponse)
0 commit comments