Skip to content

Commit

Permalink
feat(prost): add deprecated attribute for helper methods of deprecate…
Browse files Browse the repository at this point in the history
…d field (#20257)
  • Loading branch information
wenym1 authored Jan 22, 2025
1 parent d558db4 commit 983dd18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/expr/core/src/window_function/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ impl Frame {
let bounds = match frame.get_type()? {
PbType::Unspecified => bail!("unspecified type of `WindowFrame`"),
PbType::RowsLegacy => {
let start = FrameBound::<usize>::from_protobuf_legacy(frame.get_start()?)?;
let end = FrameBound::<usize>::from_protobuf_legacy(frame.get_end()?)?;
FrameBounds::Rows(RowsFrameBounds { start, end })
#[expect(deprecated)]
{
let start = FrameBound::<usize>::from_protobuf_legacy(frame.get_start()?)?;
let end = FrameBound::<usize>::from_protobuf_legacy(frame.get_end()?)?;
FrameBounds::Rows(RowsFrameBounds { start, end })
}
}
PbType::Rows => {
let bounds = must_match!(frame.get_bounds()?, PbBounds::Rows(bounds) => bounds);
Expand Down
27 changes: 25 additions & 2 deletions src/prost/helpers/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use quote::quote;
use syn::ext::IdentExt;
use syn::spanned::Spanned;
use syn::{
Error, Expr, ExprLit, Field, GenericArgument, Lit, Meta, PathArguments, PathSegment, Result,
Type,
AttrStyle, Attribute, Error, Expr, ExprLit, Field, GenericArgument, Lit, Meta, Path,
PathArguments, PathSegment, Result, Type,
};

fn extract_type_from_option(option_segment: &PathSegment) -> Type {
Expand Down Expand Up @@ -83,16 +83,36 @@ fn extract_enum_type_from_field(field: &Field) -> Option<Type> {
Some(syn::parse_str::<Type>(&enum_type.value()).unwrap())
}

fn is_deprecated(field: &Field) -> bool {
field.attrs.iter().any(|attr| match &attr.meta {
Meta::Path(path) => path.is_ident("deprecated"),
_ => false,
})
}

pub fn implement(field: &Field) -> Result<TokenStream2> {
let field_name = field
.clone()
.ident
.ok_or_else(|| Error::new(field.span(), "Expected the field to have a name"))?;

let getter_fn_name = Ident::new(&format!("get_{}", field_name.unraw()), Span::call_site());
let is_deprecated = is_deprecated(field);

let attr_list: Vec<Attribute> = if is_deprecated {
vec![Attribute {
pound_token: Default::default(),
style: AttrStyle::Outer,
bracket_token: Default::default(),
meta: Meta::from(Path::from(Ident::new("deprecated", Span::call_site()))),
}]
} else {
vec![]
};

if let Some(enum_type) = extract_enum_type_from_field(field) {
return Ok(quote! {
#(#attr_list)*
#[inline(always)]
pub fn #getter_fn_name(&self) -> std::result::Result<#enum_type, crate::PbFieldNotFound> {
if self.#field_name.eq(&0) {
Expand All @@ -110,6 +130,7 @@ pub fn implement(field: &Field) -> Result<TokenStream2> {
// ::core::option::Option<Foo>
let ty = extract_type_from_option(data_type);
return Ok(quote! {
#(#attr_list)*
#[inline(always)]
pub fn #getter_fn_name(&self) -> std::result::Result<&#ty, crate::PbFieldNotFound> {
self.#field_name.as_ref().ok_or_else(|| crate::PbFieldNotFound(stringify!(#field_name)))
Expand All @@ -120,6 +141,7 @@ pub fn implement(field: &Field) -> Result<TokenStream2> {
{
// Primitive types. Return value instead of reference.
return Ok(quote! {
#(#attr_list)*
#[inline(always)]
pub fn #getter_fn_name(&self) -> #ty {
self.#field_name
Expand All @@ -129,6 +151,7 @@ pub fn implement(field: &Field) -> Result<TokenStream2> {
}

Ok(quote! {
#(#attr_list)*
#[inline(always)]
pub fn #getter_fn_name(&self) -> &#ty {
&self.#field_name
Expand Down

0 comments on commit 983dd18

Please sign in to comment.