This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git


The following commit(s) were added to refs/heads/main by this push:
     new 21dd33319 feat(compiler): add gRPC flags to fory compiler (#3361)
21dd33319 is described below

commit 21dd333197e8984848e5dc97685a200f64611198
Author: Ayush Kumar <[email protected]>
AuthorDate: Thu Feb 19 10:40:00 2026 +0530

    feat(compiler): add gRPC flags to fory compiler (#3361)
    
    ## Why?
    
    For initializing stub generation for languages, compiler flags `--grpc`
    and `--grpc-backend` were required.
    ## What does this PR do?
    
    ### 1. fory_compiler/base.py
    
    Update `GeneratorOptions` to include grpc (bool) and grpc_backend (str).
    ```python
    @dataclass
    class GeneratorOptions:
        """Options for code generation."""
    
        output_dir: Path
        package_override: Optional[str] = None
        go_nested_type_style: Optional[str] = None
        grpc: bool = False
        grpc_backend: Optional[str] = None
    
    ```
    
    Add generate_services(self) -> List[GeneratedFile] method to
    `BaseGenerator` (returns empty list by default).
    ```python
        def generate_services(self) -> List[GeneratedFile]:
            """Generate service-related code (e.g. gRPC stubs).
    
            Base implementation returns empty list. Subclasses should override
            if they support service generation.
            """
            return []
    ```
    
    ### 2. fory_compiler/cli.py
    
    - Add --grpc flag (store_true).
    - Add --grpc-backend option (string).
    - Update `compile_file` to:
    - - Pass flags to `GeneratorOptions`.
    - - Call `generator.generate_services()` if --grpc is set.
    
    ```python
        parser.add_argument(
            "--grpc",
            action="store_true",
            help="Generate gRPC service code (stubs, serialization traits, 
etc.)",
        )
    
        parser.add_argument(
            "--grpc-backend",
            type=str,
            default=None,
            help="Specify gRPC backend (e.g., 'grpc++' for C++, 'grpcio' for 
Python). Defaults to standard backend for the language.",
        )
    ```
    
    ## Related issues
    
    Closes #3271
    
    ## Does this PR introduce any user-facing change?
    - [x] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
    N/A
---
 compiler/fory_compiler/cli.py             | 17 +++++++++++++++++
 compiler/fory_compiler/generators/base.py |  9 +++++++++
 2 files changed, 26 insertions(+)

diff --git a/compiler/fory_compiler/cli.py b/compiler/fory_compiler/cli.py
index 54147a496..35853804a 100644
--- a/compiler/fory_compiler/cli.py
+++ b/compiler/fory_compiler/cli.py
@@ -379,6 +379,12 @@ def parse_args(args: Optional[List[str]] = None) -> 
argparse.Namespace:
         help="Scan and print without deleting files",
     )
 
+    parser.add_argument(
+        "--grpc",
+        action="store_true",
+        help="Generate gRPC service code (stubs, serialization traits, etc.)",
+    )
+
     return parser.parse_args(args)
 
 
@@ -425,6 +431,7 @@ def compile_file(
     emit_fdl: bool = False,
     emit_fdl_path: Optional[Path] = None,
     resolve_cache: Optional[Dict[Path, Schema]] = None,
+    grpc: bool = False,
 ) -> bool:
     """Compile a single IDL file with import resolution.
 
@@ -486,11 +493,17 @@ def compile_file(
             output_dir=lang_output,
             package_override=package_override,
             go_nested_type_style=go_nested_type_style,
+            grpc=grpc,
         )
 
         generator_class = GENERATORS[lang]
         generator = generator_class(schema, options)
         files = generator.generate()
+
+        if grpc:
+            service_files = generator.generate_services()
+            files.extend(service_files)
+
         generator.write_files(files)
 
         for f in files:
@@ -511,6 +524,7 @@ def compile_file_recursive(
     stack: Set[Path],
     resolve_cache: Dict[Path, Schema],
     go_module_root: Optional[Path],
+    grpc: bool = False,
 ) -> bool:
     file_path = file_path.resolve()
     if file_path in generated:
@@ -574,6 +588,7 @@ def compile_file_recursive(
             stack,
             resolve_cache,
             go_module_root,
+            grpc,
         ):
             stack.remove(file_path)
             return False
@@ -588,6 +603,7 @@ def compile_file_recursive(
         emit_fdl,
         emit_fdl_path,
         resolve_cache,
+        grpc,
     )
     if ok:
         generated.add(file_path)
@@ -676,6 +692,7 @@ def cmd_compile(args: argparse.Namespace) -> int:
                 set(),
                 resolve_cache,
                 None,
+                args.grpc,
             ):
                 success = False
         except ImportError as e:
diff --git a/compiler/fory_compiler/generators/base.py 
b/compiler/fory_compiler/generators/base.py
index 69cdaee48..abe6cd668 100644
--- a/compiler/fory_compiler/generators/base.py
+++ b/compiler/fory_compiler/generators/base.py
@@ -47,6 +47,7 @@ class GeneratorOptions:
     output_dir: Path
     package_override: Optional[str] = None
     go_nested_type_style: Optional[str] = None
+    grpc: bool = False
 
 
 class BaseGenerator(ABC):
@@ -71,6 +72,14 @@ class BaseGenerator(ABC):
         """Generate code and return a list of generated files."""
         pass
 
+    def generate_services(self) -> List[GeneratedFile]:
+        """Generate service-related code (e.g. gRPC stubs).
+
+        Base implementation returns empty list. Subclasses should override
+        if they support service generation.
+        """
+        return []
+
     @abstractmethod
     def generate_type(self, field_type: FieldType, nullable: bool = False) -> 
str:
         """Generate the type string for a field type."""


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to