Skip to content

Commit 4c3fbb5

Browse files
committed
refactor: consolidate json utility
1 parent 4a5fe91 commit 4c3fbb5

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

src/iceberg/json_internal.cc

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "iceberg/sort_order.h"
2727
#include "iceberg/transform.h"
2828
#include "iceberg/util/formatter.h"
29+
#include "iceberg/util/macros.h"
2930

3031
namespace iceberg {
3132

@@ -39,26 +40,6 @@ constexpr std::string_view kNullOrder = "null-order";
3940
constexpr std::string_view kOrderId = "order-id";
4041
constexpr std::string_view kFields = "fields";
4142

42-
// --- helper for safe JSON extraction ---
43-
template <typename T>
44-
expected<T, Error> GetJsonValue(const nlohmann::json& json, std::string_view key) {
45-
if (!json.contains(key)) {
46-
return unexpected<Error>({.kind = ErrorKind::kInvalidArgument,
47-
.message = "Missing key: " + std::string(key)});
48-
}
49-
try {
50-
return json.at(key).get<T>();
51-
} catch (const std::exception& ex) {
52-
return unexpected<Error>({.kind = ErrorKind::kInvalidArgument,
53-
.message = std::string("Failed to parse key: ") +
54-
key.data() + ", " + ex.what()});
55-
}
56-
}
57-
58-
#define TRY_ASSIGN(json_value, expr) \
59-
auto _tmp_##json_value = (expr); \
60-
if (!_tmp_##json_value) return unexpected(_tmp_##json_value.error()); \
61-
auto json_value = std::move(_tmp_##json_value.value());
6243
} // namespace
6344

6445
nlohmann::json ToJson(const SortField& sort_field) {
@@ -84,26 +65,34 @@ nlohmann::json ToJson(const SortOrder& sort_order) {
8465

8566
expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(
8667
const nlohmann::json& json) {
87-
TRY_ASSIGN(transform_str, GetJsonValue<std::string>(json, kTransform));
88-
TRY_ASSIGN(transform, TransformFunctionFromString(transform_str));
89-
TRY_ASSIGN(source_id, GetJsonValue<int32_t>(json, kSourceId));
90-
TRY_ASSIGN(direction_str, GetJsonValue<std::string>(json, kDirection));
91-
TRY_ASSIGN(direction, SortDirectionFromString(direction_str));
92-
TRY_ASSIGN(null_order_str, GetJsonValue<std::string>(json, kNullOrder));
93-
TRY_ASSIGN(null_order, NullOrderFromString(null_order_str));
68+
ICEBERG_CHECK_JSON_FIELD(kTransform, json);
69+
ICEBERG_CHECK_JSON_FIELD(kSourceId, json);
70+
ICEBERG_CHECK_JSON_FIELD(kDirection, json);
71+
ICEBERG_CHECK_JSON_FIELD(kNullOrder, json);
72+
73+
auto source_id = json[kSourceId].get<int32_t>();
74+
ICEBERG_ASSIGN_OR_RAISE(
75+
auto transform, TransformFunctionFromString(json[kTransform].get<std::string>()));
76+
ICEBERG_ASSIGN_OR_RAISE(auto direction,
77+
SortDirectionFromString(json[kDirection].get<std::string>()));
78+
ICEBERG_ASSIGN_OR_RAISE(auto null_order,
79+
NullOrderFromString(json[kNullOrder].get<std::string>()));
9480

9581
return std::make_unique<SortField>(source_id, std::move(transform), direction,
9682
null_order);
9783
}
9884

9985
expected<std::unique_ptr<SortOrder>, Error> SortOrderFromJson(
10086
const nlohmann::json& json) {
101-
TRY_ASSIGN(order_id, GetJsonValue<int32_t>(json, kOrderId));
87+
ICEBERG_CHECK_JSON_FIELD(kOrderId, json);
88+
ICEBERG_CHECK_JSON_FIELD(kFields, json);
89+
90+
auto order_id = json[kOrderId].get<int32_t>();
10291

10392
std::vector<SortField> sort_fields;
104-
for (const auto& field_json : json.at(kFields)) {
105-
TRY_ASSIGN(sort_field, SortFieldFromJson(field_json));
106-
sort_fields.push_back(*sort_field);
93+
for (const auto& field_json : json[kFields]) {
94+
ICEBERG_ASSIGN_OR_RAISE(auto sort_field, SortFieldFromJson(field_json));
95+
sort_fields.push_back(std::move(*sort_field));
10796
}
10897

10998
return std::make_unique<SortOrder>(order_id, std::move(sort_fields));

src/iceberg/json_internal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@
2727
#include "iceberg/expected.h"
2828
#include "iceberg/type_fwd.h"
2929

30+
#define ICEBERG_CHECK_JSON_FIELD(field_name, json) \
31+
if (!json.contains(field_name)) [[unlikely]] { \
32+
return unexpected<Error>({ \
33+
.kind = ErrorKind::kJsonParseError, \
34+
.message = std::format("Missing '{}' in {}", field_name, json.dump()), \
35+
}); \
36+
}
37+
3038
namespace iceberg {
39+
3140
/// \brief Serializes a `SortField` object to JSON.
3241
///
3342
/// This function converts a `SortField` object into a JSON representation.

src/iceberg/schema_internal.cc

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <nlohmann/json.hpp>
2929

3030
#include "iceberg/expected.h"
31+
#include "iceberg/json_internal.h"
3132
#include "iceberg/schema.h"
3233
#include "iceberg/type.h"
3334
#include "iceberg/util/macros.h"
@@ -480,14 +481,6 @@ nlohmann::json SchemaToJson(const Schema& schema) {
480481

481482
namespace {
482483

483-
#define ICEBERG_CHECK_JSON_FIELD(field_name, json) \
484-
if (!json.contains(field_name)) [[unlikely]] { \
485-
return unexpected<Error>({ \
486-
.kind = ErrorKind::kJsonParseError, \
487-
.message = std::format("Missing '{}' in {}", field_name, json.dump()), \
488-
}); \
489-
}
490-
491484
expected<std::unique_ptr<Type>, Error> StructTypeFromJson(const nlohmann::json& json) {
492485
ICEBERG_CHECK_JSON_FIELD(kFields, json);
493486

0 commit comments

Comments
 (0)