-
Notifications
You must be signed in to change notification settings - Fork 598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(connector): Add a dummy trait WithOptions
#14175
Merged
xxchan
merged 6 commits into
main
from
12-25-refactor_connector_Add_a_dummy_trait_WithOptions_
Dec 27, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1c4224
refactor(connector): Add a dummy trait `WithOptions`
xxchan 50ef9e7
add bound to SourceProperties
xxchan 230ef70
refine derive impl. Add missing derives
xxchan 4bf25e5
add note
xxchan 51e3ad1
update nexmark
xxchan e2f5a96
fix test
xxchan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,8 @@ pub mod common; | |
|
||
pub use paste::paste; | ||
|
||
mod with_options; | ||
|
||
#[cfg(test)] | ||
mod with_options_test; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::collections::HashMap; | ||
|
||
/// Marker trait for `WITH` options. Only for `#[derive(WithOptions)]`, should not be used manually. | ||
/// | ||
/// This is used to ensure the `WITH` options types have reasonable structure. | ||
/// | ||
/// TODO: add this bound for sink. There's a `SourceProperties` trait for sources, but no similar | ||
/// things for sinks. | ||
pub trait WithOptions { | ||
#[doc(hidden)] | ||
#[inline(always)] | ||
fn assert_receiver_is_with_options(&self) {} | ||
} | ||
|
||
// Currently CDC properties are handled specially. | ||
// - It simply passes HashMap to Java DBZ. | ||
// - It's not handled by serde. | ||
// - It contains fields other than WITH options. | ||
// TODO: remove the workaround here. And also use #[derive] for it. | ||
|
||
impl<T: crate::source::cdc::CdcSourceTypeTrait> WithOptions | ||
for crate::source::cdc::CdcProperties<T> | ||
{ | ||
} | ||
|
||
// impl the trait for value types | ||
|
||
impl<T: WithOptions> WithOptions for Option<T> {} | ||
impl WithOptions for Vec<String> {} | ||
impl WithOptions for HashMap<String, String> {} | ||
|
||
impl WithOptions for String {} | ||
impl WithOptions for bool {} | ||
impl WithOptions for usize {} | ||
impl WithOptions for u32 {} | ||
impl WithOptions for u64 {} | ||
impl WithOptions for i32 {} | ||
impl WithOptions for i64 {} | ||
impl WithOptions for f64 {} | ||
impl WithOptions for std::time::Duration {} | ||
impl WithOptions for crate::sink::kafka::CompressionCodec {} | ||
impl WithOptions for nexmark::config::RateShape {} | ||
impl WithOptions for nexmark::event::EventType {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,13 @@ pub fn generate_with_options_yaml_sink() -> String { | |
|
||
/// Collect all structs with `#[derive(WithOptions)]` in the `.rs` files in `path` (plus `common.rs`), | ||
/// and generate a YAML file. | ||
/// | ||
/// Note: here we assumes the struct is parsed by `serde`. If it's not the case, | ||
/// the generated `yaml` might be inconsistent with the actual parsing logic. | ||
/// TODO: improve the test to check whether serde is used. | ||
/// | ||
/// - For sources, the parsing logic is in `TryFromHashMap`. | ||
/// - For sinks, the parsing logic is in `TryFrom<SinkParam>`. | ||
Comment on lines
+51
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Highlight Note |
||
fn generate_with_options_yaml_inner(path: &Path) -> String { | ||
let mut structs = vec![]; | ||
let mut functions = BTreeMap::<String, FunctionInfo>::new(); | ||
|
@@ -194,7 +201,12 @@ fn has_with_options_attribute(attrs: &[Attribute]) -> bool { | |
if let Ok(Meta::List(meta_list)) = attr.parse_meta() { | ||
return meta_list.path.is_ident("derive") | ||
&& meta_list.nested.iter().any(|nested| match nested { | ||
syn::NestedMeta::Meta(Meta::Path(path)) => path.is_ident("WithOptions"), | ||
syn::NestedMeta::Meta(Meta::Path(path)) => { | ||
// Check if the path contains WithOptions | ||
path.segments | ||
.iter() | ||
.any(|segment| segment.ident == "WithOptions") | ||
} | ||
_ => false, | ||
}); | ||
} | ||
|
@@ -273,6 +285,9 @@ fn extract_serde_properties(field: &Field) -> SerdeProperties { | |
/// // ... | ||
/// } | ||
/// ``` | ||
/// | ||
/// Note: here we assumes `#[serde(flatten)]` is used for struct fields. If it's not the case, | ||
/// the generated `yaml` might be inconsistent with the actual parsing logic. | ||
fn flatten_nested_options(options: BTreeMap<String, StructInfo>) -> BTreeMap<String, StructInfo> { | ||
let mut deleted_keys = HashSet::new(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, DeriveInput}; | ||
|
||
/// Annotates that the struct represents the WITH properties for a connector. | ||
#[proc_macro_derive(WithOptions, attributes(with_option))] | ||
pub fn derive_helper_attr(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as DeriveInput); | ||
|
||
let fields = match input.data { | ||
syn::Data::Struct(ref data) => match data.fields { | ||
syn::Fields::Named(ref fields) => &fields.named, | ||
_ => return quote! { compile_error!("WithOptions can only be derived for structs with named fields"); }.into(), | ||
}, | ||
_ => return quote! { compile_error!("WithOptions can only be derived for structs"); }.into(), | ||
}; | ||
|
||
let mut assert_impls = vec![]; | ||
|
||
for field in fields { | ||
let field_name = field.ident.as_ref().unwrap(); | ||
|
||
assert_impls.push(quote!( | ||
crate::with_options::WithOptions::assert_receiver_is_with_options(&self.#field_name); | ||
)) | ||
} | ||
|
||
let struct_name = input.ident; | ||
// This macro is only be expected to used in risingwave_connector. This trait is also defined there. | ||
if input.generics.params.is_empty() { | ||
quote! { | ||
impl crate::with_options::WithOptions for #struct_name { | ||
fn assert_receiver_is_with_options(&self) { | ||
#(#assert_impls)* | ||
} | ||
} | ||
} | ||
.into() | ||
} else { | ||
// Note: CDC properties have generics. | ||
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); | ||
quote! { | ||
impl #impl_generics crate::with_options::WithOptions for #struct_name #ty_generics #where_clause { | ||
fn assert_receiver_is_with_options(&self) { | ||
#(#assert_impls)* | ||
} | ||
} | ||
}.into() | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Highlight TODOs