Skip to content

Commit

Permalink
Inline - macros
Browse files Browse the repository at this point in the history
Per #1517 (comment) consolidate all macros-related PRs
  • Loading branch information
nyurik committed Feb 5, 2024
1 parent 395c975 commit 090ea6d
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 116 deletions.
18 changes: 9 additions & 9 deletions pgrx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn pg_test(attr: TokenStream, item: TokenStream) -> TokenStream {
let res = pgrx_tests::run_test(#sql_funcname, #expected_error, crate::pg_test::postgresql_conf_options());
match res {
Ok(()) => (),
Err(e) => panic!("{:?}", e)
Err(e) => panic!("{e:?}")
}
}
});
Expand Down Expand Up @@ -184,12 +184,12 @@ pub fn pg_cast(attr: TokenStream, item: TokenStream) -> TokenStream {
{
"implicit" => cast = PgCast::Implicit,
"assignment" => cast = PgCast::Assignment,
other => panic!("Unrecognized pg_cast option: {}. ", other),
other => panic!("Unrecognized pg_cast option: {other}. "),
}
}
}
Err(err) => {
panic!("Failed to parse attribute to pg_cast: {}", err)
panic!("Failed to parse attribute to pg_cast: {err}")
}
}
// `pg_cast` does not support other `pg_extern` attributes for now, pass an empty attribute token stream.
Expand Down Expand Up @@ -715,7 +715,7 @@ fn impl_postgres_enum(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
let (name, _, _) = ::pgrx::enum_helper::lookup_enum_by_oid(unsafe { ::pgrx::pg_sys::Oid::from_datum(datum, is_null)? } );
match name.as_str() {
#from_datum
_ => panic!("invalid enum value: {}", name)
_ => panic!("invalid enum value: {name}")
}
}
}
Expand Down Expand Up @@ -795,8 +795,8 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
let name = &ast.ident;
let generics = &ast.generics;
let has_lifetimes = generics.lifetimes().next();
let funcname_in = Ident::new(&format!("{}_in", name).to_lowercase(), name.span());
let funcname_out = Ident::new(&format!("{}_out", name).to_lowercase(), name.span());
let funcname_in = Ident::new(&format!("{name}_in").to_lowercase(), name.span());
let funcname_out = Ident::new(&format!("{name}_out").to_lowercase(), name.span());
let mut args = parse_postgres_type_args(&ast.attrs);
let mut stream = proc_macro2::TokenStream::new();

Expand Down Expand Up @@ -905,7 +905,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
pub fn #funcname_in #generics(input: Option<&#lifetime ::core::ffi::CStr>) -> Option<#name #generics> {
input.map_or_else(|| {
for m in <#name as ::pgrx::inoutfuncs::JsonInOutFuncs>::NULL_ERROR_MESSAGE {
::pgrx::pg_sys::error!("{}", m);
::pgrx::pg_sys::error!("{m}");
}
None
}, |i| Some(<#name as ::pgrx::inoutfuncs::JsonInOutFuncs>::input(i)))
Expand All @@ -929,7 +929,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
pub fn #funcname_in #generics(input: Option<&#lifetime ::core::ffi::CStr>) -> Option<#name #generics> {
input.map_or_else(|| {
for m in <#name as ::pgrx::inoutfuncs::InOutFuncs>::NULL_ERROR_MESSAGE {
::pgrx::pg_sys::error!("{}", m);
::pgrx::pg_sys::error!("{m}");
}
None
}, |i| Some(<#name as ::pgrx::inoutfuncs::InOutFuncs>::input(i)))
Expand All @@ -952,7 +952,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
pub fn #funcname_in #generics(input: Option<&#lifetime ::core::ffi::CStr>) -> Option<::pgrx::datum::PgVarlena<#name #generics>> {
input.map_or_else(|| {
for m in <#name as ::pgrx::inoutfuncs::PgVarlenaInOutFuncs>::NULL_ERROR_MESSAGE {
::pgrx::pg_sys::error!("{}", m);
::pgrx::pg_sys::error!("{m}");
}
None
}, |i| Some(<#name as ::pgrx::inoutfuncs::PgVarlenaInOutFuncs>::input(i)))
Expand Down
16 changes: 8 additions & 8 deletions pgrx-macros/src/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub(crate) fn deriving_postgres_hash(ast: DeriveInput) -> syn::Result<proc_macro
///
/// ["optimization hints"]: https://www.postgresql.org/docs/current/xoper-optimization.html
pub fn derive_pg_eq(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_eq", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_eq").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_operator(immutable, parallel_safe)]
Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn derive_pg_eq(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro
///
/// See `derive_pg_eq` for the implications of this assumption.
pub fn derive_pg_ne(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_ne", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_ne").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_operator(immutable, parallel_safe)]
Expand All @@ -138,7 +138,7 @@ pub fn derive_pg_ne(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro
}

pub fn derive_pg_lt(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_lt", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_lt").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_operator(immutable, parallel_safe)]
Expand All @@ -155,7 +155,7 @@ pub fn derive_pg_lt(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro
}

pub fn derive_pg_gt(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_gt", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_gt").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_operator(immutable, parallel_safe)]
Expand All @@ -171,7 +171,7 @@ pub fn derive_pg_gt(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro
}

