Skip to content

Commit 1c2530c

Browse files
authored
feat: add doc attribute to SchemaField (#96)
1 parent 5aed8f5 commit 1c2530c

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/iceberg/schema_field.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@
2727
namespace iceberg {
2828

2929
SchemaField::SchemaField(int32_t field_id, std::string name, std::shared_ptr<Type> type,
30-
bool optional)
30+
bool optional, std::string doc)
3131
: field_id_(field_id),
3232
name_(std::move(name)),
3333
type_(std::move(type)),
34-
optional_(optional) {}
34+
optional_(optional),
35+
doc_(std::move(doc)) {}
3536

3637
SchemaField SchemaField::MakeOptional(int32_t field_id, std::string name,
37-
std::shared_ptr<Type> type) {
38-
return SchemaField(field_id, std::move(name), std::move(type), true);
38+
std::shared_ptr<Type> type, std::string doc) {
39+
return {field_id, std::move(name), std::move(type), true, std::move(doc)};
3940
}
4041

4142
SchemaField SchemaField::MakeRequired(int32_t field_id, std::string name,
42-
std::shared_ptr<Type> type) {
43-
return SchemaField(field_id, std::move(name), std::move(type), false);
43+
std::shared_ptr<Type> type, std::string doc) {
44+
return {field_id, std::move(name), std::move(type), false, std::move(doc)};
4445
}
4546

4647
int32_t SchemaField::field_id() const { return field_id_; }
@@ -51,9 +52,13 @@ const std::shared_ptr<Type>& SchemaField::type() const { return type_; }
5152

5253
bool SchemaField::optional() const { return optional_; }
5354

55+
std::string_view SchemaField::doc() const { return doc_; }
56+
5457
std::string SchemaField::ToString() const {
55-
return std::format("{} ({}): {} ({})", name_, field_id_, *type_,
56-
optional_ ? "optional" : "required");
58+
std::string result = std::format("{} ({}): {} ({}){}", name_, field_id_, *type_,
59+
optional_ ? "optional" : "required",
60+
!doc_.empty() ? std::format(" - {}", doc_) : "");
61+
return result;
5762
}
5863

5964
bool SchemaField::Equals(const SchemaField& other) const {

src/iceberg/schema_field.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ class ICEBERG_EXPORT SchemaField : public iceberg::util::Formattable {
4444
/// \param[in] name The field name.
4545
/// \param[in] type The field type.
4646
/// \param[in] optional Whether values of this field are required or nullable.
47+
/// \param[in] doc Optional documentation string for the field.
4748
SchemaField(int32_t field_id, std::string name, std::shared_ptr<Type> type,
48-
bool optional);
49+
bool optional, std::string doc = {});
4950

5051
/// \brief Construct an optional (nullable) field.
5152
static SchemaField MakeOptional(int32_t field_id, std::string name,
52-
std::shared_ptr<Type> type);
53+
std::shared_ptr<Type> type, std::string doc = {});
5354
/// \brief Construct a required (non-null) field.
5455
static SchemaField MakeRequired(int32_t field_id, std::string name,
55-
std::shared_ptr<Type> type);
56+
std::shared_ptr<Type> type, std::string doc = {});
5657

5758
/// \brief Get the field ID.
5859
[[nodiscard]] int32_t field_id() const;
@@ -66,6 +67,9 @@ class ICEBERG_EXPORT SchemaField : public iceberg::util::Formattable {
6667
/// \brief Get whether the field is optional.
6768
[[nodiscard]] bool optional() const;
6869

70+
/// \brief Get the field documentation.
71+
std::string_view doc() const;
72+
6973
[[nodiscard]] std::string ToString() const override;
7074

7175
friend bool operator==(const SchemaField& lhs, const SchemaField& rhs) {
@@ -84,6 +88,7 @@ class ICEBERG_EXPORT SchemaField : public iceberg::util::Formattable {
8488
std::string name_;
8589
std::shared_ptr<Type> type_;
8690
bool optional_;
91+
std::string doc_;
8792
};
8893

8994
} // namespace iceberg

test/schema_field_test.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,29 @@ TEST(SchemaFieldTest, Equality) {
7979
ASSERT_EQ(field1, field6);
8080
ASSERT_EQ(field6, field1);
8181
}
82+
83+
TEST(SchemaFieldTest, WithDoc) {
84+
{
85+
iceberg::SchemaField field(/*field_id=*/1, /*name=*/"foo",
86+
std::make_shared<iceberg::IntType>(),
87+
/*optional=*/false, /*doc=*/"Field documentation");
88+
EXPECT_EQ(1, field.field_id());
89+
EXPECT_EQ("foo", field.name());
90+
EXPECT_EQ(iceberg::TypeId::kInt, field.type()->type_id());
91+
EXPECT_FALSE(field.optional());
92+
EXPECT_EQ("Field documentation", field.doc());
93+
EXPECT_EQ("foo (1): int (required) - Field documentation", field.ToString());
94+
}
95+
{
96+
iceberg::SchemaField field = iceberg::SchemaField::MakeOptional(
97+
/*field_id=*/2, /*name=*/"bar",
98+
/*type=*/std::make_shared<iceberg::FixedType>(10),
99+
/*doc=*/"Field with 10 bytes");
100+
EXPECT_EQ(2, field.field_id());
101+
EXPECT_EQ("bar", field.name());
102+
EXPECT_EQ(iceberg::FixedType(10), *field.type());
103+
EXPECT_TRUE(field.optional());
104+
EXPECT_EQ("Field with 10 bytes", field.doc());
105+
EXPECT_EQ("bar (2): fixed(10) (optional) - Field with 10 bytes", field.ToString());
106+
}
107+
}

0 commit comments

Comments
 (0)