Kriskras99 commented on code in PR #569: URL: https://github.com/apache/avro-rs/pull/569#discussion_r3492982688
########## avro_derive/src/enums/bare_union.rs: ########## @@ -0,0 +1,107 @@ +// 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. + +use crate::attributes::{NamedTypeOptions, Repr, VariantOptions}; +use crate::enums::{FieldVariants, SchemaType, variant_to_schema_expr}; +use crate::implementation::Implementation; +use crate::utils::json_value_expr; +use proc_macro2::Ident; +use quote::quote; +use std::collections::HashSet; +use syn::spanned::Spanned; +use syn::{DataEnum, Generics}; + +pub fn to_implementation( + ident: Ident, + generics: Generics, + container_attrs: NamedTypeOptions, + data: DataEnum, +) -> Result<Implementation, Vec<syn::Error>> { + let Some(Repr::BareUnion { untagged }) = container_attrs.repr else { + unreachable!() + }; + + let mut errors = Vec::new(); + let mut variant_exprs = Vec::new(); + + // Used for checking that the resulting schema is usefull + let mut have_null = false; + let mut names = HashSet::new(); + let mut tuple_sizes = HashSet::new(); + + for variant in data.variants { + let variant_span = variant.span(); + let variant_attrs = match VariantOptions::new(&variant.attrs, variant_span) { + Ok(attrs) => attrs, + Err(errs) => { + errors.extend(errs); + continue; + } + }; + + if variant_attrs.skip { + continue; + } + match variant_to_schema_expr( + variant, + variant_attrs, + container_attrs.rename_all, + container_attrs.rename_all_fields, + true, + true, + |info| { + match (info.schema_type, info.variant) { + (_, FieldVariants::Named(0)) if untagged => { + Err("AvroSchema: Empty struct variants are not allowed for `#[serde(untagged)]`".to_string()) + } + (_, FieldVariants::Unnamed(len)) if untagged && len >= 2 && !tuple_sizes.insert(len) => { Review Comment: An untagged bare union will do the following to tuple variants of different sizes: - `()`: `Schema::Null` - `(T1)`: `T1::get_schema_in_ctxt` - `(T1,..,Tn`): `Schema::Record` So only for tuple variants of size 2 or higher we need to check if their sizes are unique because the serializer will look for the first record schema (with the tuple attribute) that has the same length. For tuple variants of size 1, it would be nice to check that the schemas are unique at compile time but we don't have that information, and it is caught at schema construction by the Union builder. For size 0 we have the `(SchemaType::Null, _) if have_null` check. I've added the following comment to the check: ``` // Tuples of size 2 or larger are represented as a Schema::Record with an "org.apache.avro.rust.tuple" attribute. // As the only information Serde provides for untagged enums is the size of the tuple, the amount of fields in // records with the tuple tag must be unique for it to find the correct variant. // This check is not watertight as it will fail for `enum Abc { A((i32, i32)), B(i64, i64) }` // as we don't know the schema for the newtype inside Abc::A. ``` -- 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]
