- ruby > 2.3.7
- grpc
- google-protobuf
This is a simple library to convert a hash to grpc object
eg.
Sample Proto file (.proto) :
syntax = "proto3";
package cpqgrpc.product.v1;
message ProductRequest {
uint64 id = 2;
string name = 3;
repeated string categories = 4;
}
message ProductResp {
uint64 id = 2;
string name = 3;
repeated string categories = 4;
}
service Product {
rpc GetProduct (ProductRequest) returns (ProductResp);
}
Sample generated service (.rb):
require 'google/protobuf'
require 'cpqgrpc/custom_types_pb'
Google::Protobuf::DescriptorPool.generated_pool.build do
add_message "cpqgrpc.product.v1.ProductRequest" do
optional :id, :uint64, 2
optional :name, :string, 3
repeated :categories, :string, 4
end
end
module Cpqgrpc
module Product
module V1
ProductRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("cpqgrpc.product.v1.ProductRequest").msgclass
ProductResp = Google::Protobuf::DescriptorPool.generated_pool.lookup("cpqgrpc.product.v1.ProductResp").msgclass
end
end
end
-
To convert hash { id: 1, name: 'Macbook' } to Grpc object Cpqgrpc::Product::V1::ProductRequest use below
GrpcDataTransformers.hash_to_grpc_object( { id: 1, name: 'Macbook', categories: ['hardware', 'laptop'] })