Class: Protocol::GRPC::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/grpc/interface.rb

Overview

Represents an interface definition for gRPC methods. Can be used by both client stubs and server implementations.

Defined Under Namespace

Classes: RPC

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Interface

Initialize a new interface instance.



151
152
153
# File 'lib/protocol/grpc/interface.rb', line 151

def initialize(name)
	@name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



156
157
158
# File 'lib/protocol/grpc/interface.rb', line 156

def name
  @name
end

#The service name (e.g., "hello.Greeter").(servicename(e.g., "hello.Greeter")) ⇒ Object (readonly)



156
# File 'lib/protocol/grpc/interface.rb', line 156

attr :name

Class Method Details

.inherited(subclass) ⇒ Object

Hook called when a subclass is created. Initializes the RPC hash for the subclass.



64
65
66
67
68
# File 'lib/protocol/grpc/interface.rb', line 64

def self.inherited(subclass)
	super
	
	subclass.instance_variable_set(:@rpcs, {})
end

.lookup_rpc(name) ⇒ Object

Look up RPC definition for a method. Looks up the inheritance chain to find the RPC definition.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/protocol/grpc/interface.rb', line 117

def self.lookup_rpc(name)
	klass = self
	while klass && klass != Interface
		if klass.instance_variable_defined?(:@rpcs)
			if rpc = klass.instance_variable_get(:@rpcs)[name]
				return rpc
			end
		end
		klass = klass.superclass
	end
	
	# Not found:
	return nil
end

.pascal_case_to_snake_case(pascal_case) ⇒ Object

Convert PascalCase to snake_case.



170
171
172
173
174
175
# File 'lib/protocol/grpc/interface.rb', line 170

def self.pascal_case_to_snake_case(pascal_case)
	pascal_case
		.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')  # Insert underscore before capital letters followed by lowercase
		.gsub(/([a-z\d])([A-Z])/, '\1_\2')      # Insert underscore between lowercase/digit and uppercase
		.downcase
end

.rpc(name, request_class = nil, response_class = nil, **options) ⇒ Object

Define an RPC method.

Examples:

Using stream() decorator syntax

rpc :div, DivArgs, DivReply                      # unary
rpc :sum, stream(Num), Num                       # client streaming
rpc :fib, FibArgs, stream(Num)                   # server streaming
rpc :chat, stream(DivArgs), stream(DivReply)     # bidirectional streaming


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/protocol/grpc/interface.rb', line 83

def self.rpc(name, request_class = nil, response_class = nil, **options)
	options[:name] = name
	
	# Check if request or response are wrapped with stream()
	request_streaming = request_class.is_a?(Streaming)
	response_streaming = response_class.is_a?(Streaming)
	
	# Unwrap StreamWrapper if present
	options[:request_class] ||= request_streaming ? request_class.message_class : request_class
	options[:response_class] ||= response_streaming ? response_class.message_class : response_class
	
	# Auto-detect streaming type from stream() decorators if not explicitly set
	if !options.key?(:streaming)
		if request_streaming && response_streaming
			options[:streaming] = :bidirectional
		elsif request_streaming
			options[:streaming] = :client_streaming
		elsif response_streaming
			options[:streaming] = :server_streaming
		else
			options[:streaming] = :unary
		end
	end
	
	# Ensure snake_case method name is always available
	options[:method] ||= pascal_case_to_snake_case(name.to_s).to_sym
	
	@rpcs[name] = RPC.new(**options)
end

.rpcsObject

Get all RPC definitions from this class and all parent classes.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/protocol/grpc/interface.rb', line 134

def self.rpcs
	all_rpcs = {}
	klass = self
	
	# Walk up the inheritance chain:
	while klass && klass != Interface
		if klass.instance_variable_defined?(:@rpcs)
			all_rpcs.merge!(klass.instance_variable_get(:@rpcs))
		end
		klass = klass.superclass
	end
	
	all_rpcs
end

.stream(message_class) ⇒ Object

Helper method to mark a message type as streamed in RPC definitions. Can be called directly within Interface subclasses without the Protocol::GRPC prefix.

Examples:

Define streaming RPCs

class MyService < Protocol::GRPC::Interface
  rpc :sum, stream(Num), Num              # client streaming
  rpc :fib, FibArgs, stream(Num)          # server streaming
  rpc :chat, stream(Msg), stream(Msg)     # bidirectional streaming
end


57
58
59
# File 'lib/protocol/grpc/interface.rb', line 57

def self.stream(message_class)
	Streaming.new(message_class)
end

Instance Method Details

#path(method_name) ⇒ Object

Build gRPC path for a method.



161
162
163
# File 'lib/protocol/grpc/interface.rb', line 161

def path(method_name)
	Methods.build_path(@name, method_name.to_s)
end