|
| 1 | +use proc_macro2::{Ident, TokenStream}; |
| 2 | +use quote::{quote, quote_spanned}; |
| 3 | +use syn::spanned::Spanned; |
| 4 | +use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, PathArguments, Type}; |
| 5 | + |
| 6 | +/// Derive a `deltakernel::schemas::ToDataType` implementation for the annotated struct. The actual |
| 7 | +/// field names in the schema (and therefore of the struct members) are all mandated by the Delta |
| 8 | +/// spec, and so the user of this macro is responsible for ensuring that |
| 9 | +/// e.g. `Metadata::schema_string` is the snake_case-ified version of `schemaString` from [Delta's |
| 10 | +/// Change Metadata](https://github.com/delta-io/delta/blob/master/PROTOCOL.md#change-metadata) |
| 11 | +/// action (this macro allows the use of standard rust snake_case, and will convert to the correct |
| 12 | +/// delta schema camelCase version). |
| 13 | +#[proc_macro_derive(Schema)] |
| 14 | +pub fn derive_schema(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 15 | + let input = parse_macro_input!(input as DeriveInput); |
| 16 | + let struct_ident = input.ident; |
| 17 | + |
| 18 | + let schema_fields = gen_schema_fields(&input.data); |
| 19 | + let output = quote! { |
| 20 | + #[automatically_derived] |
| 21 | + impl crate::actions::schemas::ToDataType for #struct_ident { |
| 22 | + fn to_data_type() -> crate::schema::DataType { |
| 23 | + use crate::actions::schemas::{ToDataType, GetStructField}; |
| 24 | + crate::schema::StructType::new(vec![ |
| 25 | + #schema_fields |
| 26 | + ]).into() |
| 27 | + } |
| 28 | + } |
| 29 | + }; |
| 30 | + proc_macro::TokenStream::from(output) |
| 31 | +} |
| 32 | + |
| 33 | +// turn our struct name into the schema name, goes from snake_case to camelCase |
| 34 | +fn get_schema_name(name: &Ident) -> Ident { |
| 35 | + let snake_name = name.to_string(); |
| 36 | + let mut next_caps = false; |
| 37 | + let ret: String = snake_name |
| 38 | + .chars() |
| 39 | + .filter_map(|c| { |
| 40 | + if c == '_' { |
| 41 | + next_caps = true; |
| 42 | + None |
| 43 | + } else if next_caps { |
| 44 | + next_caps = false; |
| 45 | + // This assumes we're using ascii, should be okay |
| 46 | + Some(c.to_ascii_uppercase()) |
| 47 | + } else { |
| 48 | + Some(c) |
| 49 | + } |
| 50 | + }) |
| 51 | + .collect(); |
| 52 | + Ident::new(&ret, name.span()) |
| 53 | +} |
| 54 | + |
| 55 | +fn gen_schema_fields(data: &Data) -> TokenStream { |
| 56 | + let fields = match data { |
| 57 | + Data::Struct(DataStruct { |
| 58 | + fields: Fields::Named(fields), |
| 59 | + .. |
| 60 | + }) => &fields.named, |
| 61 | + _ => panic!("this derive macro only works on structs with named fields"), |
| 62 | + }; |
| 63 | + |
| 64 | + let schema_fields = fields.iter().map(|field| { |
| 65 | + let name = field.ident.as_ref().unwrap(); // we know these are named fields |
| 66 | + let name = get_schema_name(name); |
| 67 | + match field.ty { |
| 68 | + Type::Path(ref type_path) => { |
| 69 | + let type_path_quoted = type_path.path.segments.iter().map(|segment| { |
| 70 | + let segment_ident = &segment.ident; |
| 71 | + match &segment.arguments { |
| 72 | + PathArguments::None => quote! { #segment_ident :: }, |
| 73 | + PathArguments::AngleBracketed(angle_args) => quote! { #segment_ident::#angle_args :: }, |
| 74 | + _ => panic!("Can only handle <> type path args"), |
| 75 | + } |
| 76 | + }); |
| 77 | + quote_spanned! { field.span() => #(#type_path_quoted),* get_struct_field(stringify!(#name))} |
| 78 | + } |
| 79 | + _ => { |
| 80 | + panic!("Can't handle type: {:?}", field.ty); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + quote! { #(#schema_fields),* } |
| 85 | +} |
0 commit comments