Skip to content

Latest commit

 

History

History
125 lines (98 loc) · 4.67 KB

File metadata and controls

125 lines (98 loc) · 4.67 KB

Multi-Language gRPC Code Generator - Complete Solution

Summary

Successfully implemented a unified build tool that generates gRPC server and client code for multiple programming languages from centralized Protocol Buffer definitions.

What Was Implemented

1. Centralized Proto Management

  • Created proto/ directory containing shared Protocol Buffer definitions
  • Added hero.proto and calculator.proto as examples
  • Enables single source of truth for all language implementations

2. Multi-Language Code Generation

  • Go: Uses protoc-gen-go and protoc-gen-go-grpc plugins
  • Python: Uses grpcio-tools with full type hint support (.pyi files)
  • TypeScript: Uses ts-proto with both general and NestJS-specific variants
  • Rust: Uses tonic-build for modern async gRPC implementation

3. Unified Build Tool (codegen.js)

  • Command-line interface with options for specific languages or all at once
  • Automatic dependency installation for each language
  • Cross-platform Protocol Buffers compiler auto-installation
  • Organized output directory structure
  • Comprehensive error handling and user feedback

4. NPM Integration

  • npm run build - Generate for all languages
  • npm run build:go - Go-specific generation
  • npm run build:python - Python-specific generation
  • npm run build:typescript - TypeScript/NestJS generation
  • npm run build:rust - Rust-specific generation

5. Generated Code Structure

generated/
├── go/                     # Go server/client (.pb.go, _grpc.pb.go files)
├── python/                 # Python server/client (.py, .pyi, _grpc.py files)
├── typescript/             # TypeScript interfaces and NestJS decorators
│   └── nestjs/            # NestJS-specific metadata and decorators
└── rust/                  # Complete Rust project with Cargo.toml and tonic

Key Features

✅ Unified Interface

  • Single command generates code for all supported languages
  • Consistent behavior across different platforms
  • Automatic tool installation and dependency management

✅ Production Ready

  • Proper error handling and validation
  • Comprehensive documentation and examples
  • Support for all gRPC features (unary, streaming, bidirectional)
  • Language-specific optimizations and best practices

✅ Developer Experience

  • Clear CLI interface with help and list options
  • Detailed progress reporting and error messages
  • Automatic cleanup of build artifacts
  • Comprehensive usage examples for each language

✅ Extensible Design

  • Easy to add new languages by extending the generators object
  • Modular structure allows for language-specific customizations
  • Configuration-driven approach

Usage Examples

Basic Usage

# Generate for all languages
npm run build

# Generate for specific language
npm run build:python

# List supported languages
node codegen.js --list

Adding New Proto Files

  1. Add .proto files to the proto/ directory
  2. Run npm run build
  3. Generated code appears in language-specific directories

Integration with Existing Projects

  • Go: Import generated packages
  • Python: Import generated modules
  • TypeScript/NestJS: Use generated interfaces and decorators
  • Rust: Add generated crate as dependency

Benefits Achieved

  1. Single Source of Truth: All languages use the same proto definitions
  2. Consistency: Generated code follows language-specific best practices
  3. Productivity: Developers can focus on business logic instead of gRPC boilerplate
  4. Maintainability: Changes to APIs are automatically propagated to all languages
  5. Quality: Generated code includes proper type definitions and error handling

Files Created/Modified

New Files

  • codegen.js - Main build tool
  • package.json - Node.js dependencies and scripts
  • proto/hero.proto - Hero service definition
  • proto/calculator.proto - Calculator service definition
  • BUILD.md - Build tool documentation
  • EXAMPLES.md - Usage examples
  • .gitignore - Ignore build artifacts
  • generated/ - Complete directory structure with generated code

Modified Files

  • README.md - Updated with new build tool information

Technical Implementation

The solution uses:

  • Node.js for the build tool (cross-platform compatibility)
  • Protocol Buffers compiler (auto-installed)
  • Language-specific plugins (automatically managed)
  • Structured output (organized by language)
  • Error handling (graceful failure with helpful messages)

This implementation fully satisfies the requirement to "build tool can generate proto file to server and client for multiple language (typescript nestjs, go, python, rust). use same proto folder contain proto file and generate."