Remove panic

This commit is contained in:
Marco Napetti 2021-09-06 12:21:21 +02:00
parent 0eb6902d1b
commit cc2f491a1e

View File

@ -1,7 +1,7 @@
use proc_macro2::{Ident, Span, TokenStream}; use proc_macro2::{Ident, Span, TokenStream};
use quote::quote; use quote::quote;
use syn::{Attribute, Data, Fields, Lit, Meta, punctuated::Punctuated, token::Comma}; use syn::{Attribute, Data, Fields, Lit, Meta, parse::Error, punctuated::Punctuated, spanned::Spanned, token::Comma};
use convert_case::{Case, Casing}; use convert_case::{Case, Casing};
@ -103,36 +103,40 @@ impl sea_orm::prelude::EntityName for Entity {
} }
} }
}); });
let field_type = sql_type.unwrap_or_else(|| {
let field_type = &field.ty; let field_type = match sql_type {
let temp = quote! { #field_type } Some(t) => t,
.to_string()//E.g.: "Option < String >" None => {
.replace(" ", ""); let field_type = &field.ty;
let temp = if temp.starts_with("Option<") { let temp = quote! { #field_type }
nullable = true; .to_string()//E.g.: "Option < String >"
&temp[7..(temp.len() - 1)] .replace(" ", "");
let temp = if temp.starts_with("Option<") {
nullable = true;
&temp[7..(temp.len() - 1)]
}
else {
temp.as_str()
};
match temp {
"char" => quote! { Char(None) },
"String" | "&str" => quote! { String(None) },
"u8" | "i8" => quote! { TinyInteger },
"u16" | "i16" => quote! { SmallInteger },
"u32" | "u64" | "i32" | "i64" => quote! { Integer },
"u128" | "i128" => quote! { BigInteger },
"f32" => quote! { Float },
"f64" => quote! { Double },
"bool" => quote! { Boolean },
"NaiveDate" => quote! { Date },
"NaiveTime" => quote! { Time },
"NaiveDateTime" => quote! { DateTime },
"Uuid" => quote! { Uuid },
"Decimal" => quote! { BigInteger },
_ => return Err(Error::new(field.span(), format!("unrecognized type {}", temp))),
}
} }
else { };
temp.as_str()
};
match temp {
"char" => quote! { Char(None) },
"String" | "&str" => quote! { String(None) },
"u8" | "i8" => quote! { TinyInteger },
"u16" | "i16" => quote! { SmallInteger },
"u32" | "u64" | "i32" | "i64" => quote! { Integer },
"u128" | "i128" => quote! { BigInteger },
"f32" => quote! { Float },
"f64" => quote! { Double },
"bool" => quote! { Boolean },
"NaiveDate" => quote! { Date },
"NaiveTime" => quote! { Time },
"NaiveDateTime" => quote! { DateTime },
"Uuid" => quote! { Uuid },
"Decimal" => quote! { BigInteger },
_ => panic!("unrecognized type {}", temp),
}
});
let mut match_row = quote! { Self::#field_name => sea_orm::prelude::ColumnType::#field_type.def() }; let mut match_row = quote! { Self::#field_name => sea_orm::prelude::ColumnType::#field_type.def() };
if nullable { if nullable {