Restore support for sqlx(rename) on types, with a deprecation warning

This commit is contained in:
Jonas Platte 2021-01-01 03:15:53 +01:00 committed by Ryan Leckey
parent fd8b2b7f8a
commit e2f7aa2eca
4 changed files with 72 additions and 11 deletions

View File

@ -105,11 +105,11 @@ pub use json::Json;
///
/// ##### Attributes
///
/// * `#[sqlx(rename = "<SQL type name>")]` on struct definition: instead of inferring the SQL type name from the inner
/// field (in the above case, `BIGINT`), explicitly set it to `<SQL type name>` instead. May trigger
/// errors or unexpected behavior if the encoding of the given type is different than that of the
/// inferred type (e.g. if you rename the above to `VARCHAR`).
/// Affects Postgres only.
/// * `#[sqlx(type_name = "<SQL type name>")]` on struct definition: instead of inferring the SQL
/// type name from the inner field (in the above case, `BIGINT`), explicitly set it to
/// `<SQL type name>` instead. May trigger errors or unexpected behavior if the encoding of the
/// given type is different than that of the inferred type (e.g. if you rename the above to
/// `VARCHAR`). Affects Postgres only.
///
/// ### Enumeration
///

View File

@ -1,5 +1,7 @@
use proc_macro2::Ident;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, quote_spanned};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Variant};
@ -26,6 +28,27 @@ macro_rules! try_set {
};
}
pub struct TypeName {
pub val: String,
pub span: Span,
/// Whether the old sqlx(rename) syntax was used instead of sqlx(type_name)
pub deprecated_rename: bool,
}
impl TypeName {
pub fn get(&self) -> TokenStream {
let val = &self.val;
if self.deprecated_rename {
quote_spanned!(self.span=> {
sqlx::_rename();
#val
})
} else {
quote! { #val }
}
}
}
#[derive(Copy, Clone)]
pub enum RenameAll {
LowerCase,
@ -39,7 +62,7 @@ pub enum RenameAll {
pub struct SqlxContainerAttributes {
pub transparent: bool,
pub type_name: Option<String>,
pub type_name: Option<TypeName>,
pub rename_all: Option<RenameAll>,
pub repr: Option<Ident>,
}
@ -95,7 +118,31 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContai
lit: Lit::Str(val),
..
}) if path.is_ident("type_name") => {
try_set!(type_name, val.value(), value)
try_set!(
type_name,
TypeName {
val: val.value(),
span: value.span(),
deprecated_rename: false
},
value
)
}
Meta::NameValue(MetaNameValue {
path,
lit: Lit::Str(val),
..
}) if path.is_ident("rename") => {
try_set!(
type_name,
TypeName {
val: val.value(),
span: value.span(),
deprecated_rename: true
},
value
)
}
u => fail!(u, "unexpected attribute"),

View File

@ -83,7 +83,10 @@ fn expand_derive_has_sql_type_transparent(
let mut tts = proc_macro2::TokenStream::new();
if cfg!(feature = "postgres") {
let ty_name = attr.type_name.unwrap_or_else(|| ident.to_string());
let ty_name = attr
.type_name
.map(|tn| tn.get())
.unwrap_or_else(|| quote! { #ident });
tts.extend(quote!(
impl sqlx::Type< sqlx::postgres::Postgres > for #ident #ty_generics {
@ -142,7 +145,10 @@ fn expand_derive_has_sql_type_strong_enum(
}
if cfg!(feature = "postgres") {
let ty_name = attributes.type_name.unwrap_or_else(|| ident.to_string());
let ty_name = attributes
.type_name
.map(|tn| tn.get())
.unwrap_or_else(|| quote! { #ident });
tts.extend(quote!(
impl sqlx::Type< sqlx::Postgres > for #ident {
@ -180,7 +186,10 @@ fn expand_derive_has_sql_type_struct(
let mut tts = proc_macro2::TokenStream::new();
if cfg!(feature = "postgres") {
let ty_name = attributes.type_name.unwrap_or_else(|| ident.to_string());
let ty_name = attributes
.type_name
.map(|tn| tn.get())
.unwrap_or_else(|| quote! { #ident });
tts.extend(quote!(
impl sqlx::Type< sqlx::Postgres > for #ident {

View File

@ -149,3 +149,8 @@ pub mod prelude {
pub use super::Statement;
pub use super::Type;
}
#[doc(hidden)]
#[inline(always)]
#[deprecated = "sqlx(rename) is now called sqlx(type_name)"]
pub fn _rename() {}