forked from apache/arrow-rs
-
Notifications
You must be signed in to change notification settings - Fork 2
Add documentation and examples to VariantBuilder #1
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
Merged
PinkCrow007
merged 1 commit into
PinkCrow007:variant-builder
from
alamb:alamb/variant_builder_docs
Jun 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,98 @@ fn make_room_for_header(buffer: &mut Vec<u8>, start_pos: usize, header_size: usi | |
| buffer.copy_within(src_start..src_end, dst_start); | ||
| } | ||
|
|
||
| /// Builder for [`Variant`] values | ||
| /// | ||
| /// # Example: create a Primitive Int8 | ||
| /// ``` | ||
| /// # use parquet_variant::{Variant, VariantBuilder, VariantMetadata}; | ||
| /// let mut builder = VariantBuilder::new(); | ||
| /// builder.append_value(Variant::Int8(42)); | ||
| /// // Finish the builder to get the metadata and value | ||
| /// let (metadata, value) = builder.finish(); | ||
| /// // use the Variant API to verify the result | ||
| /// let metadata = VariantMetadata::try_new(&metadata).unwrap(); | ||
| /// let variant = Variant::try_new(&metadata, &value).unwrap(); | ||
| /// assert_eq!(variant, Variant::Int8(42)); | ||
| /// ``` | ||
| /// | ||
| /// # Example: Create an Object | ||
| /// This example shows how to create an object with two fields: | ||
| /// ```json | ||
| /// { | ||
| /// "first_name": "Jiaying", | ||
| /// "last_name": "Li" | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ``` | ||
| /// # use parquet_variant::{Variant, VariantBuilder, VariantMetadata}; | ||
| /// let mut builder = VariantBuilder::new(); | ||
| /// // Create an object builder that will write fields to the object | ||
| /// let mut object_builder = builder.new_object(); | ||
| /// object_builder.append_value("first_name", "Jiaying"); | ||
| /// object_builder.append_value("last_name", "Li"); | ||
| /// object_builder.finish(); | ||
| /// // Finish the builder to get the metadata and value | ||
| /// let (metadata, value) = builder.finish(); | ||
| /// // use the Variant API to verify the result | ||
| /// let metadata = VariantMetadata::try_new(&metadata).unwrap(); | ||
| /// let variant = Variant::try_new(&metadata, &value).unwrap(); | ||
| /// let Variant::Object(variant_object) = variant else { | ||
| /// panic!("unexpected variant type") | ||
| /// }; | ||
| /// /* TODO: uncomment this, but now VariantObject:field is not implemented | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filed |
||
| /// assert_eq!( | ||
| /// variant_object.field("first_name").unwrap(), | ||
| /// Variant::String("Jiaying") | ||
| /// ); | ||
| /// assert_eq!( | ||
| /// variant_object.field("last_name").unwrap(), | ||
| /// Variant::String("Li") | ||
| /// ); | ||
| /// */ | ||
| /// ``` | ||
| /// | ||
| /// # Example: Create an Array | ||
| /// | ||
| /// This example shows how to create an array of integers: `[1, 2, 3]`. | ||
| /// (this test actually fails at the moment) | ||
| /// ``` | ||
| /// # use parquet_variant::{Variant, VariantBuilder, VariantMetadata}; | ||
| /// let mut builder = VariantBuilder::new(); | ||
| /// // Create an array builder that will write elements to the array | ||
| /// let mut array_builder = builder.new_array(); | ||
| /// array_builder.append_value(1i8); | ||
| /// array_builder.append_value(2i8); | ||
| /// array_builder.append_value(3i8); | ||
| /// // Finish the builder to get the metadata and value | ||
| /// let (metadata, value) = builder.finish(); | ||
| /// // use the Variant API to verify the result | ||
| /// let metadata = VariantMetadata::try_new(&metadata).unwrap(); | ||
| /// let variant = Variant::try_new(&metadata, &value).unwrap(); | ||
| /// let Variant::Object(variant_object) = variant else { | ||
| /// panic!("unexpected variant type") | ||
| /// }; | ||
| /// // TODO: VERIFY THE RESULT this, but now VariantObject:field is not implemented | ||
| /// ``` | ||
| /// | ||
| /// # Example: Array of objects | ||
| /// | ||
| /// THis example shows how to create an array of objects: | ||
| /// ```json | ||
| /// [ | ||
| /// { | ||
| /// "first_name": "Jiaying", | ||
| /// "last_name": "Li" | ||
| /// }, | ||
| /// { | ||
| /// "first_name": "Malthe", | ||
| /// "last_name": "Karbo" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fyi @mkarbo |
||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// TODO | ||
| /// | ||
| pub struct VariantBuilder { | ||
| buffer: Vec<u8>, | ||
| dict: HashMap<String, u32>, | ||
|
|
@@ -122,10 +214,16 @@ impl VariantBuilder { | |
| self.buffer.len() | ||
| } | ||
|
|
||
| /// Create an [`ArrayBuilder`] for creating [`Variant::Array`] values. | ||
| /// | ||
| /// See the examples on [`VariantBuilder`] for usage. | ||
| pub fn new_array(&mut self) -> ArrayBuilder { | ||
| ArrayBuilder::new(self) | ||
| } | ||
|
|
||
| /// Create an [`ObjectBuilder`] for creating [`Variant::Object`] values. | ||
| /// | ||
| /// See the examples on [`VariantBuilder`] for usage. | ||
| pub fn new_object(&mut self) -> ObjectBuilder { | ||
| ObjectBuilder::new(self) | ||
| } | ||
|
|
@@ -198,6 +296,9 @@ impl Default for VariantBuilder { | |
| } | ||
| } | ||
|
|
||
| /// A builder for creating [`Variant::Array`] values. | ||
| /// | ||
| /// See the examples on [`VariantBuilder`] for usage. | ||
| pub struct ArrayBuilder<'a> { | ||
| parent: &'a mut VariantBuilder, | ||
| start_pos: usize, | ||
|
|
@@ -255,6 +356,9 @@ impl<'a> ArrayBuilder<'a> { | |
| } | ||
| } | ||
|
|
||
| /// A builder for creating [`Variant::Object`] values. | ||
| /// | ||
| /// See the examples on [`VariantBuilder`] for usage. | ||
| pub struct ObjectBuilder<'a> { | ||
| parent: &'a mut VariantBuilder, | ||
| start_pos: usize, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PinkCrow007 I hope you don't mind this reference, but I think it is nice to leave some sort of personal legacy in code / examples. Your name will live on whenever someone uses this code / sees these docs