Ninja
Ninja

Reputation: 301

Creating grpc client request with repeated fields

I have proto file like this:

message StartAssignmentRequest {
  string additional_comment = 3;
  repeated RideSlip slips = 4;
}


message RideSlip{
  string slip_name = 2;
  string slip_ext = 3;
  string slip_link = 4;
}

Now I want to create its request and I am doing something like this:

req := &api.StartAssignmentRequest{
    AdditionalComment:"AdditionalComment",
    Slips: &api.RideSlip[],
}

but not having idea how I can send RideSlip data properly.

Upvotes: 2

Views: 2921

Answers (1)

blackgreen
blackgreen

Reputation: 44962

Protobuffer (both 2 and 3) repeated fields are compiled to slices in Go.

Just append to it:

req := &api.StartAssignmentRequest{
    AdditionalComment: "AdditionalComment",
}

req.Slips = append(req.Slips, &api.RideSlip{
    SlipName: "foo",
    SlipExt: "bar",
    SlipLink: "https://stackoverflow.com",
})

Or assign to it a literal value:

req := &api.StartAssignmentRequest{
    AdditionalComment: "AdditionalComment",
    Slips: []*api.RideSlip{
        {
            SlipName: "foo",
            SlipExt: "bar",
            SlipLink: "https://stackoverflow.com",
        },
    },
}

Upvotes: 1

Related Questions