我正在将gRPC与戈朗一起使用。我有一个非常简单的原型定义和一个gRPC服务。原始定义在谷歌/原型布夫/任何类型的认可中有一个字段。gRPC 服务无法将此字段映射到输入值,并且始终初始化为零
原型定义:
syntax = "proto3";
option go_package = "service";
option java_multiple_files = true;
option java_package = "io.grpc.consensus";
import "google/protobuf/any.proto";
package service;
service MyService {
rpc Verify (Payload) returns (Response) {}
}
message Response {
string policyId =1;
string txnId =2;
}
message Endorsement {
string endorserId=1;
// This is being initialise to nil by gRPC
google.protobuf.Any data = 2;
string signature=3;
bool isVerified=4;
}
message Payload {
string policyId =1;
string txnId =2;
repeated Endorsement endorsements=3;
}
使用这个,一个简单的gRPC服务被实现:
package service
import (
"log"
"golang.org/x/net/context"
)
type ServiceServerImpl struct {
}
func NewServiceServerImpl() *ServiceServerImpl {
return &ServiceServerImpl{}
}
func (s *ServiceServerImpl) Verify(ctx context.Context, txnPayload *Payload) (*Response, error) {
log.Printf("Got verification request: %s", txnPayload.TxnId)
for _, endorsement := range txnPayload.Endorsements {
j, err := endorsement.Data.UnmarshalNew()
if err != nil {
log.Print("Error while unmarshaling the endorsement")
}
if j==nil {
//This gets printed as payload's endorsement data is always null for google/protobuf/any type
log.Print("Data is null for endorsement")
}
}
return &Response{TxnId: txnPayload.TxnId, PolicyId: txnPayload.PolicyId}, nil
}
输入数据:
{
"policyId": "9dd97b1e-b76f-4c49-b067-22143c954e75",
"txnId": "231-4dc0-8e54-58231df6f0ce",
"endorsements": [
{
"endorserId": "67e1dfbd-1716-4d91-94ec-83dde64e4b80",
"data": {
"type": "issueTx",
"userId": 1,
"transaction": {
"amount": 10123.50
}
},
"signature": "MEUCIBkooxG2uFZeSEeaf5Xh5hWLxcKGMxCZzfnPshOh22y2AiEAwVLAaGhccUv8UhgC291qNWtxrGawX2pPsI7UUA/7QLM=",
"isVerified": false
}
]
}
月关宝盒
qq_笑_17
相关分类