In my gRPC service, which was written in golang , I have such rpc method
called CreateCity
. As you can see, in this method, I want to create a new record in the database and return all the information about this record as a response.
func (server *Server) CreateCity(context context.Context, request *proto.CreateCityRequest) (*proto.CreateCityResponse, error) {
city := proto.City {
Name: request.GetCity().Name,
Country: request.GetCity().Country,
}
err := databases.DBGORM.Table("city").Create(&city).Error
if err != nil {
utils.Logger().Println(err.Error())
return nil, status.Errorf(codes.Internal, err.Error())
}
result := &proto.CreateCityResponse {
City: &city,
}
return result, nil
}
proto file looks like this:
syntax = "proto3";
package proto;
import "google/protobuf/wrappers.proto";
import "google/protobuf/timestamp.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option go_package = "./proto";
service CityService {
rpc CreateCity(CreateCityRequest) returns (CreateCityResponse) {}
}
message City {
google.protobuf.StringValue name = 1 [json_name = "name", (gogoproto.jsontag) = "name", (gogoproto.wktpointer) = true];
google.protobuf.StringValue country = 2 [json_name = "country", (gogoproto.jsontag) = "country", (gogoproto.wktpointer) = true];
}
message CreateDealerGroupRequest {
City city = 1;
}
message CreateDealerGroupResponse {
City city = 1;
}
Is it possible to dynamically fill in the struct with data without explicitly specifying the name? As you can see now, I explicitly specify the name of the fields and their value:
city := proto.City {
Name: request.GetCity().Name,
Country: request.GetCity().Country,
}