Skip to content

Commit 4bfae16

Browse files
committed
complete
1 parent b23c6fc commit 4bfae16

File tree

4 files changed

+384
-224
lines changed

4 files changed

+384
-224
lines changed

src/iceberg/catalog/rest/json_internal.cc

Lines changed: 110 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
#include <nlohmann/json.hpp>
2727

2828
#include "iceberg/json_internal.h"
29-
#include "iceberg/partition_spec.h"
3029
#include "iceberg/result.h"
31-
#include "iceberg/schema.h"
32-
#include "iceberg/sort_order.h"
3330
#include "iceberg/table_identifier.h"
3431
#include "iceberg/table_metadata.h"
3532
#include "iceberg/util/json_util_internal.h"
@@ -74,11 +71,8 @@ nlohmann::json TableIdentifierToJson(const TableIdentifier& identifier) {
7471
Result<TableIdentifier> TableIdentifierFromJson(const nlohmann::json& json) {
7572
TableIdentifier identifier;
7673

77-
if (json.contains(kNamespace)) {
78-
ICEBERG_ASSIGN_OR_RAISE(identifier.ns.levels,
79-
GetJsonValue<std::vector<std::string>>(json, kNamespace));
80-
}
81-
74+
ICEBERG_ASSIGN_OR_RAISE(identifier.ns.levels,
75+
GetJsonValue<std::vector<std::string>>(json, kNamespace));
8276
ICEBERG_ASSIGN_OR_RAISE(identifier.name, GetJsonValue<std::string>(json, kName));
8377

8478
return identifier;
@@ -101,6 +95,65 @@ Result<ListNamespaceResponse> ListNamespaceResponseFromJson(const nlohmann::json
10195
return response;
10296
}
10397

98+
nlohmann::json ToJson(const CreateNamespaceRequest& request) {
99+
nlohmann::json json;
100+
json[kNamespaces] = request.namespaces;
101+
SetOptionalField(json, kProperties, request.properties);
102+
return json;
103+
}
104+
105+
Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson(
106+
const nlohmann::json& json) {
107+
CreateNamespaceRequest request;
108+
109+
ICEBERG_ASSIGN_OR_RAISE(request.namespaces,
110+
GetJsonValue<std::vector<std::string>>(json, kNamespaces));
111+
using MapType = std::unordered_map<std::string, std::string>;
112+
ICEBERG_ASSIGN_OR_RAISE(request.properties,
113+
GetJsonValueOptional<MapType>(json, kProperties));
114+
115+
return request;
116+
}
117+
118+
nlohmann::json ToJson(const CreateNamespaceResponse& response) {
119+
nlohmann::json json;
120+
json[kNamespaces] = response.namespaces;
121+
SetOptionalField(json, kProperties, response.properties);
122+
return json;
123+
}
124+
125+
Result<CreateNamespaceResponse> CreateNamespaceResponseFromJson(
126+
const nlohmann::json& json) {
127+
CreateNamespaceResponse response;
128+
129+
ICEBERG_ASSIGN_OR_RAISE(response.namespaces,
130+
GetJsonValue<std::vector<std::string>>(json, kNamespaces));
131+
using MapType = std::unordered_map<std::string, std::string>;
132+
ICEBERG_ASSIGN_OR_RAISE(response.properties,
133+
GetJsonValueOptional<MapType>(json, kProperties));
134+
135+
return response;
136+
}
137+
138+
nlohmann::json ToJson(const GetNamespacePropertiesResponse& response) {
139+
nlohmann::json json;
140+
json[kNamespaces] = response.namespaces;
141+
json[kProperties] = response.properties;
142+
return json;
143+
}
144+
145+
Result<GetNamespacePropertiesResponse> GetNamespacePropertiesResponseFromJson(
146+
const nlohmann::json& json) {
147+
GetNamespacePropertiesResponse response;
148+
149+
ICEBERG_ASSIGN_OR_RAISE(response.namespaces,
150+
GetJsonValue<std::vector<std::string>>(json, kNamespaces));
151+
using MapType = std::unordered_map<std::string, std::string>;
152+
ICEBERG_ASSIGN_OR_RAISE(response.properties, GetJsonValue<MapType>(json, kProperties));
153+
154+
return response;
155+
}
156+
104157
nlohmann::json ToJson(const UpdateNamespacePropsRequest& request) {
105158
nlohmann::json json;
106159
SetOptionalField(json, kRemovals, request.removals);
@@ -156,64 +209,13 @@ nlohmann::json ToJson(const ListTableResponse& response) {
156209
Result<ListTableResponse> ListTableResponseFromJson(const nlohmann::json& json) {
157210
ListTableResponse response;
158211

159-
ICEBERG_ASSIGN_OR_RAISE(auto identifiers_json,
160-
GetJsonValue<nlohmann::json>(json, kIdentifiers));
161-
162-
for (const auto& id_json : identifiers_json) {
212+
for (const auto& id_json : json[kIdentifiers]) {
163213
ICEBERG_ASSIGN_OR_RAISE(auto identifier, TableIdentifierFromJson(id_json));
164214
response.identifiers.push_back(std::move(identifier));
165215
}
166216
return response;
167217
}
168218

169-
nlohmann::json ToJson(const RenameTableRequest& request) {
170-
nlohmann::json json;
171-
json[kSource] = TableIdentifierToJson(request.source);
172-
json[kDestination] = TableIdentifierToJson(request.destination);
173-
return json;
174-
}
175-
176-
Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json) {
177-
RenameTableRequest request;
178-
179-
ICEBERG_ASSIGN_OR_RAISE(auto source_json, GetJsonValue<nlohmann::json>(json, kSource));
180-
ICEBERG_ASSIGN_OR_RAISE(request.source, TableIdentifierFromJson(source_json));
181-
182-
ICEBERG_ASSIGN_OR_RAISE(auto dest_json,
183-
GetJsonValue<nlohmann::json>(json, kDestination));
184-
ICEBERG_ASSIGN_OR_RAISE(request.destination, TableIdentifierFromJson(dest_json));
185-
186-
return request;
187-
}
188-
189-
nlohmann::json ToJson(const LoadTableResponse& response) {
190-
nlohmann::json json;
191-
192-
SetOptionalField(json, kMetadataLocation, response.metadata_location);
193-
json[kMetadata] = iceberg::ToJson(response.metadata);
194-
SetOptionalField(json, kConfig, response.config);
195-
196-
return json;
197-
}
198-
199-
Result<LoadTableResponse> LoadTableResponseFromJson(const nlohmann::json& json) {
200-
LoadTableResponse response;
201-
202-
ICEBERG_ASSIGN_OR_RAISE(response.metadata_location,
203-
GetJsonValueOptional<std::string>(json, kMetadataLocation));
204-
205-
ICEBERG_ASSIGN_OR_RAISE(auto metadata_json,
206-
GetJsonValue<nlohmann::json>(json, kMetadata));
207-
ICEBERG_ASSIGN_OR_RAISE(auto metadata_ptr,
208-
iceberg::TableMetadataFromJson(metadata_json));
209-
response.metadata = std::move(*metadata_ptr);
210-
211-
using MapType = std::unordered_map<std::string, std::string>;
212-
ICEBERG_ASSIGN_OR_RAISE(response.config, GetJsonValueOptional<MapType>(json, kConfig));
213-
214-
return response;
215-
}
216-
217219
nlohmann::json ToJson(const CreateTableRequest& request) {
218220
nlohmann::json json;
219221
json[kName] = request.name;
@@ -236,14 +238,11 @@ nlohmann::json ToJson(const CreateTableRequest& request) {
236238
Result<CreateTableRequest> CreateTableRequestFromJson(const nlohmann::json& json) {
237239
CreateTableRequest request;
238240

239-
// Parse required fields
240241
ICEBERG_ASSIGN_OR_RAISE(request.name, GetJsonValue<std::string>(json, kName));
241242

242-
// Parse optional location
243243
ICEBERG_ASSIGN_OR_RAISE(request.location,
244244
GetJsonValueOptional<std::string>(json, kLocation));
245245

246-
// Parse required schema
247246
ICEBERG_ASSIGN_OR_RAISE(auto schema_json, GetJsonValue<nlohmann::json>(json, kSchema));
248247
ICEBERG_ASSIGN_OR_RAISE(auto schema_ptr, iceberg::SchemaFromJson(schema_json));
249248
request.schema = std::move(schema_ptr);
@@ -297,4 +296,52 @@ Result<RegisterTableRequest> RegisterTableRequestFromJson(const nlohmann::json&
297296
return request;
298297
}
299298

299+
nlohmann::json ToJson(const RenameTableRequest& request) {
300+
nlohmann::json json;
301+
json[kSource] = TableIdentifierToJson(request.source);
302+
json[kDestination] = TableIdentifierToJson(request.destination);
303+
return json;
304+
}
305+
306+
Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json) {
307+
RenameTableRequest request;
308+
309+
ICEBERG_ASSIGN_OR_RAISE(auto source_json, GetJsonValue<nlohmann::json>(json, kSource));
310+
ICEBERG_ASSIGN_OR_RAISE(request.source, TableIdentifierFromJson(source_json));
311+
312+
ICEBERG_ASSIGN_OR_RAISE(auto dest_json,
313+
GetJsonValue<nlohmann::json>(json, kDestination));
314+
ICEBERG_ASSIGN_OR_RAISE(request.destination, TableIdentifierFromJson(dest_json));
315+
316+
return request;
317+
}
318+
319+
nlohmann::json ToJson(const LoadTableResponse& response) {
320+
nlohmann::json json;
321+
322+
SetOptionalField(json, kMetadataLocation, response.metadata_location);
323+
json[kMetadata] = iceberg::ToJson(response.metadata);
324+
SetOptionalField(json, kConfig, response.config);
325+
326+
return json;
327+
}
328+
329+
Result<LoadTableResponse> LoadTableResponseFromJson(const nlohmann::json& json) {
330+
LoadTableResponse response;
331+
332+
ICEBERG_ASSIGN_OR_RAISE(response.metadata_location,
333+
GetJsonValueOptional<std::string>(json, kMetadataLocation));
334+
335+
ICEBERG_ASSIGN_OR_RAISE(auto metadata_json,
336+
GetJsonValue<nlohmann::json>(json, kMetadata));
337+
ICEBERG_ASSIGN_OR_RAISE(auto metadata_ptr,
338+
iceberg::TableMetadataFromJson(metadata_json));
339+
response.metadata = std::move(*metadata_ptr);
340+
341+
using MapType = std::unordered_map<std::string, std::string>;
342+
ICEBERG_ASSIGN_OR_RAISE(response.config, GetJsonValueOptional<MapType>(json, kConfig));
343+
344+
return response;
345+
}
346+
300347
} // namespace iceberg::rest

src/iceberg/catalog/rest/json_internal.h

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,44 @@ nlohmann::json ToJson(const ListNamespaceResponse& response);
3838
/// \return A `ListNamespaceResponse` object or an error if the conversion fails.
3939
Result<ListNamespaceResponse> ListNamespaceResponseFromJson(const nlohmann::json& json);
4040

41+
/// \brief Serializes a `CreateNamespaceRequest` object to JSON.
42+
///
43+
/// \param request The `CreateNamespaceRequest` object to be serialized.
44+
/// \return A JSON object representing the `CreateNamespaceRequest`.
45+
nlohmann::json ToJson(const CreateNamespaceRequest& request);
46+
47+
/// \brief Deserializes a JSON object into a `CreateNamespaceRequest` object.
48+
///
49+
/// \param json The JSON object representing a `CreateNamespaceRequest`.
50+
/// \return A `CreateNamespaceRequest` object or an error if the conversion fails.
51+
Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson(const nlohmann::json& json);
52+
53+
/// \brief Serializes a `CreateNamespaceResponse` object to JSON.
54+
///
55+
/// \param response The `CreateNamespaceResponse` object to be serialized.
56+
/// \return A JSON object representing the `CreateNamespaceResponse`.
57+
nlohmann::json ToJson(const CreateNamespaceResponse& response);
58+
59+
/// \brief Deserializes a JSON object into a `CreateNamespaceResponse` object.
60+
///
61+
/// \param json The JSON object representing a `CreateNamespaceResponse`.
62+
/// \return A `CreateNamespaceResponse` object or an error if the conversion fails.
63+
Result<CreateNamespaceResponse> CreateNamespaceResponseFromJson(
64+
const nlohmann::json& json);
65+
66+
/// \brief Serializes a `GetNamespacePropertiesResponse` object to JSON.
67+
///
68+
/// \param response The `GetNamespacePropertiesResponse` object to be serialized.
69+
/// \return A JSON object representing the `GetNamespacePropertiesResponse`.
70+
nlohmann::json ToJson(const GetNamespacePropertiesResponse& response);
71+
72+
/// \brief Deserializes a JSON object into a `GetNamespacePropertiesResponse` object.
73+
///
74+
/// \param json The JSON object representing a `GetNamespacePropertiesResponse`.
75+
/// \return A `GetNamespacePropertiesResponse` object or an error if the conversion fails.
76+
Result<GetNamespacePropertiesResponse> GetNamespacePropertiesResponseFromJson(
77+
const nlohmann::json& json);
78+
4179
/// \brief Serializes an `UpdateNamespacePropsRequest` object to JSON.
4280
///
4381
/// \param request The `UpdateNamespacePropsRequest` object to be serialized.
@@ -76,30 +114,6 @@ nlohmann::json ToJson(const ListTableResponse& response);
76114
/// \return A `ListTableResponse` object or an error if the conversion fails.
77115
Result<ListTableResponse> ListTableResponseFromJson(const nlohmann::json& json);
78116

79-
/// \brief Serializes a `RenameTableRequest` object to JSON.
80-
///
81-
/// \param request The `RenameTableRequest` object to be serialized.
82-
/// \return A JSON object representing the `RenameTableRequest`.
83-
nlohmann::json ToJson(const RenameTableRequest& request);
84-
85-
/// \brief Deserializes a JSON object into a `RenameTableRequest` object.
86-
///
87-
/// \param json The JSON object representing a `RenameTableRequest`.
88-
/// \return A `RenameTableRequest` object or an error if the conversion fails.
89-
Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json);
90-
91-
/// \brief Serializes a `LoadTableResponse` object to JSON.
92-
///
93-
/// \param response The `LoadTableResponse` object to be serialized.
94-
/// \return A JSON object representing the `LoadTableResponse`.
95-
nlohmann::json ToJson(const LoadTableResponse& response);
96-
97-
/// \brief Deserializes a JSON object into a `LoadTableResponse` object.
98-
///
99-
/// \param json The JSON object representing a `LoadTableResponse`.
100-
/// \return A `LoadTableResponse` object or an error if the conversion fails.
101-
Result<LoadTableResponse> LoadTableResponseFromJson(const nlohmann::json& json);
102-
103117
/// \brief Serializes a `CreateTableRequest` object to JSON.
104118
///
105119
/// \param request The `CreateTableRequest` object to be serialized.
@@ -124,4 +138,28 @@ nlohmann::json ToJson(const RegisterTableRequest& request);
124138
/// \return A `RegisterTableRequest` object or an error if the conversion fails.
125139
Result<RegisterTableRequest> RegisterTableRequestFromJson(const nlohmann::json& json);
126140

141+
/// \brief Serializes a `RenameTableRequest` object to JSON.
142+
///
143+
/// \param request The `RenameTableRequest` object to be serialized.
144+
/// \return A JSON object representing the `RenameTableRequest`.
145+
nlohmann::json ToJson(const RenameTableRequest& request);
146+
147+
/// \brief Deserializes a JSON object into a `RenameTableRequest` object.
148+
///
149+
/// \param json The JSON object representing a `RenameTableRequest`.
150+
/// \return A `RenameTableRequest` object or an error if the conversion fails.
151+
Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json);
152+
153+
/// \brief Serializes a `LoadTableResponse` object to JSON.
154+
///
155+
/// \param response The `LoadTableResponse` object to be serialized.
156+
/// \return A JSON object representing the `LoadTableResponse`.
157+
nlohmann::json ToJson(const LoadTableResponse& response);
158+
159+
/// \brief Deserializes a JSON object into a `LoadTableResponse` object.
160+
///
161+
/// \param json The JSON object representing a `LoadTableResponse`.
162+
/// \return A `LoadTableResponse` object or an error if the conversion fails.
163+
Result<LoadTableResponse> LoadTableResponseFromJson(const nlohmann::json& json);
164+
127165
} // namespace iceberg::rest

0 commit comments

Comments
 (0)