I'm trying to convert an internal type to the protobuf generated type and I can't get the array to convert. I'm new to go so I don't know all the methods that could help. But this is my attempt. When run this code I get
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x86c724]
along with a lot other byte data. I'm wondering what the best way to convert internal structs to the protobufs is. I think I'm having the most trouble with the protobuf generated code being pointers.
Proto Definitions
message GameHistory {
message Game {
int64 gameId = 1;
}
repeated Game matches = 1;
string username = 2;
}
message GetRequest {
string username = 1;
}
message GetGameResponse {
GameHistory gameHistory = 1;
}
Go Code
// GameHistory model
type GameHistory struct {
Game []struct {
GameID int64 `json:"gameId"`
} `json:"games"`
UserName string `json:"username"`
}
func constructGameHistoryResponse(gameHistory models.GameHistory) *pb.GetGameResponse {
games := make([]*pb.GameHistory_Game, len(gameHistory.Games))
for i := range matchHistory.Matches {
games[i].GameID = gameHistory.Games[i].GameID
}
res := &pb.GetGameResponse{
GameHistory: &pb.GameHistory{
Games: games,
},
}
}