Skip to content

Commit

Permalink
generate LATEST automatically
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Chien <[email protected]>
  • Loading branch information
stdrc committed Feb 3, 2025
1 parent 3384483 commit 1f3a88b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
4 changes: 0 additions & 4 deletions proto/hummock.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ enum CompatibilityVersion {
NO_TRIVIAL_SPLIT = 1;
NO_MEMBER_TABLE_IDS = 2;
SPLIT_GROUP_BY_TABLE_ID = 3;

// IMPORTANT:
// Don't forget to change `CompatibilityVersion::LATEST` in `prost/src/lib.rs` to the latest version
// when adding new versions to this enum.
}

message GroupConstruct {
Expand Down
4 changes: 0 additions & 4 deletions proto/plan_common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ enum ColumnDescVersion {
// Introduced in https://github.com/risingwavelabs/risingwave/pull/13707#discussion_r1429947537,
// in case DEFAULT_KEY_COLUMN_NAME changes
COLUMN_DESC_VERSION_PR_13707 = 1;

// IMPORTANT:
// Don't forget to change `ColumnDescVersion::LATEST` in `prost/src/lib.rs` to the latest version
// when adding new versions to this enum.
}

message ColumnDesc {
Expand Down
4 changes: 0 additions & 4 deletions proto/stream_plan.proto
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,6 @@ enum AggNodeVersion {

// https://github.com/risingwavelabs/risingwave/issues/13465#issuecomment-1821016508
AGG_NODE_VERSION_ISSUE_13465 = 2;

// IMPORTANT:
// Don't forget to change `AggNodeVersion::LATEST` in `prost/src/lib.rs` to the latest version
// when adding new versions to this enum.
}

message SimpleAggNode {
Expand Down
14 changes: 13 additions & 1 deletion src/prost/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.type_attribute(
"hummock.TableWatermarks.EpochNewWatermarks",
"#[derive(Eq)]",
);
)
// proto version enums
.type_attribute("stream_plan.AggNodeVersion", "#[derive(prost_helpers::Version)]")
.type_attribute(
"plan_common.ColumnDescVersion",
"#[derive(prost_helpers::Version)]",
)
.type_attribute(
"hummock.CompatibilityVersion",
"#[derive(prost_helpers::Version)]",
)
// end
;

// If any configuration for `prost_build` is not exposed by `tonic_build`, specify it here.
let mut prost_config = prost_build::Config::new();
Expand Down
41 changes: 39 additions & 2 deletions src/prost/helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#![feature(iterator_try_collect)]

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::{parse_macro_input, DataStruct, DeriveInput, Result};
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Result};

mod generate;

Expand Down Expand Up @@ -69,3 +69,40 @@ fn produce(ast: &DeriveInput) -> Result<TokenStream2> {
#struct_get
})
}

#[cfg_attr(coverage, coverage(off))]
#[proc_macro_derive(Version)]
pub fn version(input: TokenStream) -> TokenStream {
fn version_inner(ast: &DeriveInput) -> syn::Result<TokenStream2> {
let last_variant = match &ast.data {
Data::Enum(v) => v.variants.iter().last().ok_or_else(|| {
syn::Error::new(
Span::call_site(),
"This macro requires at least one variant in the enum.",
)
})?,
_ => {
return Err(syn::Error::new(
Span::call_site(),
"This macro only supports enums.",
));
}
};

let enum_name = &ast.ident;
let last_variant_name = &last_variant.ident;

Ok(quote! {
impl #enum_name {
pub const LATEST: Self = Self::#last_variant_name;
}
})
}

let ast = parse_macro_input!(input as DeriveInput);

match version_inner(&ast) {
Ok(tokens) => tokens.into(),
Err(e) => e.to_compile_error().into(),
}
}
15 changes: 0 additions & 15 deletions src/prost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,21 +523,6 @@ impl std::fmt::Debug for plan_common::ColumnDesc {
}
}

impl stream_plan::AggNodeVersion {
/// The latest version of Agg node proto message.
pub const LATEST: Self = Self::Issue13465;
}

impl plan_common::ColumnDescVersion {
/// The latest version of `ColumnDesc` proto message.
pub const LATEST: Self = Self::Pr13707;
}

impl hummock::CompatibilityVersion {
/// The latest version of `CompatibilityVersion` proto message.
pub const LATEST: Self = Self::SplitGroupByTableId;
}

#[cfg(test)]
mod tests {
use crate::data::{data_type, DataType};
Expand Down

0 comments on commit 1f3a88b

Please sign in to comment.