Skip to content

Commit ef171a4

Browse files
committed
fix: review comments
1 parent 3ec142a commit ef171a4

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

src/iceberg/json_internal.cc

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ const std::unordered_set<std::string_view> kValidSnapshotSummaryFields = {
110110
SnapshotSummaryFields::kTotalEqDeletes,
111111
SnapshotSummaryFields::kDeletedDuplicatedFiles,
112112
SnapshotSummaryFields::kChangedPartitionCountProp,
113-
SnapshotSummaryFields::kWAPID,
114-
SnapshotSummaryFields::kPublishedWAPID,
115-
SnapshotSummaryFields::kSourceSnapshotID,
113+
SnapshotSummaryFields::kWAPId,
114+
SnapshotSummaryFields::kPublishedWAPId,
115+
SnapshotSummaryFields::kSourceSnapshotId,
116116
SnapshotSummaryFields::kEngineName,
117117
SnapshotSummaryFields::kEngineVersion};
118118

@@ -154,6 +154,14 @@ Result<std::optional<T>> GetJsonValueOptional(const nlohmann::json& json,
154154
}
155155
}
156156

157+
template <typename T>
158+
void SetOptionalField(nlohmann::json& json, std::string_view key,
159+
const std::optional<T>& value) {
160+
if (value.has_value()) {
161+
json[key] = *value;
162+
}
163+
}
164+
157165
} // namespace
158166

159167
nlohmann::json ToJson(const SortField& sort_field) {
@@ -302,44 +310,25 @@ nlohmann::json SnapshotRefToJson(const SnapshotRef& ref) {
302310
json[kType] = SnapshotRefTypeToString(ref.type());
303311
if (ref.type() == SnapshotRefType::kBranch) {
304312
const auto& branch = std::get<SnapshotRef::Branch>(ref.retention);
305-
if (branch.min_snapshots_to_keep.has_value()) {
306-
json[kMinSnapshotsToKeep] = *branch.min_snapshots_to_keep;
307-
}
308-
if (branch.max_snapshot_age_ms.has_value()) {
309-
json[kMaxSnapshotAgeMs] = *branch.max_snapshot_age_ms;
310-
}
311-
if (branch.max_ref_age_ms.has_value()) {
312-
json[kMaxRefAgeMs] = *branch.max_ref_age_ms;
313-
}
313+
SetOptionalField(json, kMinSnapshotsToKeep, branch.min_snapshots_to_keep);
314+
SetOptionalField(json, kMaxSnapshotAgeMs, branch.max_snapshot_age_ms);
315+
SetOptionalField(json, kMaxRefAgeMs, branch.max_ref_age_ms);
314316
} else if (ref.type() == SnapshotRefType::kTag) {
315317
const auto& tag = std::get<SnapshotRef::Tag>(ref.retention);
316-
if (tag.max_ref_age_ms.has_value()) {
317-
json[kMaxRefAgeMs] = *tag.max_ref_age_ms;
318-
}
318+
SetOptionalField(json, kMaxRefAgeMs, tag.max_ref_age_ms);
319319
}
320320
return json;
321321
}
322322

323323
nlohmann::json SnapshotToJson(const Snapshot& snapshot) {
324324
nlohmann::json json;
325325
json[kSnapshotId] = snapshot.snapshot_id;
326-
if (snapshot.parent_snapshot_id.has_value()) {
327-
json[kParentSnapshotId] = *snapshot.parent_snapshot_id;
328-
}
326+
SetOptionalField(json, kParentSnapshotId, snapshot.parent_snapshot_id);
329327
json[kSequenceNumber] = snapshot.sequence_number;
330328
json[kTimestampMs] = snapshot.timestamp_ms;
331329
json[kManifestList] = snapshot.manifest_list;
332-
333-
nlohmann::json summary_json;
334-
for (const auto& [key, value] : snapshot.summary) {
335-
summary_json[key] = value;
336-
}
337-
json[kSummary] = summary_json;
338-
339-
if (snapshot.schema_id.has_value()) {
340-
json[kSchemaId] = *snapshot.schema_id;
341-
}
342-
330+
json[kSummary] = snapshot.summary;
331+
SetOptionalField(json, kSchemaId, snapshot.schema_id);
343332
return json;
344333
}
345334

src/iceberg/snapshot.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ ICEBERG_EXPORT constexpr std::string_view SnapshotRefTypeToString(
4848
return "branch";
4949
case SnapshotRefType::kTag:
5050
return "tag";
51-
default:
52-
return "invalid";
5351
}
5452
}
5553
/// \brief Get the relative snapshot reference type from name
@@ -198,11 +196,11 @@ struct SnapshotSummaryFields {
198196
/// Other Fields, see https://iceberg.apache.org/spec/#other-fields
199197

200198
/// \brief The Write-Audit-Publish id of a staged snapshot
201-
inline static const std::string kWAPID = "wap.id";
199+
inline static const std::string kWAPId = "wap.id";
202200
/// \brief The Write-Audit-Publish id of a snapshot already been published
203-
inline static const std::string kPublishedWAPID = "published-wap-id";
201+
inline static const std::string kPublishedWAPId = "published-wap-id";
204202
/// \brief The original id of a cherry-picked snapshot
205-
inline static const std::string kSourceSnapshotID = "source-snapshot-id";
203+
inline static const std::string kSourceSnapshotId = "source-snapshot-id";
206204
/// \brief Name of the engine that created the snapshot
207205
inline static const std::string kEngineName = "engine-name";
208206
/// \brief Version of the engine that created the snapshot

test/json_internal_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "iceberg/sort_order.h"
3333
#include "iceberg/transform.h"
3434
#include "iceberg/util/formatter.h" // IWYU pragma: keep
35+
#include "matchers.h"
3536

3637
namespace iceberg {
3738

@@ -235,9 +236,8 @@ TEST(JsonInternalTest, SnapshotFromJsonWithInvalidSummary) {
235236
auto result = SnapshotFromJson(invalid_json_snapshot);
236237
ASSERT_FALSE(result.has_value());
237238

238-
EXPECT_EQ(result.error().kind, ErrorKind::kJsonParseError);
239-
EXPECT_TRUE(result.error().message.find("Invalid snapshot summary field") !=
240-
std::string::npos);
239+
EXPECT_THAT(result, IsError(ErrorKind::kJsonParseError));
240+
EXPECT_THAT(result, HasErrorMessage("Invalid snapshot summary field"));
241241
}
242242

243243
} // namespace iceberg

0 commit comments

Comments
 (0)