Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
module Gateway
# Storage for an actual function definition.
# Implements a `call` method so that it may be invoked with a simple hash of params.
class Function
attr_reader :service_name, :name, :rpc_desc
def initialize(service_name, service_stub, rpc_desc)
@service_name = service_name
@service_stub = service_stub
@rpc_desc = rpc_desc
@name = GRPC::GenericService.underscore(rpc_desc.name.to_s).to_sym
end
def to_s
"<GrpcFunction #{service_name}##{name} >"
end
def call(params = {}, metadata = {})
@service_stub.send(name, arg(params || {}), metadata: metadata).to_hash
end
private
# Build arguments to a func
def arg(params)
rpc_desc.input.decode_json(params.to_json)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment