Kriskras99 commented on code in PR #512:
URL: https://github.com/apache/avro-rs/pull/512#discussion_r2969304859
##########
avro/src/schema/union.rs:
##########
@@ -74,7 +81,116 @@ impl UnionSchema {
&self.schemas
}
- /// Returns true if the any of the variants of this `UnionSchema` is
`Null`.
+ /// Get the variant at the given index.
+ pub fn get_variant(&self, index: usize) -> Result<&Schema, Error> {
+ self.schemas.get(index).ok_or_else(|| {
+ Details::GetUnionVariant {
+ index: index as i64,
+ num_variants: self.schemas.len(),
+ }
+ .into()
+ })
+ }
+
+ /// Get the index of the provided [`SchemaKind`].
+ ///
+ /// The schema must not be a logical type, as only the base type are saved
in the lookup index.
+ pub(crate) fn index_of_schema_kind(&self, kind: SchemaKind) ->
Option<usize> {
+ self.variant_index.get(&kind).copied()
+ }
+
+ /// Get the index and schema for the provided name.
+ ///
+ /// Will use `names` to resolve references.
+ pub(crate) fn find_named_schema<'s>(
+ &'s self,
+ name: &str,
+ names: &'s HashMap<Name, impl Borrow<Schema>>,
+ ) -> Result<Option<(usize, &'s Schema)>, Error> {
+ for index in self.named_index.iter().copied() {
+ let schema = &self.schemas[index];
+ if let Some(schema_name) = schema.name()
+ && schema_name.name() == name
+ {
+ let schema = if let Schema::Ref { name } = schema {
+ names
+ .get(name)
+ .ok_or_else(||
Details::SchemaResolutionError(name.clone()))?
+ .borrow()
+ } else {
+ schema
+ };
+ return Ok(Some((index, schema)));
+ }
+ }
+ Ok(None)
+ }
+
+ /// Find a [`Schema::Fixed`] with the given size.
+ ///
+ /// Will use `names` to resolve references.
+ pub(crate) fn find_fixed_of_size_n<'s>(
+ &'s self,
+ size: usize,
+ names: &'s HashMap<Name, impl Borrow<Schema>>,
+ ) -> Result<Option<(usize, &'s FixedSchema)>, Error> {
+ for index in self.named_index.iter().copied() {
+ let schema = &self.schemas[index];
+ let schema = if let Schema::Ref { name } = schema {
+ names
+ .get(name)
+ .ok_or_else(||
Details::SchemaResolutionError(name.clone()))?
+ .borrow()
+ } else {
+ schema
+ };
+ match schema {
+ Schema::Fixed(fixed)
+ | Schema::Uuid(UuidSchema::Fixed(fixed))
+ | Schema::Decimal(DecimalSchema {
+ inner: InnerDecimalSchema::Fixed(fixed),
+ ..
+ })
+ | Schema::Duration(fixed)
+ if fixed.size == size =>
+ {
+ return Ok(Some((index, fixed)));
+ }
+ _ => {}
+ }
+ }
+ Ok(None)
+ }
+
+ /// Find a [`Schema::Record`] with `n` fields.
+ ///
+ /// Will use `names` to resolve references.
+ pub(crate) fn find_record_with_n_fields<'s>(
Review Comment:
It's used in the serializer for the `tuple` and `map` cases. We don't have a
set name for a tuple, so we just use the first record with the same amount of
fields.
For `serialize_map` it's used for records with flattened fields, because we
don't get the name. This is an improvement on the previous serialize
implementation that would just pick the first map/record/reference.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]