Я хочу замаскировать сообщение protobuf, используя FieldMask
в Python.
proto file
syntax = "proto3";
message Msg {
message ImageInfo {
string url = 1;
int32 width = 2;
int32 height = 3;
}
string name = 1;
map<string, ImageInfo> image_info = 2;
}
Маскировка string
поле может быть успешно выполнено.
from google.protobuf import field_mask_pb2
import msg_pb2
original_msg = msg_pb2.Msg(name='image-0')
original_msg.image_info['main'].url = 'http://example.com/image-0.jpg'
original_msg.image_info['main'].width = 10
original_msg.image_info['main'].height = 10
field_mask=field_mask_pb2.FieldMask(paths=['name'])
new_msg = msg_pb2.Msg()
field_mask.MergeMessage(original_msg, new_msg)
Но, маскирование map<string, message>
не выполняется.
field_mask=field_mask_pb2.FieldMask(paths=['name', 'image_info'])
new_msg = msg_pb2.Msg()
field_mask.MergeMessage(original_msg, new_msg)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 473, in MergeMessage
source, destination, replace_message_field, replace_repeated_field) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 625, in MergeMessage
self._root, source, destination, replace_message, replace_repeated) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 667, in _MergeMessage
repeated_destination.add().MergeFrom(item) AttributeError: 'google.protobuf.pyext._message.MessageMapContainer' object has no attribute 'add'
Как его замаскировать?
Редактировать
Одним из решений является сообщение-обертка.Но это немного беспокоит.
syntax = "proto3";
message Msg {
message ImageInfo {
string url = 1;
int32 width = 2;
int32 height = 3;
}
message ImageInfoContainer {
map<string, ImageInfo> image_info = 1;
}
string name = 1;
ImageInfoContainer image_info = 2;
}