I
I
Ilya2014-03-17 15:19:18
Python
Ilya, 2014-03-17 15:19:18

How to convert Protobuf Descriptor to .proto?

How to convert code like this:

DESCRIPTOR = descriptor.FileDescriptor(
  name='addressbook.proto',
  package='tutorial',
  serialized_pb='\n\x11\x61\x64\x64ressbook.proto\x12\x08tutorial\"\xda
\x01\n\x06Person\x12\x0c\n\x04name\x18\x01 \x02(\t\x12\n\n\x02id
\x18\x02 \x02(\x05\x12\r\n\x05\x65mail\x18\x03 \x03(\t\x12+\n\x05phone
\x18\x04 \x03(\x0b\x32\x1c.tutorial.Person.PhoneNumber\x1aM\n
\x0bPhoneNumber\x12\x0e\.........'

In the original addressbook.proto with the source code of the structure, like this:
message Car {
  required string model = 1;
  enum BodyType {
    sedan = 0;
    hatchback = 1;
    SUV = 2;
  }
  required BodyType type = 2 [default = sedan];
  optional string color = 3;
  required int32 year = 4; 
  message Owner {
    required string name = 1;
    required string lastName = 2; 
    required int64 driverLicense = 3;
  }
  repeated Owner previousOwner = 5;
}

I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xandox, 2014-03-18
@Gorily

Concerning the first - just make print serialized_pb should help
Concerning the second - generally in any way. The structure, as such, is not stored in the serialized data. The serialized data looks something like this
[Tag|Type|Value]
where Tag is the field tag (that is, you don't know the field name anymore, and its modifiers too (required, optional, repeated))
Type - see the docs, actual values ​​are 0 , 1, 2, 5 (like)
At the same time, Tag and Type go together (Tag << 3 | Type) and are encoded by varint (see the docs for what it is)
The most ambush is that nested messages are serialized in the same way as bytes and string, that is, if you don’t know what messege should be here, then only by trying to parse it can you understand it. In general, it is possible to extract a structure from the serialized data - it is partially possible, but it will not work to restore it to 100%.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question