Skip to content

[Variant] Improved API for accessing Variant Objects and lists #7756

@alamb

Description

@alamb

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

After @scovich's PR to validate objects on construction

I expected to be able to use the APIs for variant without havin to check for errors. Also I found the field_by_name very confusing as I expected get() to retrieve a field of a Variant object but instead it was called field_by_name.

Describe the solution you'd like
I want APIs that are infallible (don't return errors) so they are easier to work with

Describe alternatives you've considered

I would personally like to write

let variant_object = variant.as_object().unwrap()
// retrieve the value from a field as Option<Variant>
assert_eq!(variant_object.get("my_field"), Some(Variant::from("foo")));
// non existent fields return None
assert_eq!(variant_object.get("my_non_existent)field"), None);

Likewise for VariantLists I would like to do

let variant_list = variant.as_list().unwrap();
// retrieve item at index 1
assert_eq!(variant_list.get(1), Some(Variant::from("foo")));
/// non existent items return None (like Rust slices)
assert_eq!(variant_list.get(100), None);

Additional context
I noticed this while working on

Metadata

Metadata

Labels

enhancementAny new improvement worthy of a entry in the changelogparquetChanges to the parquet crate

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions