sweapper
sweapper

Reputation: 11

How to get a custom go type using gogo/protobuf

My code in .proto file currently looks like this:

message Category {
    int64 CategoryID = 1;
}

message Categories {
    repeated Category cat = 1;
}

When I run protoc --gogofaster_out=. *.proto the output I get is:

type Category struct {
    CategoryID int64
}

type Categories struct {
    Cat []*Category
}

But what I actually want is:

type Category struct {
    CategoryID int64
}

type Categories []*Category

What does my code in .proto file need to be to get the desired output?

Upvotes: 1

Views: 1085

Answers (1)

kee
kee

Reputation: 46

Protobuf is basically a mechanism to serialize structured data. That means that before sending a proto 'message', it has to be serialized. When you compile this proto for different languages, it generated appropriate classes (for c++/Java), structs for Golang. In your case, "type Categories []*Category" is not a message rather a standalone entity that could not be serialized. ( I can be corrected here). Refer Protobuf Language guide https://developers.google.com/protocol-buffers/docs/proto3

If the intention here is to have an array of Category types and serialize them, it is recommended to encapsulate it in a message.

Upvotes: 1

Related Questions