Extension protobuf
Есть такой файл test_message.proto
// Protocol version: v.2.5.0
syntax = "proto2";
message TestMessage {
required string id = 1;
// Extensions.
extensions 100 to 9999;
}
сообщение в json из которого выглядит так:
{
"id":"id_str"
}
Мне нужно добавить расширение к этому сообщению чтобы поддерживался такой json:
{
"id":"id_str",
"ext": {
"ext_field_1":"string_value"
}
}
в отдельном файле test_message_ext.proto я пишу расширение:
syntax = "proto2";
import "test_message.proto";
message TestMessageExt {
optional string ext_field_1 = 1;
}
extend TestMessage {
optional TestMessageExt ext = 201;
}
запускаю protoc --python_out=. ./*.prot
и пытаюсь распарсить расширенный json
import test_message_pb2
import test_message_ext_pb2
from google.protobuf import json_format
file = open("./msg.json")
msgJson = file.read()
msg = test_message_pb2.TestMessage()
json_format.Parse(msgJson,msg)
print(msg.id)
Получаю ошибку:
google.protobuf.json_format.ParseError: Message type "TestMessage" has no field named "ext" at "TestMessage".
Available Fields(except extensions): "['id']"
Вопрос: что нужно поменять что бы распарсить json в protobuf?