R
R
rxecore2018-11-12 15:43:08
go
rxecore, 2018-11-12 15:43:08

How to organize nested messages in protobuf?

Good afternoon!
I need to read the message type and, depending on its type, already parse, in which I would do this

type Message strcut {
     typeMessage string
      Body interface {}// Суда я могу положить что угодно , так и в protobuf хотелось бы
}

or JSON example
var msg {
     typeMsg: user_info,
     body: user{
           id : 564,
           name:dwadswa 
},
 }

as an option i see
message Message {
       string typeMessage = 1;
       bytes  body = 2;
}

but I think this is a bad idea and there is a better option. Google did not give results or I did not understand them. Thank you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2018-11-12
@rxecore

Use google.protobuf.Any if the type is not known
or use oneof if there are a limited number of types and they are known

A
Alexander Pavlyuk, 2018-11-12
@pav5000

In the protobuffer, you will have to make several fields, one for each type of message. After receiving the type ID, access the required field.

K
klim0v, 2020-04-12
@klim0v

I would suggest using google.protobuf.Struct - it's just a prototype representation of a JSON object. Any proto3 message can be mechanically converted to JSON and embedded in a field of this type. It is a very flexible type and gives the benefits of dynamic typing for protobuf.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question