pub fn derive_pg_le(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_le", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_le").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_operator(immutable, parallel_safe)]
Expand All @@ -187,7 +187,7 @@ pub fn derive_pg_le(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro
}

pub fn derive_pg_ge(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_ge", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_ge").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_operator(immutable, parallel_safe)]
Expand All @@ -203,7 +203,7 @@ pub fn derive_pg_ge(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro
}

pub fn derive_pg_cmp(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_cmp", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_cmp").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_extern(immutable, parallel_safe)]
Expand All @@ -228,7 +228,7 @@ pub fn derive_pg_cmp(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macr
/// Postgres is no different: this hashing is for the explicit purpose of equality checks,
/// and it also needs to be able to reason from hash equality to actual equality.
pub fn derive_pg_hash(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_hash", name).to_lowercase(), name.span());
let pg_name = Ident::new(&format!("{name}_hash").to_lowercase(), name.span());
quote! {
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_extern(immutable, parallel_safe)]
Expand Down
12 changes: 6 additions & 6 deletions pgrx-sql-entity-graph/src/aggregate/aggregate_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ mod tests {
let first = &list.found[0];
let first_string = match &first.used_ty.resolved_ty {
syn::Type::Path(ty_path) => ty_path.path.segments.last().unwrap().ident.to_string(),
_ => return Err(eyre_err!("Wrong first.used_ty.resolved_ty: {:?}", first)),
_ => return Err(eyre_err!("Wrong first.used_ty.resolved_ty: {first:?}")),
};
assert_eq!(first_string, "i32");

let second = &list.found[1];
let second_string = match &second.used_ty.resolved_ty {
syn::Type::Path(ty_path) => ty_path.path.segments.last().unwrap().ident.to_string(),
_ => return Err(eyre_err!("Wrong second.used_ty.resolved_ty: {:?}", second)),
_ => return Err(eyre_err!("Wrong second.used_ty.resolved_ty: {second:?}")),
};
assert_eq!(second_string, "i8");
Ok(())
Expand All @@ -175,14 +175,14 @@ mod tests {
let first = &list.found[0];
let first_string = match &first.used_ty.resolved_ty {
syn::Type::Path(ty_path) => ty_path.path.segments.last().unwrap().ident.to_string(),
_ => return Err(eyre_err!("Wrong first.used_ty.resolved_ty: {:?}", first)),
_ => return Err(eyre_err!("Wrong first.used_ty.resolved_ty: {first:?}")),
};
assert_eq!(first_string, "i32");

let second = &list.found[1];
let second_string = match &second.used_ty.resolved_ty {
syn::Type::Path(ty_path) => ty_path.path.segments.last().unwrap().ident.to_string(),
_ => return Err(eyre_err!("Wrong second.used_ty.resolved_ty: {:?}", second)),
_ => return Err(eyre_err!("Wrong second.used_ty.resolved_ty: {second:?}")),
};
assert_eq!(second_string, "VariadicArray");
Ok(())
Expand All @@ -200,14 +200,14 @@ mod tests {
let first = &list.found[0];
let first_string = match &first.used_ty.resolved_ty {
syn::Type::Path(ty_path) => ty_path.path.segments.last().unwrap().ident.to_string(),
_ => return Err(eyre_err!("Wrong first.ty: {:?}", first)),
_ => return Err(eyre_err!("Wrong first.ty: {first:?}")),
};
assert_eq!(first_string, "i32");

let second = &list.found[1];
let second_string = match &second.used_ty.resolved_ty {
syn::Type::Path(ty_path) => ty_path.path.segments.last().unwrap().ident.to_string(),
_ => return Err(eyre_err!("Wrong second.used_ty.resolved_ty: {:?}", second)),
_ => return Err(eyre_err!("Wrong second.used_ty.resolved_ty: {second:?}")),
};
assert_eq!(second_string, "VariadicArray");
Ok(())
Expand Down
28 changes: 13 additions & 15 deletions pgrx-sql-entity-graph/src/aggregate/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl ToSql for PgAggregateEntity {

if let Some(value) = self.finalfunc {
optional_attributes.push((
format!("\tFINALFUNC = {}\"{}\"", schema, value),
format!("\tFINALFUNC = {schema}\"{value}\""),
format!("/* {}::final */", self.full_path),
));
}
Expand All @@ -189,43 +189,43 @@ impl ToSql for PgAggregateEntity {
}
if let Some(value) = self.combinefunc {
optional_attributes.push((
format!("\tCOMBINEFUNC = {}\"{}\"", schema, value),
format!("\tCOMBINEFUNC = {schema}\"{value}\""),
format!("/* {}::combine */", self.full_path),
));
}
if let Some(value) = self.serialfunc {
optional_attributes.push((
format!("\tSERIALFUNC = {}\"{}\"", schema, value),
format!("\tSERIALFUNC = {schema}\"{value}\""),
format!("/* {}::serial */", self.full_path),
));
}
if let Some(value) = self.deserialfunc {
optional_attributes.push((
format!("\tDESERIALFUNC ={} \"{}\"", schema, value),
format!("\tDESERIALFUNC ={schema} \"{value}\""),
format!("/* {}::deserial */", self.full_path),
));
}
if let Some(value) = self.initcond {
optional_attributes.push((
format!("\tINITCOND = '{}'", value),
format!("\tINITCOND = '{value}'"),
format!("/* {}::INITIAL_CONDITION */", self.full_path),
));
}
if let Some(value) = self.msfunc {
optional_attributes.push((
format!("\tMSFUNC = {}\"{}\"", schema, value),
format!("\tMSFUNC = {schema}\"{value}\""),
format!("/* {}::moving_state */", self.full_path),
));
}
if let Some(value) = self.minvfunc {
optional_attributes.push((
format!("\tMINVFUNC = {}\"{}\"", schema, value),
format!("\tMINVFUNC = {schema}\"{value}\""),
format!("/* {}::moving_state_inverse */", self.full_path),
));
}
if let Some(value) = self.mfinalfunc {
optional_attributes.push((
format!("\tMFINALFUNC = {}\"{}\"", schema, value),
format!("\tMFINALFUNC = {schema}\"{value}\""),
format!("/* {}::moving_state_finalize */", self.full_path),
));
}
Expand All @@ -237,13 +237,13 @@ impl ToSql for PgAggregateEntity {
}
if let Some(value) = self.minitcond {
optional_attributes.push((
format!("\tMINITCOND = '{}'", value),
format!("\tMINITCOND = '{value}'"),
format!("/* {}::MOVING_INITIAL_CONDITION */", self.full_path),
));
}
if let Some(value) = self.sortop {
optional_attributes.push((
format!("\tSORTOP = \"{}\"", value),
format!("\tSORTOP = \"{value}\""),
format!("/* {}::SORT_OPERATOR */", self.full_path),
));
}
Expand Down Expand Up @@ -296,7 +296,7 @@ impl ToSql for PgAggregateEntity {
if let Some(value) = &self.mstype {
let mstype_sql = map_ty(value).wrap_err("Mapping moving state type")?;
optional_attributes.push((
format!("\tMSTYPE = {}", mstype_sql),
format!("\tMSTYPE = {mstype_sql}"),
format!("/* {}::MovingState = {} */", self.full_path, value.full_path),
));
}
Expand All @@ -305,9 +305,7 @@ impl ToSql for PgAggregateEntity {
for (index, (optional_attribute, comment)) in optional_attributes.iter().enumerate() {
let optional_attribute_string = format!(
"{optional_attribute}{maybe_comma} {comment}{maybe_newline}",
optional_attribute = optional_attribute,
maybe_comma = if index == optional_attributes.len() - 1 { "" } else { "," },
comment = comment,
maybe_newline = if index == optional_attributes.len() - 1 { "" } else { "\n" }
);
optional_attributes_string += &optional_attribute_string;
Expand Down Expand Up @@ -359,7 +357,7 @@ impl ToSql for PgAggregateEntity {
maybe_comma = if needs_comma { ", " } else { " " },
full_path = arg.used_ty.full_path,
name = if let Some(name) = arg.name {
format!(r#""{}" "#, name)
format!(r#""{name}" "#)
} else { "".to_string() },
);
args.push(buf);
Expand All @@ -378,7 +376,7 @@ impl ToSql for PgAggregateEntity {
SqlGraphEntity::BuiltinType(defined) => defined == arg.used_ty.full_path,
_ => false,
})
.ok_or_else(|| eyre!("Could not find arg type in graph. Got: {:?}", arg))?;
.ok_or_else(|| eyre!("Could not find arg type in graph. Got: {arg:?}"))?;
let needs_comma = idx < (direct_args.len() - 1);
let buf = format!(
"\
Expand Down
Loading

0 comments on commit 090ea6d

Please sign in to comment.