github-actions[bot] commented on code in PR #41467:
URL: https://github.com/apache/beam/pull/41467#discussion_r4623182570


##########
sdks/rust/beam/derive/src/outputs.rs:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+//! `#[derive(Outputs)]`.
+
+use proc_macro2::TokenStream as TokenStream2;
+use quote::{format_ident, quote};
+use syn::{Data, DeriveInput, Fields, Ident, Type};
+
+use crate::attrs::{ContainerOpts, parse_id, reject_duplicates};
+
+/// One output: a variant of the enum.
+struct Output {
+    variant: Ident,
+    field: Ident,
+    tag: String,
+    ty: Type,
+}
+
+impl Output {
+    fn parse(variant: &syn::Variant) -> syn::Result<Self> {
+        let ty = match &variant.fields {
+            Fields::Unnamed(fields) if fields.unnamed.len() == 1 => 
fields.unnamed[0].ty.clone(),
+            _ => {
+                return Err(syn::Error::new_spanned(
+                    variant,
+                    "an output variant has one field: the element type of the 
output",
+                ));
+            }
+        };
+        let field = format_ident!("{}", 
snake_case(&variant.ident.to_string()));

Review Comment:
   **🟠 High**: `variant.ident.to_string()` retains the `r#` prefix on raw 
identifiers (though uncommon on PascalCase enum variants, more relevant if a 
snake_case conversion produces a Rust keyword like `type`, `match`, `in`, 
`ref`, `loop`, `async`, `move`, etc.). If `snake_case(...)` produces a Rust 
keyword (e.g., `enum MyOut { Type(i32), Match(String) }` -> `"type"`, 
`"match"`), `format_ident!("{}", ...)` will panic at macro expansion time 
because `Ident::new` rejects keywords unless `r#` is used or `Ident::new_raw` 
is called. Furthermore, across the crate (`row/field.rs`, `state.rs`, 
`timers.rs`, `beam_enum.rs`), calling `ident.to_string()` on raw identifiers 
like `r#type` includes the literal `"r#"` in Beam schema field names, state 
IDs, and timer IDs. Consider using `syn::ext::IdentExt::unraw()` for string 
names/IDs and handling keywords when constructing generated field identifiers.



##########
sdks/rust/beam/derive/src/beam_enum.rs:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+//! `#[derive(BeamEnum)]`.
+
+use proc_macro2::TokenStream as TokenStream2;
+use quote::quote;
+use syn::{Data, DeriveInput, Fields};
+
+use crate::attrs::ContainerOpts;
+
+/// Expands `#[derive(BeamEnum)]`: variant names encode as `STRING`.
+pub(crate) fn expand_enum(input: &DeriveInput) -> syn::Result<TokenStream2> {
+    let opts = ContainerOpts::parse(input)?;
+    let krate = &opts.krate;
+    let name = &input.ident;
+
+    let Data::Enum(data) = &input.data else {
+        return Err(syn::Error::new_spanned(
+            &input.ident,
+            "BeamEnum only supports enums; use #[derive(BeamRow)] for structs",
+        ));
+    };
+
+    // Beam has no portable tagged-union schema type, so variants cannot carry 
payloads.
+    let variants = data
+        .variants
+        .iter()
+        .map(|variant| match variant.fields {
+            Fields::Unit => Ok(&variant.ident),
+            _ => Err(syn::Error::new_spanned(
+                variant,
+                "BeamEnum supports only fieldless variants; \
+                 Beam has no portable tagged-union type",
+            )),
+        })
+        .collect::<syn::Result<Vec<_>>>()?;

Review Comment:
   **🟡 Medium**: If `#[derive(BeamEnum)]` is applied to an empty enum (`enum 
Empty {}`), `to_arms` is empty and `let label = match self { #(#to_arms),* };` 
in `to_field_value` and `encode_field` will fail to typecheck (`match self {}` 
on `&Self` has type `()` instead of `&str`, whereas `match *self {}` is `!`, or 
empty enums should be explicitly rejected just like in `Outputs`). Consider 
rejecting empty enums with a clear `syn::Error` (similar to `Outputs needs at 
least one variant`).



##########
sdks/rust/beam/derive/src/beam_enum.rs:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+//! `#[derive(BeamEnum)]`.
+
+use proc_macro2::TokenStream as TokenStream2;
+use quote::quote;
+use syn::{Data, DeriveInput, Fields};
+
+use crate::attrs::ContainerOpts;
+
+/// Expands `#[derive(BeamEnum)]`: variant names encode as `STRING`.
+pub(crate) fn expand_enum(input: &DeriveInput) -> syn::Result<TokenStream2> {
+    let opts = ContainerOpts::parse(input)?;

Review Comment:
   **🟢 Low**: `expand_enum` (and `expand_timers` in `timers.rs`) parses 
`ContainerOpts` but silently ignores `opts.id` if the user writes `#[beam(id = 
"...")]` on the enum, unlike `outputs.rs` and `state.rs` which explicitly 
return an error when `opts.id.is_some()`. Consider rejecting `opts.id` here and 
in `timers.rs` as well to catch misplaced attributes.



##########
sdks/rust/README.md:
##########
@@ -31,6 +31,7 @@ The SDK is a Cargo workspace with its root at `sdks/rust`:
 | Crate | Path | Contents |
 | :--- | :--- | :--- |
 | `apache-beam-model` | `beam/model/` | Protobuf and gRPC bindings generated 
from the standard Beam model protos. |
+| `apache-beam-derive` | `beam/derive/` | `#[derive(BeamRow)]` and 
`#[derive(BeamEnum)]` proc-macros. `apache-beam-core` re-exports them. Do not 
depend on this crate directly. |

Review Comment:
   **🟢 Low**: The README table only mentions `#[derive(BeamRow)]` and 
`#[derive(BeamEnum)]` for `apache-beam-derive`, omitting `Outputs`, `State`, 
`Timers`, and `Register` (also note that the PR description mentions 
`TypedOutputs` / `TupleTag<T>` / `<Name>Senders` for `Outputs`, whereas the 
implementation derives `Outputs` on an enum and generates `<Name>PCollections`).
   
   ```suggestion
   | `apache-beam-derive` | `beam/derive/` | `#[derive(BeamRow)]`, 
`#[derive(BeamEnum)]`, `#[derive(Outputs)]`, `#[derive(State)]`, 
`#[derive(Timers)]`, and `#[derive(Register)]` proc-macros. `apache-beam-core` 
re-exports them. Do not depend on this crate directly. |
   ```



##########
sdks/rust/beam/derive/src/row/field.rs:
##########
@@ -0,0 +1,291 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+//! The fields of a `#[derive(BeamRow)]` struct: their attributes, their 
generated expressions
+//! and their checks.
+
+use std::collections::HashSet;
+
+use proc_macro2::TokenStream as TokenStream2;
+use quote::quote;
+use syn::{GenericArgument, Ident, LitInt, LitStr, Path, PathArguments, Type};
+
+/// Options parsed from field-level `#[beam(...)]` attributes.
+#[derive(Default)]
+struct FieldOpts {
+    rename: Option<String>,
+    skip: bool,
+    bytes: bool,
+    encoding_position: Option<i32>,
+}
+
+impl FieldOpts {
+    fn parse(field: &syn::Field) -> syn::Result<Self> {
+        let mut opts = Self::default();
+
+        for attr in field.attrs.iter().filter(|a| a.path().is_ident("beam")) {
+            attr.parse_nested_meta(|meta| {
+                if meta.path.is_ident("rename") {
+                    opts.rename = 
Some(meta.value()?.parse::<LitStr>()?.value());
+                } else if meta.path.is_ident("skip") {
+                    opts.skip = true;
+                } else if meta.path.is_ident("bytes") {
+                    opts.bytes = true;
+                } else if meta.path.is_ident("encoding_position") {
+                    opts.encoding_position = 
Some(meta.value()?.parse::<LitInt>()?.base10_parse()?);
+                } else {
+                    return Err(meta.error("unsupported beam field attribute"));
+                }
+                Ok(())
+            })?;
+        }
+
+        Ok(opts)

Review Comment:
   **🟡 Medium**: If a field is annotated with `#[beam(skip)]` along with other 
attributes like `#[beam(skip, encoding_position = 0)]`, `#[beam(skip, rename = 
"foo")]`, or `#[beam(skip, bytes)]`, the extra attributes are silently ignored 
because `partition_fields` discards `FieldOpts` for skipped fields before 
validation. For example, setting `encoding_position` on a skipped field will 
likely cause a confusing `encoding_position is out of range` error on another 
field. Consider rejecting `rename`, `bytes`, and `encoding_position` when 
`skip` is set.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to