Skip to content

Commit

Permalink
fix: enable field_with_name to support nested fields with '.' delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwilcoxson-rel authored and rtyler committed May 17, 2024
1 parent 507c3a3 commit 402ddf9
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion crates/core/src/kernel/models/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,19 @@ impl StructType {

/// Returns a reference of a specific [`StructField`] instance selected by name.
pub fn field_with_name(&self, name: &str) -> Result<&StructField, Error> {
Ok(&self.fields[self.index_of(name)?])
match name.split_once('.') {
Some((parent, children)) => {
let parent_field = &self.fields[self.index_of(parent)?];
match parent_field.data_type {
DataType::Struct(ref inner) => Ok(inner.field_with_name(children)?),
_ => Err(Error::Schema(format!(
"Field {} is not a struct type",
parent_field.name()
))),
}
}
None => Ok(&self.fields[self.index_of(name)?]),
}
}

/// Get all invariants in the schemas
Expand Down Expand Up @@ -983,4 +995,27 @@ mod tests {
);
assert_eq!(get_hash(&field_1), get_hash(&field_2));
}

#[test]
fn test_field_with_name() {
let schema = StructType::new(vec![
StructField::new("a", DataType::STRING, true),
StructField::new("b", DataType::INTEGER, true),
]);
let field = schema.field_with_name("b").unwrap();
assert_eq!(*field, StructField::new("b", DataType::INTEGER, true));
}

#[test]
fn test_field_with_name_nested() {
let nested = StructType::new(vec![StructField::new("a", DataType::BOOLEAN, true)]);
let schema = StructType::new(vec![
StructField::new("a", DataType::STRING, true),
StructField::new("b", DataType::Struct(Box::new(nested)), true),
]);

let field = schema.field_with_name("b.a").unwrap();

assert_eq!(*field, StructField::new("a", DataType::BOOLEAN, true));
}
}

0 comments on commit 402ddf9

Please sign in to comment.