2626#include " iceberg/sort_order.h"
2727#include " iceberg/transform.h"
2828#include " iceberg/util/formatter.h"
29+ #include " iceberg/util/macros.h"
2930
3031namespace iceberg {
3132
@@ -39,26 +40,6 @@ constexpr std::string_view kNullOrder = "null-order";
3940constexpr std::string_view kOrderId = " order-id" ;
4041constexpr 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
6445nlohmann::json ToJson (const SortField& sort_field) {
@@ -84,26 +65,34 @@ nlohmann::json ToJson(const SortOrder& sort_order) {
8465
8566expected<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
9985expected<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));
0 commit comments