Skip to content

Commit 36a76ca

Browse files
committed
refactor(serde): avoid duplicate macro for fallible REST ToJson
Extract the FromJson declarations into ICEBERG_DECLARE_FROM_JSON so the four schema/metadata-bearing models declare a Result-returning ToJson without duplicating the whole serde macro.
1 parent b252b72 commit 36a76ca

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

src/iceberg/catalog/rest/json_serde_internal.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,16 @@ namespace iceberg::rest {
3838
template <typename Model>
3939
Result<Model> FromJson(const nlohmann::json& json);
4040

41-
#define ICEBERG_DECLARE_JSON_SERDE(Model) \
41+
// Declares the two FromJson entry points for a model.
42+
#define ICEBERG_DECLARE_FROM_JSON(Model) \
4243
ICEBERG_REST_EXPORT Result<Model> Model##FromJson(const nlohmann::json& json); \
4344
\
4445
template <> \
45-
ICEBERG_REST_EXPORT Result<Model> FromJson(const nlohmann::json& json); \
46-
\
47-
ICEBERG_REST_EXPORT nlohmann::json ToJson(const Model& model);
46+
ICEBERG_REST_EXPORT Result<Model> FromJson(const nlohmann::json& json);
4847

49-
// Same as ICEBERG_DECLARE_JSON_SERDE, but ToJson returns Result because the model
50-
// embeds a Schema/TableMetadata whose default-value serialization can fail.
51-
#define ICEBERG_DECLARE_JSON_SERDE_FALLIBLE(Model) \
52-
ICEBERG_REST_EXPORT Result<Model> Model##FromJson(const nlohmann::json& json); \
53-
\
54-
template <> \
55-
ICEBERG_REST_EXPORT Result<Model> FromJson(const nlohmann::json& json); \
56-
\
57-
ICEBERG_REST_EXPORT Result<nlohmann::json> ToJson(const Model& model);
48+
#define ICEBERG_DECLARE_JSON_SERDE(Model) \
49+
ICEBERG_DECLARE_FROM_JSON(Model) \
50+
ICEBERG_REST_EXPORT nlohmann::json ToJson(const Model& model);
5851

5952
/// \note Don't forget to add `ICEBERG_DEFINE_FROM_JSON` to the end of
6053
/// `json_internal.cc` to define the `FromJson` function for the model.
@@ -67,16 +60,23 @@ ICEBERG_DECLARE_JSON_SERDE(GetNamespaceResponse)
6760
ICEBERG_DECLARE_JSON_SERDE(UpdateNamespacePropertiesRequest)
6861
ICEBERG_DECLARE_JSON_SERDE(UpdateNamespacePropertiesResponse)
6962
ICEBERG_DECLARE_JSON_SERDE(ListTablesResponse)
70-
ICEBERG_DECLARE_JSON_SERDE_FALLIBLE(LoadTableResult)
7163
ICEBERG_DECLARE_JSON_SERDE(RegisterTableRequest)
7264
ICEBERG_DECLARE_JSON_SERDE(RenameTableRequest)
73-
ICEBERG_DECLARE_JSON_SERDE_FALLIBLE(CreateTableRequest)
74-
ICEBERG_DECLARE_JSON_SERDE_FALLIBLE(CommitTableRequest)
75-
ICEBERG_DECLARE_JSON_SERDE_FALLIBLE(CommitTableResponse)
7665
ICEBERG_DECLARE_JSON_SERDE(OAuthTokenResponse)
7766

67+
// These models embed a Schema/TableMetadata whose default-value serialization can
68+
// fail, so their ToJson returns Result; FromJson is declared like the others.
69+
ICEBERG_DECLARE_FROM_JSON(LoadTableResult)
70+
ICEBERG_REST_EXPORT Result<nlohmann::json> ToJson(const LoadTableResult& model);
71+
ICEBERG_DECLARE_FROM_JSON(CreateTableRequest)
72+
ICEBERG_REST_EXPORT Result<nlohmann::json> ToJson(const CreateTableRequest& model);
73+
ICEBERG_DECLARE_FROM_JSON(CommitTableRequest)
74+
ICEBERG_REST_EXPORT Result<nlohmann::json> ToJson(const CommitTableRequest& model);
75+
ICEBERG_DECLARE_FROM_JSON(CommitTableResponse)
76+
ICEBERG_REST_EXPORT Result<nlohmann::json> ToJson(const CommitTableResponse& model);
77+
78+
#undef ICEBERG_DECLARE_FROM_JSON
7879
#undef ICEBERG_DECLARE_JSON_SERDE
79-
#undef ICEBERG_DECLARE_JSON_SERDE_FALLIBLE
8080

8181
ICEBERG_REST_EXPORT Result<PlanTableScanResponse> PlanTableScanResponseFromJson(
8282
const nlohmann::json& json,

0 commit comments

Comments
 (0)