|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "protocol/grpc/interface" |
| 7 | +require_relative "parser" |
| 8 | + |
| 9 | +module Protocol |
| 10 | + module GRPC |
| 11 | + module Proto |
| 12 | + # Generator for Protocol Buffers service definitions. |
| 13 | + # Generates `Protocol::GRPC::Interface` and `Async::GRPC::Service` classes from parsed proto data. |
| 14 | + class Generator |
| 15 | + # Initialize the generator with parsed proto data. |
| 16 | + # @parameter proto_file [String] Path to the original `.proto` file (for header comments) |
| 17 | + # @parameter parsed_data [Hash] Parsed data from `Parser#parse` with `:package` and `:services` keys |
| 18 | + def initialize(proto_file, parsed_data) |
| 19 | + @proto_file = proto_file |
| 20 | + @package = parsed_data[:package] |
| 21 | + @services = parsed_data[:services] |
| 22 | + end |
| 23 | + |
| 24 | + # Generate the interface class code. |
| 25 | + # @parameter service_name [String] The service name |
| 26 | + # @parameter output_path [String | Nil] Optional path to write the file |
| 27 | + # @returns [String] The generated Ruby code |
| 28 | + def generate_interface(service_name, output_path: nil) |
| 29 | + service = @services.find{|s| s[:name] == service_name} |
| 30 | + raise ArgumentError, "Service #{service_name} not found" unless service |
| 31 | + |
| 32 | + package_module = normalize_package_name(@package) |
| 33 | + package_prefix = package_module.empty? ? "" : "#{package_module}::" |
| 34 | + |
| 35 | + code = <<~RUBY |
| 36 | + # frozen_string_literal: true |
| 37 | + |
| 38 | + # Generated from #{File.basename(@proto_file)} |
| 39 | + # DO NOT EDIT - This file is auto-generated |
| 40 | + |
| 41 | + require "protocol/grpc/interface" |
| 42 | + require_relative "#{File.basename(@proto_file, '.proto')}_pb" |
| 43 | + |
| 44 | + #{package_module.empty? ? '' : "module #{package_module}"} |
| 45 | + # Interface definition for the #{service_name} service |
| 46 | + class #{service_name}Interface < Protocol::GRPC::Interface |
| 47 | + #{service[:rpcs].map do |rpc| |
| 48 | + streaming_type = case rpc[:streaming] |
| 49 | + when :unary then ":unary" |
| 50 | + when :server_streaming then ":server_streaming" |
| 51 | + when :client_streaming then ":client_streaming" |
| 52 | + when :bidirectional then ":bidirectional" |
| 53 | + end |
| 54 | + |
| 55 | + "\t\trpc :#{rpc[:name]}, request_class: #{package_prefix}#{rpc[:request]}, response_class: #{package_prefix}#{rpc[:response]}, streaming: #{streaming_type}" |
| 56 | + end.join("\n")} |
| 57 | + end |
| 58 | + #{package_module.empty? ? '' : "end"} |
| 59 | + RUBY |
| 60 | + |
| 61 | + if output_path |
| 62 | + File.write(output_path, code) |
| 63 | + end |
| 64 | + |
| 65 | + code |
| 66 | + end |
| 67 | + |
| 68 | + # Generate the service class code with empty implementations. |
| 69 | + # @parameter service_name [String] The service name |
| 70 | + # @parameter output_path [String | Nil] Optional path to write the file |
| 71 | + # @returns [String] The generated Ruby code |
| 72 | + def generate_service(service_name, output_path: nil) |
| 73 | + service = @services.find{|s| s[:name] == service_name} |
| 74 | + raise ArgumentError, "Service #{service_name} not found" unless service |
| 75 | + |
| 76 | + package_module = normalize_package_name(@package) |
| 77 | + package_prefix = package_module.empty? ? "" : "#{package_module}::" |
| 78 | + interface_class = "#{package_prefix}#{service_name}Interface" |
| 79 | + |
| 80 | + methods = service[:rpcs].map do |rpc| |
| 81 | + method_name = pascal_to_snake(rpc[:name]) |
| 82 | + |
| 83 | + case rpc[:streaming] |
| 84 | + when :unary |
| 85 | + <<~RUBY |
| 86 | + def #{method_name}(input, output, _call) |
| 87 | + request = input.read |
| 88 | + # TODO: Implement #{rpc[:name]} |
| 89 | + # response = #{package_prefix}#{rpc[:response]}.new(...) |
| 90 | + # output.write(response) |
| 91 | + end |
| 92 | + RUBY |
| 93 | + when :server_streaming |
| 94 | + <<~RUBY |
| 95 | + def #{method_name}(input, output, _call) |
| 96 | + request = input.read |
| 97 | + # TODO: Implement #{rpc[:name]} streaming |
| 98 | + # response = #{package_prefix}#{rpc[:response]}.new(...) |
| 99 | + # output.write(response) |
| 100 | + end |
| 101 | + RUBY |
| 102 | + when :client_streaming |
| 103 | + <<~RUBY |
| 104 | + def #{method_name}(input, output, _call) |
| 105 | + # TODO: Implement #{rpc[:name]} client streaming |
| 106 | + # input.each do |request| |
| 107 | + # # Process request |
| 108 | + # end |
| 109 | + # response = #{package_prefix}#{rpc[:response]}.new(...) |
| 110 | + # output.write(response) |
| 111 | + end |
| 112 | + RUBY |
| 113 | + when :bidirectional |
| 114 | + <<~RUBY |
| 115 | + def #{method_name}(input, output, _call) |
| 116 | + # TODO: Implement #{rpc[:name]} bidirectional streaming |
| 117 | + # input.each do |request| |
| 118 | + # response = #{package_prefix}#{rpc[:response]}.new(...) |
| 119 | + # output.write(response) |
| 120 | + # end |
| 121 | + end |
| 122 | + RUBY |
| 123 | + end |
| 124 | + end.join("\n") |
| 125 | + |
| 126 | + code = <<~RUBY |
| 127 | + # frozen_string_literal: true |
| 128 | + |
| 129 | + # Generated from #{File.basename(@proto_file)} |
| 130 | + # DO NOT EDIT - This file is auto-generated |
| 131 | + |
| 132 | + require "async/grpc/service" |
| 133 | + require_relative "#{File.basename(@proto_file, '.proto')}_interface" |
| 134 | + |
| 135 | + #{package_module.empty? ? '' : "module #{package_module}"} |
| 136 | + # Service implementation for #{service_name} |
| 137 | + class #{service_name}Service < Async::GRPC::Service |
| 138 | + def initialize(service_name) |
| 139 | + super(#{interface_class}, service_name) |
| 140 | + end |
| 141 | + |
| 142 | + #{methods.split("\n").map{|line| "\t\t#{line}"}.join("\n")} |
| 143 | + end |
| 144 | + #{package_module.empty? ? '' : "end"} |
| 145 | + RUBY |
| 146 | + |
| 147 | + if output_path |
| 148 | + File.write(output_path, code) |
| 149 | + end |
| 150 | + |
| 151 | + code |
| 152 | + end |
| 153 | + |
| 154 | + private |
| 155 | + |
| 156 | + def normalize_package_name(package) |
| 157 | + return "" unless package |
| 158 | + |
| 159 | + package.split(".").map do |part| |
| 160 | + # Convert snake_case to PascalCase |
| 161 | + part.split("_").map(&:capitalize).join |
| 162 | + end.join("::") |
| 163 | + end |
| 164 | + |
| 165 | + def pascal_to_snake(pascal) |
| 166 | + pascal |
| 167 | + .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') |
| 168 | + .gsub(/([a-z\d])([A-Z])/, '\1_\2') |
| 169 | + .downcase |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | +end |
0 commit comments