forked from apache/avro-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
512: feat!: Support enums and tuples in SchemaAwareSerializer and implement SchemaAwareDeserializer
#65
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
Open
martin-augment
wants to merge
12
commits into
main
Choose a base branch
from
pr-512-2026-03-23-07-11-45
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
512: feat!: Support enums and tuples in SchemaAwareSerializer and implement SchemaAwareDeserializer
#65
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
226d4c2
doc: Document the mapping between the Serde and Avro data models
Kriskras99 f1462c3
feat: `SchemaAwareDeserializer` (no tests)
Kriskras99 7b4c2dc
feat: Use `SchemaAwareDeserializer` in the readers and add tests
Kriskras99 a010ca6
feat: Rework `SchemaAwareSerializer` to be more strict and follow the…
Kriskras99 b1db20f
feat: Update tests and provide support for old ways of `BigDecimal` a…
Kriskras99 757c25e
fix: Add missing license header and set PR number for tests
Kriskras99 e2055f5
fix: Don't call `T::field_default()` if the schema is overwritten usi…
Kriskras99 3d03097
fix: Enable `rustdoc_internal` feature when `cfg(docsrs)` so we can u…
Kriskras99 faeb5ba
fix: Review feedback
Kriskras99 bc52d9f
fix: Apply suggestions from code review
Kriskras99 2ced99b
fix: Second round of code review
Kriskras99 ecaf964
fix: Use longs when serializing block item count and byte size
Kriskras99 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
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
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! # Mapping the Avro data model to the Serde data model | ||
| //! | ||
| //! When manually mapping an Avro schema to Rust types it is important to understand | ||
| //! how the different data models are mapped. When mapping from Rust types to an Avro schema, | ||
| //! see [the documentation for the reverse](super::serde_data_model_to_avro). | ||
| //! | ||
| //! Only the mapping as defined here is supported. Any other behavior might change in a minor version. | ||
| //! | ||
| //! ## Primitive Types | ||
| //! - `null`: `()` | ||
| //! - `boolean`: [`bool`] | ||
| //! - `int`: [`i32`] (or [`i16`], [`i8`], [`u16`], [`u8`]) | ||
| //! - `long`: [`i64`] (or [`u32`]) | ||
| //! - `float`: [`f32`] | ||
| //! - `double`: [`f64`] | ||
| //! - `bytes`: [`Vec<u8>`](std::vec::Vec) (or any type that uses [`Serializer::serialize_bytes`](serde::Serializer), [`Deserializer::deserialize_bytes`](serde::Deserializer), [`Deserializer::deserialize_byte_buf`](serde::Deserializer)) | ||
| //! - It is required to use [`apache_avro::serde::bytes`] as otherwise Serde will (de)serialize a `Vec` as an array of integers instead. | ||
| //! - `string`: [`String`] (or any type that uses [`Serializer::serialize_str`](serde::Serializer), [`Deserializer::deserialize_str`](serde::Deserializer), [`Deserializer::deserialize_string`](serde::Deserializer)) | ||
| //! | ||
| //! ## Complex Types | ||
| //! - `records`: A struct with the same name and fields or a tuple with the same fields. | ||
| //! - Extra fields can be added to the struct if they are marked with `#[serde(skip)]` | ||
| //! - `enums`: A enum with the same name and unit variants for every symbol | ||
| //! - The index of the symbol must match the index of the variant | ||
| //! - `arrays`: [`Vec`] (or any type that uses [`Serializer::serialize_seq`](serde::Serializer), [`Deserializer::deserialize_seq`](serde::Deserializer)) | ||
| //! - `[T; N]` is (de)serialized as a tuple by Serde, to (de)serialize them as an `array` use [`apache_avro::serde::array`] | ||
| //! - `maps`: [`HashMap<String, _>`](std::collections::HashMap) (or any type that uses [`Serializer::serialize_map`](serde::Serializer), [`Deserializer::deserialize_map`](serde::Deserializer)) | ||
| //! - `unions`: An enum with a variant for each union variant | ||
| //! - The index of the union variant must match the enum variant | ||
| //! - A `null` can be a unit variant or a newtype variant with a unit type | ||
| //! - All other variants must be newtype variants, struct variants, or tuple variants. | ||
| //! - `fixed`: [`Vec<u8>`](std::vec::Vec) (or any type that uses [`Serializer::serialize_bytes`](serde::Serializer), [`Deserializer::deserialize_bytes`](serde::Deserializer), [`Deserializer::deserialize_byte_buf`](serde::Deserializer)) | ||
| //! - It is required to use [`apache_avro::serde::fixed`] as otherwise Serde will (de)serialize a `Vec` as an array of integers instead. | ||
| //! | ||
| //! [`apache_avro::serde::array`]: crate::serde::array | ||
| //! [`apache_avro::serde::bytes`]: crate::serde::bytes | ||
| //! [`apache_avro::serde::fixed`]: crate::serde::fixed |
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
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 |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! # Mapping the Serde data model to the Avro data model | ||
| //! | ||
| //! When manually mapping Rust types to an Avro schema, or the reverse, it is important to understand | ||
| //! how the different data models are mapped. When mapping from an Avro schema to Rust types, | ||
| //! see [the documentation for the reverse](super::avro_data_model_to_serde). | ||
| //! | ||
| //! Only the schemas generated by the [`AvroSchema`] derive and the mapping as defined here are | ||
| //! supported. Any other behavior might change in a minor version. | ||
| //! | ||
| //! The following list is based on [the data model defined by Serde](https://serde.rs/data-model.html): | ||
| //! - **14 primitive types** | ||
| //! - `bool` => [`Schema::Boolean`] | ||
| //! - `i8`, `i16`, `i32`, `u8`, `u16` => [`Schema::Int`] | ||
| //! - `i64`, `u32` => [`Schema::Long`] | ||
| //! - `u64` => [`Schema::Fixed`]`(name: "u64", size: 8)` | ||
| //! - This is not a `Schema::Long` as that is a signed number of maximum 64 bits. | ||
| //! - `i128` => [`Schema::Fixed`]`(name: "i128", size: 16)` | ||
| //! - `u128` => [`Schema::Fixed`]`(name: "u128", size: 16)` | ||
| //! - `f32` => [`Schema::Float`] | ||
| //! - `f64` => [`Schema::Double`] | ||
| //! - `char` => [`Schema::String`] | ||
| //! - Only one character allowed, deserializer will return an error for strings with more than one character. | ||
| //! - **string** => [`Schema::String`] | ||
| //! - **byte array** => [`Schema::Bytes`] or [`Schema::Fixed`] | ||
| //! - **option** => [`Schema::Union([Schema::Null, _])`](crate::schema::Schema::Union) | ||
| //! - **unit** => [`Schema::Null`] | ||
| //! - **unit struct** => [`Schema::Record`] with the unqualified name equal to the name of the struct and zero fields | ||
| //! - **unit variant** => See [Enums](#enums) | ||
| //! - **newtype struct** => [`Schema::Record`] with the unqualified name equal to the name of the struct and one field | ||
| //! - **newtype variant** => See [Enums](#enums) | ||
| //! - **seq** => [`Schema::Array`] | ||
| //! - **tuple** | ||
| //! - For tuples with one element, the schema will be the schema of the only element | ||
| //! - For tuples with more than one element, the schema will be a [`Schema::Record`] with as many fields as there are elements. | ||
| //! The schema must have the attribute `org.apache.avro.rust.tuple` with the value set to `true`. | ||
| //! - **Note:** Serde (de)serializes arrays (`[T; N]`) as tuples. To (de)serialize an array as a | ||
| //! [`Schema::Array`] use [`apache_avro::serde::array`]. | ||
| //! - **tuple_struct** => [`Schema::Record`] with the unqualified name equal to the name of the struct and as many fields as there are elements | ||
| //! - **Note:** Tuple structs with 0 or 1 elements will also be (de)serialized as a [`Schema::Record`]. This | ||
| //! is different from normal tuples`. | ||
| //! - **tuple_variant** => See [Enums](#enums) | ||
| //! - **map** => [`Schema::Map`] | ||
| //! - **Note:** the key type of the map will be (de)serialized as a [`Schema::String`] | ||
| //! - **struct** => [`Schema::Record`] | ||
| //! - **struct_variant** => See [Enums](#enums) | ||
| //! | ||
| //! ## Enums | ||
| //! | ||
| //! ### Externally tagged | ||
| //! This is the default enum representation for Serde. It can be mapped in three ways to the Avro data model. | ||
| //! For all three options it is important that the enum index matches the Avro index. | ||
| //! - As a [`Schema::Enum`], this is only possible for enums with only unit variants. | ||
| //! - As a [`Schema::Union`] with a [`Schema::Record`] for every variant: | ||
| //! - **unit_variant** => [`Schema::Record`] named as the variant and with no fields. | ||
| //! - **newtype_variant** => [`Schema::Record`] named as the variant and with one field. | ||
| //! The schema must have the attribute `org.apache.avro.rust.union_of_records` with the value set to `true`. | ||
| //! - **tuple_variant** => [`Schema::Record`] named as the variant and with as many fields as there are element. | ||
| //! - **struct_variant** => [`Schema::Record`] named as the variant and with a field for every field of the struct variant. | ||
| //! - As a [`Schema::Union`] without the wrapper [`Schema::Record`], all schemas must be unique: | ||
| //! - **unit_variant** => [`Schema::Null`]. | ||
| //! - **newtype_variant** => The schema of the inner type. | ||
| //! - **tuple_variant** => [`Schema::Record`] named as the variant and with as many fields as there are element. | ||
| //! - **struct_variant** => [`Schema::Record`] named as the variant and with a field for every field of the struct variant. | ||
| //! | ||
| //! ### Internally tagged | ||
| //! This enum representation is used by Serde if the attribute `#[serde(tag = "...")]` is used. | ||
| //! It maps to a [`Schema::Record`]. There must be at least one field that is named as the value of the | ||
| //! `tag` attribute. If a field is not used by all variants it must have a `default` set. | ||
| //! | ||
| //! ### Adjacently tagged | ||
| //! This enum representation is used by Serde if the attributes `#[serde(tag = "...", content = "...")]` are used. | ||
| //! It maps to a [`Schema::Record`] with two fields. One field must be named as the value of the `tag` | ||
| //! attribute and use the [`Schema::Enum`] schema. The other field must be named as the value of the | ||
| //! `content` tag and use the [`Schema::Union`] schema. | ||
| //! | ||
| //! ### Untagged | ||
| //! This enum representation is used by Serde if the attribute `#[serde(untagged)]` is used. It maps | ||
| //! to a [`Schema::Union`] with the following schemas: | ||
| //! - **unit_variant** => [`Schema::Null`]. | ||
| //! - **newtype_variant** => The schema of the inner type. | ||
| //! - **tuple_variant** => [`Schema::Record`] named as the variant and with as many fields as there are element. | ||
| //! - **struct_variant** => [`Schema::Record`] named as the variant and with a field for every field of the struct variant. | ||
| //! | ||
| //! [`AvroSchema`]: crate::AvroSchema | ||
| //! [`Schema::Array`]: crate::schema::Schema::Array | ||
| //! [`Schema::Boolean`]: crate::schema::Schema::Boolean | ||
| //! [`Schema::Bytes`]: crate::schema::Schema::Bytes | ||
| //! [`Schema::Double`]: crate::schema::Schema::Double | ||
| //! [`Schema::Enum`]: crate::schema::Schema::Enum | ||
| //! [`Schema::Fixed`]: crate::schema::Schema::Fixed | ||
| //! [`Schema::Float`]: crate::schema::Schema::Float | ||
| //! [`Schema::Int`]: crate::schema::Schema::Int | ||
| //! [`Schema::Long`]: crate::schema::Schema::Long | ||
| //! [`Schema::Map`]: crate::schema::Schema::Map | ||
| //! [`Schema::Null`]: crate::schema::Schema::Null | ||
| //! [`Schema::Record`]: crate::schema::Schema::Record | ||
| //! [`Schema::String`]: crate::schema::Schema::String | ||
| //! [`Schema::Union`]: crate::schema::Schema::Union | ||
| //! [`apache_avro::serde::array`]: crate::serde::array | ||
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
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
Oops, something went wrong.
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.
Minor typo: stray backtick before period.
Line 57 has "normal tuples`." with a backtick before the period that appears unintentional.
Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents
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.
value:good-to-have; category:bug; feedback: The CodeRabbit AI reviewer is correct! There is a trailing backtick in the sentence and it may break the rendering of the rustdoc. It should be removed.