martin-g commented on code in PR #560: URL: https://github.com/apache/avro-rs/pull/560#discussion_r3449011031
########## avro_derive/src/fields.rs: ########## @@ -0,0 +1,182 @@ +// 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::{FieldDefault, With}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::spanned::Spanned; +use syn::{Expr, Field, Type}; + +pub fn get_field_schema_expr(field: &Field, with: With) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_schema_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_schema_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { (#closure)() }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { #path(named_schemas, enclosing_namespace) }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", + )]), + } +} + +pub fn get_field_get_record_fields_expr( + field: &Field, + with: With, +) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_get_record_fields_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_record_fields_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt( + named_schemas, + enclosing_namespace, + |_, _| (#closure)(), + ) + }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt(named_schemas, enclosing_namespace, #path) + }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", + )]), + } +} + +pub fn get_field_field_default_expr( + field: &Field, + default: FieldDefault, +) -> Result<TokenStream, Vec<syn::Error>> { + match default { + FieldDefault::Disabled => Ok(quote! { ::std::option::Option::None }), + FieldDefault::Trait => type_to_field_default_expr(&field.ty), + FieldDefault::Value(default_value) => { + let _: serde_json::Value = serde_json::from_str(&default_value[..]).map_err(|e| { + vec![syn::Error::new( + field.ident.span(), + format!("Invalid avro default json: \n{e}"), + )] + })?; + Ok(quote! { + ::std::option::Option::Some(::serde_json::from_str(#default_value).expect("Unreachable! This parsed at compile time!")) Review Comment: ```suggestion ::std::option::Option::Some(::serde_json::from_str(#default_value).expect("Unreachable! This parsed successfully at compile time!")) ``` ########## avro_derive/src/structs.rs: ########## @@ -0,0 +1,186 @@ +use crate::attributes::{FieldOptions, NamedTypeOptions}; Review Comment: These lines should be below the ASL header comment ########## avro_derive/src/fields.rs: ########## @@ -0,0 +1,182 @@ +// 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::{FieldDefault, With}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::spanned::Spanned; +use syn::{Expr, Field, Type}; + +pub fn get_field_schema_expr(field: &Field, with: With) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_schema_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_schema_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { (#closure)() }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { #path(named_schemas, enclosing_namespace) }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", + )]), + } +} + +pub fn get_field_get_record_fields_expr( + field: &Field, + with: With, +) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_get_record_fields_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_record_fields_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt( + named_schemas, + enclosing_namespace, + |_, _| (#closure)(), + ) + }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt(named_schemas, enclosing_namespace, #path) + }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", Review Comment: ```suggestion "Invalid expression, expected a function or a closure", ``` ########## avro_derive/src/utils.rs: ########## @@ -0,0 +1,67 @@ +// 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 proc_macro2::TokenStream; +use quote::quote; + +/// Convert a list of errors into actual compiler errors. +/// +/// Copied from `serde`, originally written by David Tolnay. +pub fn to_compile_errors(errors: impl AsRef<[syn::Error]>) -> TokenStream { Review Comment: ```suggestion pub(crate) fn to_compile_errors(errors: impl AsRef<[syn::Error]>) -> TokenStream { ``` same for the other functions below ########## avro_derive/src/fields.rs: ########## @@ -0,0 +1,182 @@ +// 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::{FieldDefault, With}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::spanned::Spanned; +use syn::{Expr, Field, Type}; + +pub fn get_field_schema_expr(field: &Field, with: With) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_schema_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_schema_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { (#closure)() }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { #path(named_schemas, enclosing_namespace) }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", + )]), + } +} + +pub fn get_field_get_record_fields_expr( + field: &Field, + with: With, +) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_get_record_fields_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_record_fields_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt( + named_schemas, + enclosing_namespace, + |_, _| (#closure)(), + ) + }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt(named_schemas, enclosing_namespace, #path) + }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", + )]), + } +} + +pub fn get_field_field_default_expr( Review Comment: ```suggestion pub(crate) fn get_field_field_default_expr( ``` Why there are two `field` in the name ? ########## avro_derive/src/utils.rs: ########## @@ -0,0 +1,67 @@ +// 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 proc_macro2::TokenStream; +use quote::quote; + +/// Convert a list of errors into actual compiler errors. +/// +/// Copied from `serde`, originally written by David Tolnay. +pub fn to_compile_errors(errors: impl AsRef<[syn::Error]>) -> TokenStream { + let compile_errors = errors.as_ref().iter().map(syn::Error::to_compile_error); + quote!(#(#compile_errors)*) +} + +pub fn preserve_optional(op: Option<impl quote::ToTokens>) -> TokenStream { + if let Some(tt) = op { + quote! {::std::option::Option::Some(#tt.into())} + } else { + quote! {::std::option::Option::None} + } +} + +/// Convert a list of strings to an expression that resolves to a `Option<Vec<Alias>>`. +pub fn aliases(op: &[impl AsRef<str>]) -> TokenStream { + let items: Vec<TokenStream> = op + .iter() + .map(|alias| { + let alias = alias.as_ref(); + quote! { + ::apache_avro::schema::Alias::new(#alias).expect("Alias is invalid") + } + }) + .collect(); + if items.is_empty() { + quote! {::std::option::Option::None} + } else { + quote! {::std::option::Option::Some(vec![#(#items),*])} + } +} + +/// Convert a list of strings to an expression that resolves to a `Vec<String>`. +pub fn field_aliases(op: &[impl AsRef<str>]) -> TokenStream { + let items: Vec<TokenStream> = op + .iter() + .map(|string| { + let string = string.as_ref(); + quote! { + String::from(#string) Review Comment: ```suggestion ::std::string::String::from(#string) ``` ########## avro_derive/src/fields.rs: ########## @@ -0,0 +1,182 @@ +// 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::{FieldDefault, With}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::spanned::Spanned; +use syn::{Expr, Field, Type}; + +pub fn get_field_schema_expr(field: &Field, with: With) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_schema_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_schema_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { (#closure)() }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { #path(named_schemas, enclosing_namespace) }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", + )]), + } +} + +pub fn get_field_get_record_fields_expr( + field: &Field, + with: With, +) -> Result<TokenStream, Vec<syn::Error>> { + match with { + With::Trait => Ok(type_to_get_record_fields_expr(&field.ty)?), + With::Serde(path) => { + Ok(quote! { #path::get_record_fields_in_ctxt(named_schemas, enclosing_namespace) }) + } + With::Expr(Expr::Closure(closure)) => { + if closure.inputs.is_empty() { + Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt( + named_schemas, + enclosing_namespace, + |_, _| (#closure)(), + ) + }) + } else { + Err(vec![syn::Error::new( + field.span(), + "Expected closure with 0 parameters", + )]) + } + } + With::Expr(Expr::Path(path)) => Ok(quote! { + ::apache_avro::serde::get_record_fields_in_ctxt(named_schemas, enclosing_namespace, #path) + }), + With::Expr(_expr) => Err(vec![syn::Error::new( + field.span(), + "Invalid expression, expected function or closure", + )]), + } +} + +pub fn get_field_field_default_expr( + field: &Field, + default: FieldDefault, +) -> Result<TokenStream, Vec<syn::Error>> { + match default { + FieldDefault::Disabled => Ok(quote! { ::std::option::Option::None }), + FieldDefault::Trait => type_to_field_default_expr(&field.ty), + FieldDefault::Value(default_value) => { + let _: serde_json::Value = serde_json::from_str(&default_value[..]).map_err(|e| { + vec![syn::Error::new( + field.ident.span(), Review Comment: ```suggestion field.span(), ``` as the above functions do -- 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]
