Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make find_field_with_default return json fields without path #2476

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl Schema {
.or(default_field_opt.map(|field| (field, full_path)))?;
let field_entry = self.get_field_entry(field);
let is_json = field_entry.field_type().value_type() == Type::Json;
if is_json == json_path.is_empty() {
if !is_json && !json_path.is_empty() {
return None;
}
Some((field, json_path))
Expand Down Expand Up @@ -960,4 +960,64 @@ mod tests {
assert_eq!(schema.find_field("thiswouldbeareallylongfieldname"), None);
assert_eq!(schema.find_field("baz.bar.foo"), None);
}

#[test]
fn test_find_field_with_default() {
let mut schema_builder = Schema::builder();
schema_builder.add_json_field("_default", JsonObjectOptions::default());
let default = Field::from_field_id(0);
schema_builder.add_json_field("foo", STRING);
let foo = Field::from_field_id(1);
schema_builder.add_text_field("foo.bar", STRING);
let foo_bar = Field::from_field_id(2);
schema_builder.add_text_field("bar", STRING);
let bar = Field::from_field_id(3);
schema_builder.add_json_field("baz", JsonObjectOptions::default());
let baz = Field::from_field_id(4);
let schema = schema_builder.build();

assert_eq!(schema.find_field_with_default("foo", None), Some((foo, "")));
assert_eq!(
schema.find_field_with_default("foo.bar", None),
Some((foo_bar, ""))
);
assert_eq!(schema.find_field_with_default("bar", None), Some((bar, "")));
assert_eq!(schema.find_field_with_default("bar.baz", None), None);
assert_eq!(schema.find_field_with_default("baz", None), Some((baz, "")));
assert_eq!(
schema.find_field_with_default("baz.foobar", None),
Some((baz, "foobar"))
);
assert_eq!(schema.find_field_with_default("foobar", None), None);

assert_eq!(
schema.find_field_with_default("foo", Some(default)),
Some((foo, ""))
);
assert_eq!(
schema.find_field_with_default("foo.bar", Some(default)),
Some((foo_bar, ""))
);
assert_eq!(
schema.find_field_with_default("bar", Some(default)),
Some((bar, ""))
);
// still None, we are under an existing field
assert_eq!(
schema.find_field_with_default("bar.baz", Some(default)),
None
);
assert_eq!(
schema.find_field_with_default("baz", Some(default)),
Some((baz, ""))
);
assert_eq!(
schema.find_field_with_default("baz.foobar", Some(default)),
Some((baz, "foobar"))
);
assert_eq!(
schema.find_field_with_default("foobar", Some(default)),
Some((default, "foobar"))
);
}
}
Loading