This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 9471469 feat: `RecordSchema/RecordField::builder().name()` accepts
`Into<String>` (#429)
9471469 is described below
commit 94714695e6725c268d16f3e0f5b88aaea80b08de
Author: Martin Grigorov <[email protected]>
AuthorDate: Fri Jan 23 22:50:57 2026 +0200
feat: `RecordSchema/RecordField::builder().name()` accepts `Into<String>`
(#429)
* feat: `RecordField::builder().name()` accepts `Into<String>`
* feat: Add `RecordSchemaBuilder::try_name(impl Into<String>)`
Usage:
```rust
RecordSchema::builder().try_name("str_slice")?.build();
```
* API improvements
Co-authored-by: Kriskras99 <[email protected]>
* Use full paths for the Bon types
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
* Use RecordSchema::builder().try_name() in unit tests
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
---------
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Co-authored-by: Kriskras99 <[email protected]>
Fixes #419
---
avro/src/schema/mod.rs | 2 +-
avro/src/schema/record/field.rs | 18 ++++++++++++++++++
avro/src/schema/record/schema.rs | 32 ++++++++++++++++++++++++++++++--
avro/src/schema/union.rs | 12 ++----------
4 files changed, 51 insertions(+), 13 deletions(-)
diff --git a/avro/src/schema/mod.rs b/avro/src/schema/mod.rs
index cff2a2f..f77854e 100644
--- a/avro/src/schema/mod.rs
+++ b/avro/src/schema/mod.rs
@@ -1710,7 +1710,7 @@ mod tests {
let schema_c_expected = Schema::Record(
RecordSchema::builder()
- .name(Name::new("C")?)
+ .try_name("C")?
.fields(vec![
RecordField::builder()
.name("field_one".to_string())
diff --git a/avro/src/schema/record/field.rs b/avro/src/schema/record/field.rs
index 9a42310..6e70cba 100644
--- a/avro/src/schema/record/field.rs
+++ b/avro/src/schema/record/field.rs
@@ -34,6 +34,7 @@ use strum_macros::EnumString;
#[derive(bon::Builder, Clone, Debug, PartialEq)]
pub struct RecordField {
/// Name of the field.
+ #[builder(into)]
pub name: String,
/// Documentation of the field.
#[builder(default)]
@@ -265,4 +266,21 @@ mod tests {
assert!(!non_nullable_record_field.is_nullable());
Ok(())
}
+
+ #[test]
+ fn avro_rs_419_name_into() -> TestResult {
+ let field = RecordField::builder()
+ .name("str_slice")
+ .schema(Schema::Boolean)
+ .build();
+ assert_eq!(field.name, "str_slice");
+
+ let field = RecordField::builder()
+ .name("String".to_string())
+ .schema(Schema::Boolean)
+ .build();
+ assert_eq!(field.name, "String");
+
+ Ok(())
+ }
}
diff --git a/avro/src/schema/record/schema.rs b/avro/src/schema/record/schema.rs
index c1c7a73..297dde9 100644
--- a/avro/src/schema/record/schema.rs
+++ b/avro/src/schema/record/schema.rs
@@ -42,6 +42,21 @@ pub struct RecordSchema {
pub attributes: BTreeMap<String, Value>,
}
+impl<S: record_schema_builder::State> RecordSchemaBuilder<S> {
+ /// Try to set a Name from the given string.
+ pub fn try_name<T>(
+ self,
+ name: T,
+ ) -> Result<RecordSchemaBuilder<record_schema_builder::SetName<S>>, <T as
TryInto<Name>>::Error>
+ where
+ <S as record_schema_builder::State>::Name:
record_schema_builder::IsUnset,
+ T: TryInto<Name>,
+ {
+ let name = name.try_into()?;
+ Ok(self.name(name))
+ }
+}
+
/// Calculate the lookup table for the given fields.
fn calculate_lookup_table(fields: &[RecordField]) -> BTreeMap<String, usize> {
fields
@@ -152,11 +167,11 @@ mod tests {
let name = Name::new("TestRecord")?;
let fields = vec![
RecordField::builder()
- .name("field1_null".into())
+ .name("field1_null")
.schema(Schema::Null)
.build(),
RecordField::builder()
- .name("field2_bool".into())
+ .name("field2_bool")
.schema(Schema::Boolean)
.build(),
];
@@ -181,4 +196,17 @@ mod tests {
Ok(())
}
+
+ #[test]
+ fn avro_rs_419_name_into() -> TestResult {
+ let schema = RecordSchema::builder().try_name("str_slice")?.build();
+ assert_eq!(schema.name, "str_slice".try_into()?);
+
+ let schema = RecordSchema::builder()
+ .try_name("String".to_string())?
+ .build();
+ assert_eq!(schema.name, "String".try_into()?);
+
+ Ok(())
+ }
}
diff --git a/avro/src/schema/union.rs b/avro/src/schema/union.rs
index 3b26379..5bf631a 100644
--- a/avro/src/schema/union.rs
+++ b/avro/src/schema/union.rs
@@ -151,16 +151,8 @@ mod tests {
#[test]
fn avro_rs_402_new_union_schema_duplicate_names() -> TestResult {
let res = UnionSchema::new(vec![
- Schema::Record(
- RecordSchema::builder()
- .name("Same_name".try_into()?)
- .build(),
- ),
- Schema::Record(
- RecordSchema::builder()
- .name("Same_name".try_into()?)
- .build(),
- ),
+
Schema::Record(RecordSchema::builder().try_name("Same_name")?.build()),
+
Schema::Record(RecordSchema::builder().try_name("Same_name")?.build()),
])
.map_err(Error::into_details);