pinecone.core.grpc.protos.vector_service_pb2
Generated protocol buffer code.
1# -*- coding: utf-8 -*- 2# Generated by the protocol buffer compiler. DO NOT EDIT! 3# source: vector_service.proto 4"""Generated protocol buffer code.""" 5from google.protobuf import descriptor as _descriptor 6from google.protobuf import message as _message 7from google.protobuf import reflection as _reflection 8from google.protobuf import symbol_database as _symbol_database 9 10# @@protoc_insertion_point(imports) 11 12_sym_db = _symbol_database.Default() 13 14 15from google.protobuf import struct_pb2 as google_dot_protobuf_dot_struct__pb2 16from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2 17from google.api import field_behavior_pb2 as google_dot_api_dot_field__behavior__pb2 18from protoc_gen_openapiv2.options import annotations_pb2 as protoc__gen__openapiv2_dot_options_dot_annotations__pb2 19 20 21DESCRIPTOR = _descriptor.FileDescriptor( 22 name="vector_service.proto", 23 package="", 24 syntax="proto3", 25 serialized_options=b'\n\021io.pinecone.protoP\001Z/github.com/pinecone-io/new-go-pinecone/pinecone\222A\366\002\022K\n\014Pinecone API";\n\017Pinecone.io Ops\022\023https://pinecone.io\032\023support@pinecone.io\0329{index_name}-{project_name}.svc.{environment}.pinecone.io*\001\0022\020application/json:\020application/jsonZx\nv\n\nApiKeyAuth\022h\010\002\022YAn API Key is required to call Pinecone APIs. Get yours at https://www.pinecone.io/start/\032\007Api-Key \002b\020\n\016\n\nApiKeyAuth\022\000r9\n\031More Pinecone.io API docs\022\034https://www.pinecone.io/docs', 26 create_key=_descriptor._internal_create_key, 27 serialized_pb=b'\n\x14vector_service.proto\x1a\x1cgoogle/protobuf/struct.proto\x1a\x1cgoogle/api/annotations.proto\x1a\x1fgoogle/api/field_behavior.proto\x1a.protoc-gen-openapiv2/options/annotations.proto"~\n\x0cSparseValues\x12\x35\n\x07indices\x18\x01 \x03(\rB$\x92\x41\x1eJ\x16[1, 312, 822, 14, 980]x\xe8\x07\x80\x01\x01\xe0\x41\x02\x12\x37\n\x06values\x18\x02 \x03(\x02\x42\'\x92\x41!J\x19[0.1, 0.2, 0.3, 0.4, 0.5]x\xe8\x07\x80\x01\x01\xe0\x41\x02"\xfd\x01\n\x06Vector\x12,\n\x02id\x18\x01 \x01(\tB \x92\x41\x1aJ\x12"example-vector-1"x\x80\x04\x80\x01\x01\xe0\x41\x02\x12G\n\x06values\x18\x02 \x03(\x02\x42\x37\x92\x41\x31J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\xa0\x9c\x01\x80\x01\x01\xe0\x41\x02\x12$\n\rsparse_values\x18\x04 \x01(\x0b\x32\r.SparseValues\x12V\n\x08metadata\x18\x03 \x01(\x0b\x32\x17.google.protobuf.StructB+\x92\x41(J&{"genre": "documentary", "year": 2019}"\x93\x02\n\x0cScoredVector\x12,\n\x02id\x18\x01 \x01(\tB \x92\x41\x1aJ\x12"example-vector-1"x\x80\x04\x80\x01\x01\xe0\x41\x02\x12\x18\n\x05score\x18\x02 \x01(\x02\x42\t\x92\x41\x06J\x04\x30.08\x12=\n\x06values\x18\x03 \x03(\x02\x42-\x92\x41*J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]\x12$\n\rsparse_values\x18\x05 \x01(\x0b\x32\r.SparseValues\x12V\n\x08metadata\x18\x04 \x01(\x0b\x32\x17.google.protobuf.StructB+\x92\x41(J&{"genre": "documentary", "year": 2019}"d\n\rUpsertRequest\x12&\n\x07vectors\x18\x01 \x03(\x0b\x32\x07.VectorB\x0c\x92\x41\x06x\xe8\x07\x80\x01\x01\xe0\x41\x02\x12+\n\tnamespace\x18\x02 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace""1\n\x0eUpsertResponse\x12\x1f\n\x0eupserted_count\x18\x01 \x01(\rB\x07\x92\x41\x04J\x02\x31\x30"\xb6\x01\n\rDeleteRequest\x12(\n\x03ids\x18\x01 \x03(\tB\x1b\x92\x41\x18J\x10["id-0", "id-1"]x\xe8\x07\x80\x01\x01\x12%\n\ndelete_all\x18\x02 \x01(\x08\x42\x11\x92\x41\x0e:\x05\x66\x61lseJ\x05\x66\x61lse\x12+\n\tnamespace\x18\x03 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace"\x12\'\n\x06\x66ilter\x18\x04 \x01(\x0b\x32\x17.google.protobuf.Struct"\x10\n\x0e\x44\x65leteResponse"h\n\x0c\x46\x65tchRequest\x12+\n\x03ids\x18\x01 \x03(\tB\x1e\x92\x41\x18J\x10["id-0", "id-1"]x\xe8\x07\x80\x01\x01\xe0\x41\x02\x12+\n\tnamespace\x18\x02 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace""\xa3\x01\n\rFetchResponse\x12,\n\x07vectors\x18\x01 \x03(\x0b\x32\x1b.FetchResponse.VectorsEntry\x12+\n\tnamespace\x18\x02 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace"\x1a\x37\n\x0cVectorsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\x16\n\x05value\x18\x02 \x01(\x0b\x32\x07.Vector:\x02\x38\x01"\xd0\x02\n\x0bQueryVector\x12G\n\x06values\x18\x01 \x03(\x02\x42\x37\x92\x41\x31J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\xa0\x9c\x01\x80\x01\x01\xe0\x41\x02\x12$\n\rsparse_values\x18\x05 \x01(\x0b\x32\r.SparseValues\x12(\n\x05top_k\x18\x02 \x01(\rB\x19\x92\x41\x16J\x02\x31\x30Y\x00\x00\x00\x00\x00\x88\xc3@i\x00\x00\x00\x00\x00\x00\xf0?\x12+\n\tnamespace\x18\x03 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace"\x12{\n\x06\x66ilter\x18\x04 \x01(\x0b\x32\x17.google.protobuf.StructBR\x92\x41OJM{"genre": {"$in": ["comedy", "documentary", "drama"]}, "year": {"$eq": 2019}}"\xfa\x03\n\x0cQueryRequest\x12+\n\tnamespace\x18\x01 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace"\x12+\n\x05top_k\x18\x02 \x01(\rB\x1c\x92\x41\x16J\x02\x31\x30Y\x00\x00\x00\x00\x00\x88\xc3@i\x00\x00\x00\x00\x00\x00\xf0?\xe0\x41\x02\x12{\n\x06\x66ilter\x18\x03 \x01(\x0b\x32\x17.google.protobuf.StructBR\x92\x41OJM{"genre": {"$in": ["comedy", "documentary", "drama"]}, "year": {"$eq": 2019}}\x12(\n\x0einclude_values\x18\x04 \x01(\x08\x42\x10\x92\x41\r:\x05\x66\x61lseJ\x04true\x12*\n\x10include_metadata\x18\x05 \x01(\x08\x42\x10\x92\x41\r:\x05\x66\x61lseJ\x04true\x12)\n\x07queries\x18\x06 \x03(\x0b\x32\x0c.QueryVectorB\n\x18\x01\x92\x41\x05x\n\x80\x01\x01\x12\x44\n\x06vector\x18\x07 \x03(\x02\x42\x34\x92\x41\x31J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\xa0\x9c\x01\x80\x01\x01\x12$\n\rsparse_vector\x18\t \x01(\x0b\x32\r.SparseValues\x12&\n\x02id\x18\x08 \x01(\tB\x1a\x92\x41\x17J\x12"example-vector-1"x\x80\x04"a\n\x12SingleQueryResults\x12\x1e\n\x07matches\x18\x01 \x03(\x0b\x32\r.ScoredVector\x12+\n\tnamespace\x18\x02 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace""l\n\rQueryResponse\x12(\n\x07results\x18\x01 \x03(\x0b\x32\x13.SingleQueryResultsB\x02\x18\x01\x12\x1e\n\x07matches\x18\x02 \x03(\x0b\x32\r.ScoredVector\x12\x11\n\tnamespace\x18\x03 \x01(\t"\xb2\x02\n\rUpdateRequest\x12,\n\x02id\x18\x01 \x01(\tB \x92\x41\x1aJ\x12"example-vector-1"x\x80\x04\x80\x01\x01\xe0\x41\x02\x12\x44\n\x06values\x18\x02 \x03(\x02\x42\x34\x92\x41\x31J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\xa0\x9c\x01\x80\x01\x01\x12$\n\rsparse_values\x18\x05 \x01(\x0b\x32\r.SparseValues\x12Z\n\x0cset_metadata\x18\x03 \x01(\x0b\x32\x17.google.protobuf.StructB+\x92\x41(J&{"genre": "documentary", "year": 2019}\x12+\n\tnamespace\x18\x04 \x01(\tB\x18\x92\x41\x15J\x13"example-namespace""\x10\n\x0eUpdateResponse"D\n\x19\x44\x65scribeIndexStatsRequest\x12\'\n\x06\x66ilter\x18\x01 \x01(\x0b\x32\x17.google.protobuf.Struct"4\n\x10NamespaceSummary\x12 \n\x0cvector_count\x18\x01 \x01(\rB\n\x92\x41\x07J\x05\x35\x30\x30\x30\x30"\x9a\x03\n\x1a\x44\x65scribeIndexStatsResponse\x12?\n\nnamespaces\x18\x01 \x03(\x0b\x32+.DescribeIndexStatsResponse.NamespacesEntry\x12\x1c\n\tdimension\x18\x02 \x01(\rB\t\x92\x41\x06J\x04\x31\x30\x32\x34\x12 \n\x0eindex_fullness\x18\x03 \x01(\x02\x42\x08\x92\x41\x05J\x03\x30.4\x12&\n\x12total_vector_count\x18\x04 \x01(\rB\n\x92\x41\x07J\x05\x38\x30\x30\x30\x30\x1a\x44\n\x0fNamespacesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12 \n\x05value\x18\x02 \x01(\x0b\x32\x11.NamespaceSummary:\x02\x38\x01:\x8c\x01\x92\x41\x88\x01\x32\x85\x01{"namespaces": {"": {"vectorCount": 50000}, "example-namespace-2": {"vectorCount": 30000}}, "dimension": 1024, "index_fullness": 0.4}2\xbd\x05\n\rVectorService\x12\x63\n\x06Upsert\x12\x0e.UpsertRequest\x1a\x0f.UpsertResponse"8\x82\xd3\xe4\x93\x02\x14"\x0f/vectors/upsert:\x01*\x92\x41\x1b\n\x11Vector Operations*\x06upsert\x12v\n\x06\x44\x65lete\x12\x0e.DeleteRequest\x1a\x0f.DeleteResponse"K\x82\xd3\xe4\x93\x02\'"\x0f/vectors/delete:\x01*Z\x11*\x0f/vectors/delete\x92\x41\x1b\n\x11Vector Operations*\x06\x64\x65lete\x12[\n\x05\x46\x65tch\x12\r.FetchRequest\x1a\x0e.FetchResponse"3\x82\xd3\xe4\x93\x02\x10\x12\x0e/vectors/fetch\x92\x41\x1a\n\x11Vector Operations*\x05\x66\x65tch\x12V\n\x05Query\x12\r.QueryRequest\x1a\x0e.QueryResponse".\x82\xd3\xe4\x93\x02\x0b"\x06/query:\x01*\x92\x41\x1a\n\x11Vector Operations*\x05query\x12\x63\n\x06Update\x12\x0e.UpdateRequest\x1a\x0f.UpdateResponse"8\x82\xd3\xe4\x93\x02\x14"\x0f/vectors/update:\x01*\x92\x41\x1b\n\x11Vector Operations*\x06update\x12\xb4\x01\n\x12\x44\x65scribeIndexStats\x12\x1a.DescribeIndexStatsRequest\x1a\x1b.DescribeIndexStatsResponse"e\x82\xd3\xe4\x93\x02\x33"\x15/describe_index_stats:\x01*Z\x17\x12\x15/describe_index_stats\x92\x41)\n\x11Vector Operations*\x14\x64\x65scribe_index_statsB\xc0\x03\n\x11io.pinecone.protoP\x01Z/github.com/pinecone-io/new-go-pinecone/pinecone\x92\x41\xf6\x02\x12K\n\x0cPinecone API";\n\x0fPinecone.io Ops\x12\x13https://pinecone.io\x1a\x13support@pinecone.io\x1a\x39{index_name}-{project_name}.svc.{environment}.pinecone.io*\x01\x02\x32\x10\x61pplication/json:\x10\x61pplication/jsonZx\nv\n\nApiKeyAuth\x12h\x08\x02\x12YAn API Key is required to call Pinecone APIs. Get yours at https://www.pinecone.io/start/\x1a\x07\x41pi-Key \x02\x62\x10\n\x0e\n\nApiKeyAuth\x12\x00r9\n\x19More Pinecone.io API docs\x12\x1chttps://www.pinecone.io/docsb\x06proto3', 28 dependencies=[ 29 google_dot_protobuf_dot_struct__pb2.DESCRIPTOR, 30 google_dot_api_dot_annotations__pb2.DESCRIPTOR, 31 google_dot_api_dot_field__behavior__pb2.DESCRIPTOR, 32 protoc__gen__openapiv2_dot_options_dot_annotations__pb2.DESCRIPTOR, 33 ], 34) 35 36 37_SPARSEVALUES = _descriptor.Descriptor( 38 name="SparseValues", 39 full_name="SparseValues", 40 filename=None, 41 file=DESCRIPTOR, 42 containing_type=None, 43 create_key=_descriptor._internal_create_key, 44 fields=[ 45 _descriptor.FieldDescriptor( 46 name="indices", 47 full_name="SparseValues.indices", 48 index=0, 49 number=1, 50 type=13, 51 cpp_type=3, 52 label=3, 53 has_default_value=False, 54 default_value=[], 55 message_type=None, 56 enum_type=None, 57 containing_type=None, 58 is_extension=False, 59 extension_scope=None, 60 serialized_options=b"\222A\036J\026[1, 312, 822, 14, 980]x\350\007\200\001\001\340A\002", 61 file=DESCRIPTOR, 62 create_key=_descriptor._internal_create_key, 63 ), 64 _descriptor.FieldDescriptor( 65 name="values", 66 full_name="SparseValues.values", 67 index=1, 68 number=2, 69 type=2, 70 cpp_type=6, 71 label=3, 72 has_default_value=False, 73 default_value=[], 74 message_type=None, 75 enum_type=None, 76 containing_type=None, 77 is_extension=False, 78 extension_scope=None, 79 serialized_options=b"\222A!J\031[0.1, 0.2, 0.3, 0.4, 0.5]x\350\007\200\001\001\340A\002", 80 file=DESCRIPTOR, 81 create_key=_descriptor._internal_create_key, 82 ), 83 ], 84 extensions=[], 85 nested_types=[], 86 enum_types=[], 87 serialized_options=None, 88 is_extendable=False, 89 syntax="proto3", 90 extension_ranges=[], 91 oneofs=[], 92 serialized_start=165, 93 serialized_end=291, 94) 95 96 97_VECTOR = _descriptor.Descriptor( 98 name="Vector", 99 full_name="Vector", 100 filename=None, 101 file=DESCRIPTOR, 102 containing_type=None, 103 create_key=_descriptor._internal_create_key, 104 fields=[ 105 _descriptor.FieldDescriptor( 106 name="id", 107 full_name="Vector.id", 108 index=0, 109 number=1, 110 type=9, 111 cpp_type=9, 112 label=1, 113 has_default_value=False, 114 default_value=b"".decode("utf-8"), 115 message_type=None, 116 enum_type=None, 117 containing_type=None, 118 is_extension=False, 119 extension_scope=None, 120 serialized_options=b'\222A\032J\022"example-vector-1"x\200\004\200\001\001\340A\002', 121 file=DESCRIPTOR, 122 create_key=_descriptor._internal_create_key, 123 ), 124 _descriptor.FieldDescriptor( 125 name="values", 126 full_name="Vector.values", 127 index=1, 128 number=2, 129 type=2, 130 cpp_type=6, 131 label=3, 132 has_default_value=False, 133 default_value=[], 134 message_type=None, 135 enum_type=None, 136 containing_type=None, 137 is_extension=False, 138 extension_scope=None, 139 serialized_options=b"\222A1J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\240\234\001\200\001\001\340A\002", 140 file=DESCRIPTOR, 141 create_key=_descriptor._internal_create_key, 142 ), 143 _descriptor.FieldDescriptor( 144 name="sparse_values", 145 full_name="Vector.sparse_values", 146 index=2, 147 number=4, 148 type=11, 149 cpp_type=10, 150 label=1, 151 has_default_value=False, 152 default_value=None, 153 message_type=None, 154 enum_type=None, 155 containing_type=None, 156 is_extension=False, 157 extension_scope=None, 158 serialized_options=None, 159 file=DESCRIPTOR, 160 create_key=_descriptor._internal_create_key, 161 ), 162 _descriptor.FieldDescriptor( 163 name="metadata", 164 full_name="Vector.metadata", 165 index=3, 166 number=3, 167 type=11, 168 cpp_type=10, 169 label=1, 170 has_default_value=False, 171 default_value=None, 172 message_type=None, 173 enum_type=None, 174 containing_type=None, 175 is_extension=False, 176 extension_scope=None, 177 serialized_options=b'\222A(J&{"genre": "documentary", "year": 2019}', 178 file=DESCRIPTOR, 179 create_key=_descriptor._internal_create_key, 180 ), 181 ], 182 extensions=[], 183 nested_types=[], 184 enum_types=[], 185 serialized_options=None, 186 is_extendable=False, 187 syntax="proto3", 188 extension_ranges=[], 189 oneofs=[], 190 serialized_start=294, 191 serialized_end=547, 192) 193 194 195_SCOREDVECTOR = _descriptor.Descriptor( 196 name="ScoredVector", 197 full_name="ScoredVector", 198 filename=None, 199 file=DESCRIPTOR, 200 containing_type=None, 201 create_key=_descriptor._internal_create_key, 202 fields=[ 203 _descriptor.FieldDescriptor( 204 name="id", 205 full_name="ScoredVector.id", 206 index=0, 207 number=1, 208 type=9, 209 cpp_type=9, 210 label=1, 211 has_default_value=False, 212 default_value=b"".decode("utf-8"), 213 message_type=None, 214 enum_type=None, 215 containing_type=None, 216 is_extension=False, 217 extension_scope=None, 218 serialized_options=b'\222A\032J\022"example-vector-1"x\200\004\200\001\001\340A\002', 219 file=DESCRIPTOR, 220 create_key=_descriptor._internal_create_key, 221 ), 222 _descriptor.FieldDescriptor( 223 name="score", 224 full_name="ScoredVector.score", 225 index=1, 226 number=2, 227 type=2, 228 cpp_type=6, 229 label=1, 230 has_default_value=False, 231 default_value=float(0), 232 message_type=None, 233 enum_type=None, 234 containing_type=None, 235 is_extension=False, 236 extension_scope=None, 237 serialized_options=b"\222A\006J\0040.08", 238 file=DESCRIPTOR, 239 create_key=_descriptor._internal_create_key, 240 ), 241 _descriptor.FieldDescriptor( 242 name="values", 243 full_name="ScoredVector.values", 244 index=2, 245 number=3, 246 type=2, 247 cpp_type=6, 248 label=3, 249 has_default_value=False, 250 default_value=[], 251 message_type=None, 252 enum_type=None, 253 containing_type=None, 254 is_extension=False, 255 extension_scope=None, 256 serialized_options=b"\222A*J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]", 257 file=DESCRIPTOR, 258 create_key=_descriptor._internal_create_key, 259 ), 260 _descriptor.FieldDescriptor( 261 name="sparse_values", 262 full_name="ScoredVector.sparse_values", 263 index=3, 264 number=5, 265 type=11, 266 cpp_type=10, 267 label=1, 268 has_default_value=False, 269 default_value=None, 270 message_type=None, 271 enum_type=None, 272 containing_type=None, 273 is_extension=False, 274 extension_scope=None, 275 serialized_options=None, 276 file=DESCRIPTOR, 277 create_key=_descriptor._internal_create_key, 278 ), 279 _descriptor.FieldDescriptor( 280 name="metadata", 281 full_name="ScoredVector.metadata", 282 index=4, 283 number=4, 284 type=11, 285 cpp_type=10, 286 label=1, 287 has_default_value=False, 288 default_value=None, 289 message_type=None, 290 enum_type=None, 291 containing_type=None, 292 is_extension=False, 293 extension_scope=None, 294 serialized_options=b'\222A(J&{"genre": "documentary", "year": 2019}', 295 file=DESCRIPTOR, 296 create_key=_descriptor._internal_create_key, 297 ), 298 ], 299 extensions=[], 300 nested_types=[], 301 enum_types=[], 302 serialized_options=None, 303 is_extendable=False, 304 syntax="proto3", 305 extension_ranges=[], 306 oneofs=[], 307 serialized_start=550, 308 serialized_end=825, 309) 310 311 312_UPSERTREQUEST = _descriptor.Descriptor( 313 name="UpsertRequest", 314 full_name="UpsertRequest", 315 filename=None, 316 file=DESCRIPTOR, 317 containing_type=None, 318 create_key=_descriptor._internal_create_key, 319 fields=[ 320 _descriptor.FieldDescriptor( 321 name="vectors", 322 full_name="UpsertRequest.vectors", 323 index=0, 324 number=1, 325 type=11, 326 cpp_type=10, 327 label=3, 328 has_default_value=False, 329 default_value=[], 330 message_type=None, 331 enum_type=None, 332 containing_type=None, 333 is_extension=False, 334 extension_scope=None, 335 serialized_options=b"\222A\006x\350\007\200\001\001\340A\002", 336 file=DESCRIPTOR, 337 create_key=_descriptor._internal_create_key, 338 ), 339 _descriptor.FieldDescriptor( 340 name="namespace", 341 full_name="UpsertRequest.namespace", 342 index=1, 343 number=2, 344 type=9, 345 cpp_type=9, 346 label=1, 347 has_default_value=False, 348 default_value=b"".decode("utf-8"), 349 message_type=None, 350 enum_type=None, 351 containing_type=None, 352 is_extension=False, 353 extension_scope=None, 354 serialized_options=b'\222A\025J\023"example-namespace"', 355 file=DESCRIPTOR, 356 create_key=_descriptor._internal_create_key, 357 ), 358 ], 359 extensions=[], 360 nested_types=[], 361 enum_types=[], 362 serialized_options=None, 363 is_extendable=False, 364 syntax="proto3", 365 extension_ranges=[], 366 oneofs=[], 367 serialized_start=827, 368 serialized_end=927, 369) 370 371 372_UPSERTRESPONSE = _descriptor.Descriptor( 373 name="UpsertResponse", 374 full_name="UpsertResponse", 375 filename=None, 376 file=DESCRIPTOR, 377 containing_type=None, 378 create_key=_descriptor._internal_create_key, 379 fields=[ 380 _descriptor.FieldDescriptor( 381 name="upserted_count", 382 full_name="UpsertResponse.upserted_count", 383 index=0, 384 number=1, 385 type=13, 386 cpp_type=3, 387 label=1, 388 has_default_value=False, 389 default_value=0, 390 message_type=None, 391 enum_type=None, 392 containing_type=None, 393 is_extension=False, 394 extension_scope=None, 395 serialized_options=b"\222A\004J\00210", 396 file=DESCRIPTOR, 397 create_key=_descriptor._internal_create_key, 398 ), 399 ], 400 extensions=[], 401 nested_types=[], 402 enum_types=[], 403 serialized_options=None, 404 is_extendable=False, 405 syntax="proto3", 406 extension_ranges=[], 407 oneofs=[], 408 serialized_start=929, 409 serialized_end=978, 410) 411 412 413_DELETEREQUEST = _descriptor.Descriptor( 414 name="DeleteRequest", 415 full_name="DeleteRequest", 416 filename=None, 417 file=DESCRIPTOR, 418 containing_type=None, 419 create_key=_descriptor._internal_create_key, 420 fields=[ 421 _descriptor.FieldDescriptor( 422 name="ids", 423 full_name="DeleteRequest.ids", 424 index=0, 425 number=1, 426 type=9, 427 cpp_type=9, 428 label=3, 429 has_default_value=False, 430 default_value=[], 431 message_type=None, 432 enum_type=None, 433 containing_type=None, 434 is_extension=False, 435 extension_scope=None, 436 serialized_options=b'\222A\030J\020["id-0", "id-1"]x\350\007\200\001\001', 437 file=DESCRIPTOR, 438 create_key=_descriptor._internal_create_key, 439 ), 440 _descriptor.FieldDescriptor( 441 name="delete_all", 442 full_name="DeleteRequest.delete_all", 443 index=1, 444 number=2, 445 type=8, 446 cpp_type=7, 447 label=1, 448 has_default_value=False, 449 default_value=False, 450 message_type=None, 451 enum_type=None, 452 containing_type=None, 453 is_extension=False, 454 extension_scope=None, 455 serialized_options=b"\222A\016:\005falseJ\005false", 456 file=DESCRIPTOR, 457 create_key=_descriptor._internal_create_key, 458 ), 459 _descriptor.FieldDescriptor( 460 name="namespace", 461 full_name="DeleteRequest.namespace", 462 index=2, 463 number=3, 464 type=9, 465 cpp_type=9, 466 label=1, 467 has_default_value=False, 468 default_value=b"".decode("utf-8"), 469 message_type=None, 470 enum_type=None, 471 containing_type=None, 472 is_extension=False, 473 extension_scope=None, 474 serialized_options=b'\222A\025J\023"example-namespace"', 475 file=DESCRIPTOR, 476 create_key=_descriptor._internal_create_key, 477 ), 478 _descriptor.FieldDescriptor( 479 name="filter", 480 full_name="DeleteRequest.filter", 481 index=3, 482 number=4, 483 type=11, 484 cpp_type=10, 485 label=1, 486 has_default_value=False, 487 default_value=None, 488 message_type=None, 489 enum_type=None, 490 containing_type=None, 491 is_extension=False, 492 extension_scope=None, 493 serialized_options=None, 494 file=DESCRIPTOR, 495 create_key=_descriptor._internal_create_key, 496 ), 497 ], 498 extensions=[], 499 nested_types=[], 500 enum_types=[], 501 serialized_options=None, 502 is_extendable=False, 503 syntax="proto3", 504 extension_ranges=[], 505 oneofs=[], 506 serialized_start=981, 507 serialized_end=1163, 508) 509 510 511_DELETERESPONSE = _descriptor.Descriptor( 512 name="DeleteResponse", 513 full_name="DeleteResponse", 514 filename=None, 515 file=DESCRIPTOR, 516 containing_type=None, 517 create_key=_descriptor._internal_create_key, 518 fields=[], 519 extensions=[], 520 nested_types=[], 521 enum_types=[], 522 serialized_options=None, 523 is_extendable=False, 524 syntax="proto3", 525 extension_ranges=[], 526 oneofs=[], 527 serialized_start=1165, 528 serialized_end=1181, 529) 530 531 532_FETCHREQUEST = _descriptor.Descriptor( 533 name="FetchRequest", 534 full_name="FetchRequest", 535 filename=None, 536 file=DESCRIPTOR, 537 containing_type=None, 538 create_key=_descriptor._internal_create_key, 539 fields=[ 540 _descriptor.FieldDescriptor( 541 name="ids", 542 full_name="FetchRequest.ids", 543 index=0, 544 number=1, 545 type=9, 546 cpp_type=9, 547 label=3, 548 has_default_value=False, 549 default_value=[], 550 message_type=None, 551 enum_type=None, 552 containing_type=None, 553 is_extension=False, 554 extension_scope=None, 555 serialized_options=b'\222A\030J\020["id-0", "id-1"]x\350\007\200\001\001\340A\002', 556 file=DESCRIPTOR, 557 create_key=_descriptor._internal_create_key, 558 ), 559 _descriptor.FieldDescriptor( 560 name="namespace", 561 full_name="FetchRequest.namespace", 562 index=1, 563 number=2, 564 type=9, 565 cpp_type=9, 566 label=1, 567 has_default_value=False, 568 default_value=b"".decode("utf-8"), 569 message_type=None, 570 enum_type=None, 571 containing_type=None, 572 is_extension=False, 573 extension_scope=None, 574 serialized_options=b'\222A\025J\023"example-namespace"', 575 file=DESCRIPTOR, 576 create_key=_descriptor._internal_create_key, 577 ), 578 ], 579 extensions=[], 580 nested_types=[], 581 enum_types=[], 582 serialized_options=None, 583 is_extendable=False, 584 syntax="proto3", 585 extension_ranges=[], 586 oneofs=[], 587 serialized_start=1183, 588 serialized_end=1287, 589) 590 591 592_FETCHRESPONSE_VECTORSENTRY = _descriptor.Descriptor( 593 name="VectorsEntry", 594 full_name="FetchResponse.VectorsEntry", 595 filename=None, 596 file=DESCRIPTOR, 597 containing_type=None, 598 create_key=_descriptor._internal_create_key, 599 fields=[ 600 _descriptor.FieldDescriptor( 601 name="key", 602 full_name="FetchResponse.VectorsEntry.key", 603 index=0, 604 number=1, 605 type=9, 606 cpp_type=9, 607 label=1, 608 has_default_value=False, 609 default_value=b"".decode("utf-8"), 610 message_type=None, 611 enum_type=None, 612 containing_type=None, 613 is_extension=False, 614 extension_scope=None, 615 serialized_options=None, 616 file=DESCRIPTOR, 617 create_key=_descriptor._internal_create_key, 618 ), 619 _descriptor.FieldDescriptor( 620 name="value", 621 full_name="FetchResponse.VectorsEntry.value", 622 index=1, 623 number=2, 624 type=11, 625 cpp_type=10, 626 label=1, 627 has_default_value=False, 628 default_value=None, 629 message_type=None, 630 enum_type=None, 631 containing_type=None, 632 is_extension=False, 633 extension_scope=None, 634 serialized_options=None, 635 file=DESCRIPTOR, 636 create_key=_descriptor._internal_create_key, 637 ), 638 ], 639 extensions=[], 640 nested_types=[], 641 enum_types=[], 642 serialized_options=b"8\001", 643 is_extendable=False, 644 syntax="proto3", 645 extension_ranges=[], 646 oneofs=[], 647 serialized_start=1398, 648 serialized_end=1453, 649) 650 651_FETCHRESPONSE = _descriptor.Descriptor( 652 name="FetchResponse", 653 full_name="FetchResponse", 654 filename=None, 655 file=DESCRIPTOR, 656 containing_type=None, 657 create_key=_descriptor._internal_create_key, 658 fields=[ 659 _descriptor.FieldDescriptor( 660 name="vectors", 661 full_name="FetchResponse.vectors", 662 index=0, 663 number=1, 664 type=11, 665 cpp_type=10, 666 label=3, 667 has_default_value=False, 668 default_value=[], 669 message_type=None, 670 enum_type=None, 671 containing_type=None, 672 is_extension=False, 673 extension_scope=None, 674 serialized_options=None, 675 file=DESCRIPTOR, 676 create_key=_descriptor._internal_create_key, 677 ), 678 _descriptor.FieldDescriptor( 679 name="namespace", 680 full_name="FetchResponse.namespace", 681 index=1, 682 number=2, 683 type=9, 684 cpp_type=9, 685 label=1, 686 has_default_value=False, 687 default_value=b"".decode("utf-8"), 688 message_type=None, 689 enum_type=None, 690 containing_type=None, 691 is_extension=False, 692 extension_scope=None, 693 serialized_options=b'\222A\025J\023"example-namespace"', 694 file=DESCRIPTOR, 695 create_key=_descriptor._internal_create_key, 696 ), 697 ], 698 extensions=[], 699 nested_types=[ 700 _FETCHRESPONSE_VECTORSENTRY, 701 ], 702 enum_types=[], 703 serialized_options=None, 704 is_extendable=False, 705 syntax="proto3", 706 extension_ranges=[], 707 oneofs=[], 708 serialized_start=1290, 709 serialized_end=1453, 710) 711 712 713_QUERYVECTOR = _descriptor.Descriptor( 714 name="QueryVector", 715 full_name="QueryVector", 716 filename=None, 717 file=DESCRIPTOR, 718 containing_type=None, 719 create_key=_descriptor._internal_create_key, 720 fields=[ 721 _descriptor.FieldDescriptor( 722 name="values", 723 full_name="QueryVector.values", 724 index=0, 725 number=1, 726 type=2, 727 cpp_type=6, 728 label=3, 729 has_default_value=False, 730 default_value=[], 731 message_type=None, 732 enum_type=None, 733 containing_type=None, 734 is_extension=False, 735 extension_scope=None, 736 serialized_options=b"\222A1J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\240\234\001\200\001\001\340A\002", 737 file=DESCRIPTOR, 738 create_key=_descriptor._internal_create_key, 739 ), 740 _descriptor.FieldDescriptor( 741 name="sparse_values", 742 full_name="QueryVector.sparse_values", 743 index=1, 744 number=5, 745 type=11, 746 cpp_type=10, 747 label=1, 748 has_default_value=False, 749 default_value=None, 750 message_type=None, 751 enum_type=None, 752 containing_type=None, 753 is_extension=False, 754 extension_scope=None, 755 serialized_options=None, 756 file=DESCRIPTOR, 757 create_key=_descriptor._internal_create_key, 758 ), 759 _descriptor.FieldDescriptor( 760 name="top_k", 761 full_name="QueryVector.top_k", 762 index=2, 763 number=2, 764 type=13, 765 cpp_type=3, 766 label=1, 767 has_default_value=False, 768 default_value=0, 769 message_type=None, 770 enum_type=None, 771 containing_type=None, 772 is_extension=False, 773 extension_scope=None, 774 serialized_options=b"\222A\026J\00210Y\000\000\000\000\000\210\303@i\000\000\000\000\000\000\360?", 775 file=DESCRIPTOR, 776 create_key=_descriptor._internal_create_key, 777 ), 778 _descriptor.FieldDescriptor( 779 name="namespace", 780 full_name="QueryVector.namespace", 781 index=3, 782 number=3, 783 type=9, 784 cpp_type=9, 785 label=1, 786 has_default_value=False, 787 default_value=b"".decode("utf-8"), 788 message_type=None, 789 enum_type=None, 790 containing_type=None, 791 is_extension=False, 792 extension_scope=None, 793 serialized_options=b'\222A\025J\023"example-namespace"', 794 file=DESCRIPTOR, 795 create_key=_descriptor._internal_create_key, 796 ), 797 _descriptor.FieldDescriptor( 798 name="filter", 799 full_name="QueryVector.filter", 800 index=4, 801 number=4, 802 type=11, 803 cpp_type=10, 804 label=1, 805 has_default_value=False, 806 default_value=None, 807 message_type=None, 808 enum_type=None, 809 containing_type=None, 810 is_extension=False, 811 extension_scope=None, 812 serialized_options=b'\222AOJM{"genre": {"$in": ["comedy", "documentary", "drama"]}, "year": {"$eq": 2019}}', 813 file=DESCRIPTOR, 814 create_key=_descriptor._internal_create_key, 815 ), 816 ], 817 extensions=[], 818 nested_types=[], 819 enum_types=[], 820 serialized_options=None, 821 is_extendable=False, 822 syntax="proto3", 823 extension_ranges=[], 824 oneofs=[], 825 serialized_start=1456, 826 serialized_end=1792, 827) 828 829 830_QUERYREQUEST = _descriptor.Descriptor( 831 name="QueryRequest", 832 full_name="QueryRequest", 833 filename=None, 834 file=DESCRIPTOR, 835 containing_type=None, 836 create_key=_descriptor._internal_create_key, 837 fields=[ 838 _descriptor.FieldDescriptor( 839 name="namespace", 840 full_name="QueryRequest.namespace", 841 index=0, 842 number=1, 843 type=9, 844 cpp_type=9, 845 label=1, 846 has_default_value=False, 847 default_value=b"".decode("utf-8"), 848 message_type=None, 849 enum_type=None, 850 containing_type=None, 851 is_extension=False, 852 extension_scope=None, 853 serialized_options=b'\222A\025J\023"example-namespace"', 854 file=DESCRIPTOR, 855 create_key=_descriptor._internal_create_key, 856 ), 857 _descriptor.FieldDescriptor( 858 name="top_k", 859 full_name="QueryRequest.top_k", 860 index=1, 861 number=2, 862 type=13, 863 cpp_type=3, 864 label=1, 865 has_default_value=False, 866 default_value=0, 867 message_type=None, 868 enum_type=None, 869 containing_type=None, 870 is_extension=False, 871 extension_scope=None, 872 serialized_options=b"\222A\026J\00210Y\000\000\000\000\000\210\303@i\000\000\000\000\000\000\360?\340A\002", 873 file=DESCRIPTOR, 874 create_key=_descriptor._internal_create_key, 875 ), 876 _descriptor.FieldDescriptor( 877 name="filter", 878 full_name="QueryRequest.filter", 879 index=2, 880 number=3, 881 type=11, 882 cpp_type=10, 883 label=1, 884 has_default_value=False, 885 default_value=None, 886 message_type=None, 887 enum_type=None, 888 containing_type=None, 889 is_extension=False, 890 extension_scope=None, 891 serialized_options=b'\222AOJM{"genre": {"$in": ["comedy", "documentary", "drama"]}, "year": {"$eq": 2019}}', 892 file=DESCRIPTOR, 893 create_key=_descriptor._internal_create_key, 894 ), 895 _descriptor.FieldDescriptor( 896 name="include_values", 897 full_name="QueryRequest.include_values", 898 index=3, 899 number=4, 900 type=8, 901 cpp_type=7, 902 label=1, 903 has_default_value=False, 904 default_value=False, 905 message_type=None, 906 enum_type=None, 907 containing_type=None, 908 is_extension=False, 909 extension_scope=None, 910 serialized_options=b"\222A\r:\005falseJ\004true", 911 file=DESCRIPTOR, 912 create_key=_descriptor._internal_create_key, 913 ), 914 _descriptor.FieldDescriptor( 915 name="include_metadata", 916 full_name="QueryRequest.include_metadata", 917 index=4, 918 number=5, 919 type=8, 920 cpp_type=7, 921 label=1, 922 has_default_value=False, 923 default_value=False, 924 message_type=None, 925 enum_type=None, 926 containing_type=None, 927 is_extension=False, 928 extension_scope=None, 929 serialized_options=b"\222A\r:\005falseJ\004true", 930 file=DESCRIPTOR, 931 create_key=_descriptor._internal_create_key, 932 ), 933 _descriptor.FieldDescriptor( 934 name="queries", 935 full_name="QueryRequest.queries", 936 index=5, 937 number=6, 938 type=11, 939 cpp_type=10, 940 label=3, 941 has_default_value=False, 942 default_value=[], 943 message_type=None, 944 enum_type=None, 945 containing_type=None, 946 is_extension=False, 947 extension_scope=None, 948 serialized_options=b"\030\001\222A\005x\n\200\001\001", 949 file=DESCRIPTOR, 950 create_key=_descriptor._internal_create_key, 951 ), 952 _descriptor.FieldDescriptor( 953 name="vector", 954 full_name="QueryRequest.vector", 955 index=6, 956 number=7, 957 type=2, 958 cpp_type=6, 959 label=3, 960 has_default_value=False, 961 default_value=[], 962 message_type=None, 963 enum_type=None, 964 containing_type=None, 965 is_extension=False, 966 extension_scope=None, 967 serialized_options=b"\222A1J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\240\234\001\200\001\001", 968 file=DESCRIPTOR, 969 create_key=_descriptor._internal_create_key, 970 ), 971 _descriptor.FieldDescriptor( 972 name="sparse_vector", 973 full_name="QueryRequest.sparse_vector", 974 index=7, 975 number=9, 976 type=11, 977 cpp_type=10, 978 label=1, 979 has_default_value=False, 980 default_value=None, 981 message_type=None, 982 enum_type=None, 983 containing_type=None, 984 is_extension=False, 985 extension_scope=None, 986 serialized_options=None, 987 file=DESCRIPTOR, 988 create_key=_descriptor._internal_create_key, 989 ), 990 _descriptor.FieldDescriptor( 991 name="id", 992 full_name="QueryRequest.id", 993 index=8, 994 number=8, 995 type=9, 996 cpp_type=9, 997 label=1, 998 has_default_value=False, 999 default_value=b"".decode("utf-8"), 1000 message_type=None, 1001 enum_type=None, 1002 containing_type=None, 1003 is_extension=False, 1004 extension_scope=None, 1005 serialized_options=b'\222A\027J\022"example-vector-1"x\200\004', 1006 file=DESCRIPTOR, 1007 create_key=_descriptor._internal_create_key, 1008 ), 1009 ], 1010 extensions=[], 1011 nested_types=[], 1012 enum_types=[], 1013 serialized_options=None, 1014 is_extendable=False, 1015 syntax="proto3", 1016 extension_ranges=[], 1017 oneofs=[], 1018 serialized_start=1795, 1019 serialized_end=2301, 1020) 1021 1022 1023_SINGLEQUERYRESULTS = _descriptor.Descriptor( 1024 name="SingleQueryResults", 1025 full_name="SingleQueryResults", 1026 filename=None, 1027 file=DESCRIPTOR, 1028 containing_type=None, 1029 create_key=_descriptor._internal_create_key, 1030 fields=[ 1031 _descriptor.FieldDescriptor( 1032 name="matches", 1033 full_name="SingleQueryResults.matches", 1034 index=0, 1035 number=1, 1036 type=11, 1037 cpp_type=10, 1038 label=3, 1039 has_default_value=False, 1040 default_value=[], 1041 message_type=None, 1042 enum_type=None, 1043 containing_type=None, 1044 is_extension=False, 1045 extension_scope=None, 1046 serialized_options=None, 1047 file=DESCRIPTOR, 1048 create_key=_descriptor._internal_create_key, 1049 ), 1050 _descriptor.FieldDescriptor( 1051 name="namespace", 1052 full_name="SingleQueryResults.namespace", 1053 index=1, 1054 number=2, 1055 type=9, 1056 cpp_type=9, 1057 label=1, 1058 has_default_value=False, 1059 default_value=b"".decode("utf-8"), 1060 message_type=None, 1061 enum_type=None, 1062 containing_type=None, 1063 is_extension=False, 1064 extension_scope=None, 1065 serialized_options=b'\222A\025J\023"example-namespace"', 1066 file=DESCRIPTOR, 1067 create_key=_descriptor._internal_create_key, 1068 ), 1069 ], 1070 extensions=[], 1071 nested_types=[], 1072 enum_types=[], 1073 serialized_options=None, 1074 is_extendable=False, 1075 syntax="proto3", 1076 extension_ranges=[], 1077 oneofs=[], 1078 serialized_start=2303, 1079 serialized_end=2400, 1080) 1081 1082 1083_QUERYRESPONSE = _descriptor.Descriptor( 1084 name="QueryResponse", 1085 full_name="QueryResponse", 1086 filename=None, 1087 file=DESCRIPTOR, 1088 containing_type=None, 1089 create_key=_descriptor._internal_create_key, 1090 fields=[ 1091 _descriptor.FieldDescriptor( 1092 name="results", 1093 full_name="QueryResponse.results", 1094 index=0, 1095 number=1, 1096 type=11, 1097 cpp_type=10, 1098 label=3, 1099 has_default_value=False, 1100 default_value=[], 1101 message_type=None, 1102 enum_type=None, 1103 containing_type=None, 1104 is_extension=False, 1105 extension_scope=None, 1106 serialized_options=b"\030\001", 1107 file=DESCRIPTOR, 1108 create_key=_descriptor._internal_create_key, 1109 ), 1110 _descriptor.FieldDescriptor( 1111 name="matches", 1112 full_name="QueryResponse.matches", 1113 index=1, 1114 number=2, 1115 type=11, 1116 cpp_type=10, 1117 label=3, 1118 has_default_value=False, 1119 default_value=[], 1120 message_type=None, 1121 enum_type=None, 1122 containing_type=None, 1123 is_extension=False, 1124 extension_scope=None, 1125 serialized_options=None, 1126 file=DESCRIPTOR, 1127 create_key=_descriptor._internal_create_key, 1128 ), 1129 _descriptor.FieldDescriptor( 1130 name="namespace", 1131 full_name="QueryResponse.namespace", 1132 index=2, 1133 number=3, 1134 type=9, 1135 cpp_type=9, 1136 label=1, 1137 has_default_value=False, 1138 default_value=b"".decode("utf-8"), 1139 message_type=None, 1140 enum_type=None, 1141 containing_type=None, 1142 is_extension=False, 1143 extension_scope=None, 1144 serialized_options=None, 1145 file=DESCRIPTOR, 1146 create_key=_descriptor._internal_create_key, 1147 ), 1148 ], 1149 extensions=[], 1150 nested_types=[], 1151 enum_types=[], 1152 serialized_options=None, 1153 is_extendable=False, 1154 syntax="proto3", 1155 extension_ranges=[], 1156 oneofs=[], 1157 serialized_start=2402, 1158 serialized_end=2510, 1159) 1160 1161 1162_UPDATEREQUEST = _descriptor.Descriptor( 1163 name="UpdateRequest", 1164 full_name="UpdateRequest", 1165 filename=None, 1166 file=DESCRIPTOR, 1167 containing_type=None, 1168 create_key=_descriptor._internal_create_key, 1169 fields=[ 1170 _descriptor.FieldDescriptor( 1171 name="id", 1172 full_name="UpdateRequest.id", 1173 index=0, 1174 number=1, 1175 type=9, 1176 cpp_type=9, 1177 label=1, 1178 has_default_value=False, 1179 default_value=b"".decode("utf-8"), 1180 message_type=None, 1181 enum_type=None, 1182 containing_type=None, 1183 is_extension=False, 1184 extension_scope=None, 1185 serialized_options=b'\222A\032J\022"example-vector-1"x\200\004\200\001\001\340A\002', 1186 file=DESCRIPTOR, 1187 create_key=_descriptor._internal_create_key, 1188 ), 1189 _descriptor.FieldDescriptor( 1190 name="values", 1191 full_name="UpdateRequest.values", 1192 index=1, 1193 number=2, 1194 type=2, 1195 cpp_type=6, 1196 label=3, 1197 has_default_value=False, 1198 default_value=[], 1199 message_type=None, 1200 enum_type=None, 1201 containing_type=None, 1202 is_extension=False, 1203 extension_scope=None, 1204 serialized_options=b"\222A1J([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]x\240\234\001\200\001\001", 1205 file=DESCRIPTOR, 1206 create_key=_descriptor._internal_create_key, 1207 ), 1208 _descriptor.FieldDescriptor( 1209 name="sparse_values", 1210 full_name="UpdateRequest.sparse_values", 1211 index=2, 1212 number=5, 1213 type=11, 1214 cpp_type=10, 1215 label=1, 1216 has_default_value=False, 1217 default_value=None, 1218 message_type=None, 1219 enum_type=None, 1220 containing_type=None, 1221 is_extension=False, 1222 extension_scope=None, 1223 serialized_options=None, 1224 file=DESCRIPTOR, 1225 create_key=_descriptor._internal_create_key, 1226 ), 1227 _descriptor.FieldDescriptor( 1228 name="set_metadata", 1229 full_name="UpdateRequest.set_metadata", 1230 index=3, 1231 number=3, 1232 type=11, 1233 cpp_type=10, 1234 label=1, 1235 has_default_value=False, 1236 default_value=None, 1237 message_type=None, 1238 enum_type=None, 1239 containing_type=None, 1240 is_extension=False, 1241 extension_scope=None, 1242 serialized_options=b'\222A(J&{"genre": "documentary", "year": 2019}', 1243 file=DESCRIPTOR, 1244 create_key=_descriptor._internal_create_key, 1245 ), 1246 _descriptor.FieldDescriptor( 1247 name="namespace", 1248 full_name="UpdateRequest.namespace", 1249 index=4, 1250 number=4, 1251 type=9, 1252 cpp_type=9, 1253 label=1, 1254 has_default_value=False, 1255 default_value=b"".decode("utf-8"), 1256 message_type=None, 1257 enum_type=None, 1258 containing_type=None, 1259 is_extension=False, 1260 extension_scope=None, 1261 serialized_options=b'\222A\025J\023"example-namespace"', 1262 file=DESCRIPTOR, 1263 create_key=_descriptor._internal_create_key, 1264 ), 1265 ], 1266 extensions=[], 1267 nested_types=[], 1268 enum_types=[], 1269 serialized_options=None, 1270 is_extendable=False, 1271 syntax="proto3", 1272 extension_ranges=[], 1273 oneofs=[], 1274 serialized_start=2513, 1275 serialized_end=2819, 1276) 1277 1278 1279_UPDATERESPONSE = _descriptor.Descriptor( 1280 name="UpdateResponse", 1281 full_name="UpdateResponse", 1282 filename=None, 1283 file=DESCRIPTOR, 1284 containing_type=None, 1285 create_key=_descriptor._internal_create_key, 1286 fields=[], 1287 extensions=[], 1288 nested_types=[], 1289 enum_types=[], 1290 serialized_options=None, 1291 is_extendable=False, 1292 syntax="proto3", 1293 extension_ranges=[], 1294 oneofs=[], 1295 serialized_start=2821, 1296 serialized_end=2837, 1297) 1298 1299 1300_DESCRIBEINDEXSTATSREQUEST = _descriptor.Descriptor( 1301 name="DescribeIndexStatsRequest", 1302 full_name="DescribeIndexStatsRequest", 1303 filename=None, 1304 file=DESCRIPTOR, 1305 containing_type=None, 1306 create_key=_descriptor._internal_create_key, 1307 fields=[ 1308 _descriptor.FieldDescriptor( 1309 name="filter", 1310 full_name="DescribeIndexStatsRequest.filter", 1311 index=0, 1312 number=1, 1313 type=11, 1314 cpp_type=10, 1315 label=1, 1316 has_default_value=False, 1317 default_value=None, 1318 message_type=None, 1319 enum_type=None, 1320 containing_type=None, 1321 is_extension=False, 1322 extension_scope=None, 1323 serialized_options=None, 1324 file=DESCRIPTOR, 1325 create_key=_descriptor._internal_create_key, 1326 ), 1327 ], 1328 extensions=[], 1329 nested_types=[], 1330 enum_types=[], 1331 serialized_options=None, 1332 is_extendable=False, 1333 syntax="proto3", 1334 extension_ranges=[], 1335 oneofs=[], 1336 serialized_start=2839, 1337 serialized_end=2907, 1338) 1339 1340 1341_NAMESPACESUMMARY = _descriptor.Descriptor( 1342 name="NamespaceSummary", 1343 full_name="NamespaceSummary", 1344 filename=None, 1345 file=DESCRIPTOR, 1346 containing_type=None, 1347 create_key=_descriptor._internal_create_key, 1348 fields=[ 1349 _descriptor.FieldDescriptor( 1350 name="vector_count", 1351 full_name="NamespaceSummary.vector_count", 1352 index=0, 1353 number=1, 1354 type=13, 1355 cpp_type=3, 1356 label=1, 1357 has_default_value=False, 1358 default_value=0, 1359 message_type=None, 1360 enum_type=None, 1361 containing_type=None, 1362 is_extension=False, 1363 extension_scope=None, 1364 serialized_options=b"\222A\007J\00550000", 1365 file=DESCRIPTOR, 1366 create_key=_descriptor._internal_create_key, 1367 ), 1368 ], 1369 extensions=[], 1370 nested_types=[], 1371 enum_types=[], 1372 serialized_options=None, 1373 is_extendable=False, 1374 syntax="proto3", 1375 extension_ranges=[], 1376 oneofs=[], 1377 serialized_start=2909, 1378 serialized_end=2961, 1379) 1380 1381 1382_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY = _descriptor.Descriptor( 1383 name="NamespacesEntry", 1384 full_name="DescribeIndexStatsResponse.NamespacesEntry", 1385 filename=None, 1386 file=DESCRIPTOR, 1387 containing_type=None, 1388 create_key=_descriptor._internal_create_key, 1389 fields=[ 1390 _descriptor.FieldDescriptor( 1391 name="key", 1392 full_name="DescribeIndexStatsResponse.NamespacesEntry.key", 1393 index=0, 1394 number=1, 1395 type=9, 1396 cpp_type=9, 1397 label=1, 1398 has_default_value=False, 1399 default_value=b"".decode("utf-8"), 1400 message_type=None, 1401 enum_type=None, 1402 containing_type=None, 1403 is_extension=False, 1404 extension_scope=None, 1405 serialized_options=None, 1406 file=DESCRIPTOR, 1407 create_key=_descriptor._internal_create_key, 1408 ), 1409 _descriptor.FieldDescriptor( 1410 name="value", 1411 full_name="DescribeIndexStatsResponse.NamespacesEntry.value", 1412 index=1, 1413 number=2, 1414 type=11, 1415 cpp_type=10, 1416 label=1, 1417 has_default_value=False, 1418 default_value=None, 1419 message_type=None, 1420 enum_type=None, 1421 containing_type=None, 1422 is_extension=False, 1423 extension_scope=None, 1424 serialized_options=None, 1425 file=DESCRIPTOR, 1426 create_key=_descriptor._internal_create_key, 1427 ), 1428 ], 1429 extensions=[], 1430 nested_types=[], 1431 enum_types=[], 1432 serialized_options=b"8\001", 1433 is_extendable=False, 1434 syntax="proto3", 1435 extension_ranges=[], 1436 oneofs=[], 1437 serialized_start=3163, 1438 serialized_end=3231, 1439) 1440 1441_DESCRIBEINDEXSTATSRESPONSE = _descriptor.Descriptor( 1442 name="DescribeIndexStatsResponse", 1443 full_name="DescribeIndexStatsResponse", 1444 filename=None, 1445 file=DESCRIPTOR, 1446 containing_type=None, 1447 create_key=_descriptor._internal_create_key, 1448 fields=[ 1449 _descriptor.FieldDescriptor( 1450 name="namespaces", 1451 full_name="DescribeIndexStatsResponse.namespaces", 1452 index=0, 1453 number=1, 1454 type=11, 1455 cpp_type=10, 1456 label=3, 1457 has_default_value=False, 1458 default_value=[], 1459 message_type=None, 1460 enum_type=None, 1461 containing_type=None, 1462 is_extension=False, 1463 extension_scope=None, 1464 serialized_options=None, 1465 file=DESCRIPTOR, 1466 create_key=_descriptor._internal_create_key, 1467 ), 1468 _descriptor.FieldDescriptor( 1469 name="dimension", 1470 full_name="DescribeIndexStatsResponse.dimension", 1471 index=1, 1472 number=2, 1473 type=13, 1474 cpp_type=3, 1475 label=1, 1476 has_default_value=False, 1477 default_value=0, 1478 message_type=None, 1479 enum_type=None, 1480 containing_type=None, 1481 is_extension=False, 1482 extension_scope=None, 1483 serialized_options=b"\222A\006J\0041024", 1484 file=DESCRIPTOR, 1485 create_key=_descriptor._internal_create_key, 1486 ), 1487 _descriptor.FieldDescriptor( 1488 name="index_fullness", 1489 full_name="DescribeIndexStatsResponse.index_fullness", 1490 index=2, 1491 number=3, 1492 type=2, 1493 cpp_type=6, 1494 label=1, 1495 has_default_value=False, 1496 default_value=float(0), 1497 message_type=None, 1498 enum_type=None, 1499 containing_type=None, 1500 is_extension=False, 1501 extension_scope=None, 1502 serialized_options=b"\222A\005J\0030.4", 1503 file=DESCRIPTOR, 1504 create_key=_descriptor._internal_create_key, 1505 ), 1506 _descriptor.FieldDescriptor( 1507 name="total_vector_count", 1508 full_name="DescribeIndexStatsResponse.total_vector_count", 1509 index=3, 1510 number=4, 1511 type=13, 1512 cpp_type=3, 1513 label=1, 1514 has_default_value=False, 1515 default_value=0, 1516 message_type=None, 1517 enum_type=None, 1518 containing_type=None, 1519 is_extension=False, 1520 extension_scope=None, 1521 serialized_options=b"\222A\007J\00580000", 1522 file=DESCRIPTOR, 1523 create_key=_descriptor._internal_create_key, 1524 ), 1525 ], 1526 extensions=[], 1527 nested_types=[ 1528 _DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY, 1529 ], 1530 enum_types=[], 1531 serialized_options=b'\222A\210\0012\205\001{"namespaces": {"": {"vectorCount": 50000}, "example-namespace-2": {"vectorCount": 30000}}, "dimension": 1024, "index_fullness": 0.4}', 1532 is_extendable=False, 1533 syntax="proto3", 1534 extension_ranges=[], 1535 oneofs=[], 1536 serialized_start=2964, 1537 serialized_end=3374, 1538) 1539 1540_VECTOR.fields_by_name["sparse_values"].message_type = _SPARSEVALUES 1541_VECTOR.fields_by_name["metadata"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1542_SCOREDVECTOR.fields_by_name["sparse_values"].message_type = _SPARSEVALUES 1543_SCOREDVECTOR.fields_by_name["metadata"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1544_UPSERTREQUEST.fields_by_name["vectors"].message_type = _VECTOR 1545_DELETEREQUEST.fields_by_name["filter"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1546_FETCHRESPONSE_VECTORSENTRY.fields_by_name["value"].message_type = _VECTOR 1547_FETCHRESPONSE_VECTORSENTRY.containing_type = _FETCHRESPONSE 1548_FETCHRESPONSE.fields_by_name["vectors"].message_type = _FETCHRESPONSE_VECTORSENTRY 1549_QUERYVECTOR.fields_by_name["sparse_values"].message_type = _SPARSEVALUES 1550_QUERYVECTOR.fields_by_name["filter"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1551_QUERYREQUEST.fields_by_name["filter"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1552_QUERYREQUEST.fields_by_name["queries"].message_type = _QUERYVECTOR 1553_QUERYREQUEST.fields_by_name["sparse_vector"].message_type = _SPARSEVALUES 1554_SINGLEQUERYRESULTS.fields_by_name["matches"].message_type = _SCOREDVECTOR 1555_QUERYRESPONSE.fields_by_name["results"].message_type = _SINGLEQUERYRESULTS 1556_QUERYRESPONSE.fields_by_name["matches"].message_type = _SCOREDVECTOR 1557_UPDATEREQUEST.fields_by_name["sparse_values"].message_type = _SPARSEVALUES 1558_UPDATEREQUEST.fields_by_name["set_metadata"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1559_DESCRIBEINDEXSTATSREQUEST.fields_by_name["filter"].message_type = google_dot_protobuf_dot_struct__pb2._STRUCT 1560_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY.fields_by_name["value"].message_type = _NAMESPACESUMMARY 1561_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY.containing_type = _DESCRIBEINDEXSTATSRESPONSE 1562_DESCRIBEINDEXSTATSRESPONSE.fields_by_name["namespaces"].message_type = _DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY 1563DESCRIPTOR.message_types_by_name["SparseValues"] = _SPARSEVALUES 1564DESCRIPTOR.message_types_by_name["Vector"] = _VECTOR 1565DESCRIPTOR.message_types_by_name["ScoredVector"] = _SCOREDVECTOR 1566DESCRIPTOR.message_types_by_name["UpsertRequest"] = _UPSERTREQUEST 1567DESCRIPTOR.message_types_by_name["UpsertResponse"] = _UPSERTRESPONSE 1568DESCRIPTOR.message_types_by_name["DeleteRequest"] = _DELETEREQUEST 1569DESCRIPTOR.message_types_by_name["DeleteResponse"] = _DELETERESPONSE 1570DESCRIPTOR.message_types_by_name["FetchRequest"] = _FETCHREQUEST 1571DESCRIPTOR.message_types_by_name["FetchResponse"] = _FETCHRESPONSE 1572DESCRIPTOR.message_types_by_name["QueryVector"] = _QUERYVECTOR 1573DESCRIPTOR.message_types_by_name["QueryRequest"] = _QUERYREQUEST 1574DESCRIPTOR.message_types_by_name["SingleQueryResults"] = _SINGLEQUERYRESULTS 1575DESCRIPTOR.message_types_by_name["QueryResponse"] = _QUERYRESPONSE 1576DESCRIPTOR.message_types_by_name["UpdateRequest"] = _UPDATEREQUEST 1577DESCRIPTOR.message_types_by_name["UpdateResponse"] = _UPDATERESPONSE 1578DESCRIPTOR.message_types_by_name["DescribeIndexStatsRequest"] = _DESCRIBEINDEXSTATSREQUEST 1579DESCRIPTOR.message_types_by_name["NamespaceSummary"] = _NAMESPACESUMMARY 1580DESCRIPTOR.message_types_by_name["DescribeIndexStatsResponse"] = _DESCRIBEINDEXSTATSRESPONSE 1581_sym_db.RegisterFileDescriptor(DESCRIPTOR) 1582 1583SparseValues = _reflection.GeneratedProtocolMessageType( 1584 "SparseValues", 1585 (_message.Message,), 1586 { 1587 "DESCRIPTOR": _SPARSEVALUES, 1588 "__module__": "vector_service_pb2" 1589 # @@protoc_insertion_point(class_scope:SparseValues) 1590 }, 1591) 1592_sym_db.RegisterMessage(SparseValues) 1593 1594Vector = _reflection.GeneratedProtocolMessageType( 1595 "Vector", 1596 (_message.Message,), 1597 { 1598 "DESCRIPTOR": _VECTOR, 1599 "__module__": "vector_service_pb2" 1600 # @@protoc_insertion_point(class_scope:Vector) 1601 }, 1602) 1603_sym_db.RegisterMessage(Vector) 1604 1605ScoredVector = _reflection.GeneratedProtocolMessageType( 1606 "ScoredVector", 1607 (_message.Message,), 1608 { 1609 "DESCRIPTOR": _SCOREDVECTOR, 1610 "__module__": "vector_service_pb2" 1611 # @@protoc_insertion_point(class_scope:ScoredVector) 1612 }, 1613) 1614_sym_db.RegisterMessage(ScoredVector) 1615 1616UpsertRequest = _reflection.GeneratedProtocolMessageType( 1617 "UpsertRequest", 1618 (_message.Message,), 1619 { 1620 "DESCRIPTOR": _UPSERTREQUEST, 1621 "__module__": "vector_service_pb2" 1622 # @@protoc_insertion_point(class_scope:UpsertRequest) 1623 }, 1624) 1625_sym_db.RegisterMessage(UpsertRequest) 1626 1627UpsertResponse = _reflection.GeneratedProtocolMessageType( 1628 "UpsertResponse", 1629 (_message.Message,), 1630 { 1631 "DESCRIPTOR": _UPSERTRESPONSE, 1632 "__module__": "vector_service_pb2" 1633 # @@protoc_insertion_point(class_scope:UpsertResponse) 1634 }, 1635) 1636_sym_db.RegisterMessage(UpsertResponse) 1637 1638DeleteRequest = _reflection.GeneratedProtocolMessageType( 1639 "DeleteRequest", 1640 (_message.Message,), 1641 { 1642 "DESCRIPTOR": _DELETEREQUEST, 1643 "__module__": "vector_service_pb2" 1644 # @@protoc_insertion_point(class_scope:DeleteRequest) 1645 }, 1646) 1647_sym_db.RegisterMessage(DeleteRequest) 1648 1649DeleteResponse = _reflection.GeneratedProtocolMessageType( 1650 "DeleteResponse", 1651 (_message.Message,), 1652 { 1653 "DESCRIPTOR": _DELETERESPONSE, 1654 "__module__": "vector_service_pb2" 1655 # @@protoc_insertion_point(class_scope:DeleteResponse) 1656 }, 1657) 1658_sym_db.RegisterMessage(DeleteResponse) 1659 1660FetchRequest = _reflection.GeneratedProtocolMessageType( 1661 "FetchRequest", 1662 (_message.Message,), 1663 { 1664 "DESCRIPTOR": _FETCHREQUEST, 1665 "__module__": "vector_service_pb2" 1666 # @@protoc_insertion_point(class_scope:FetchRequest) 1667 }, 1668) 1669_sym_db.RegisterMessage(FetchRequest) 1670 1671FetchResponse = _reflection.GeneratedProtocolMessageType( 1672 "FetchResponse", 1673 (_message.Message,), 1674 { 1675 "VectorsEntry": _reflection.GeneratedProtocolMessageType( 1676 "VectorsEntry", 1677 (_message.Message,), 1678 { 1679 "DESCRIPTOR": _FETCHRESPONSE_VECTORSENTRY, 1680 "__module__": "vector_service_pb2" 1681 # @@protoc_insertion_point(class_scope:FetchResponse.VectorsEntry) 1682 }, 1683 ), 1684 "DESCRIPTOR": _FETCHRESPONSE, 1685 "__module__": "vector_service_pb2" 1686 # @@protoc_insertion_point(class_scope:FetchResponse) 1687 }, 1688) 1689_sym_db.RegisterMessage(FetchResponse) 1690_sym_db.RegisterMessage(FetchResponse.VectorsEntry) 1691 1692QueryVector = _reflection.GeneratedProtocolMessageType( 1693 "QueryVector", 1694 (_message.Message,), 1695 { 1696 "DESCRIPTOR": _QUERYVECTOR, 1697 "__module__": "vector_service_pb2" 1698 # @@protoc_insertion_point(class_scope:QueryVector) 1699 }, 1700) 1701_sym_db.RegisterMessage(QueryVector) 1702 1703QueryRequest = _reflection.GeneratedProtocolMessageType( 1704 "QueryRequest", 1705 (_message.Message,), 1706 { 1707 "DESCRIPTOR": _QUERYREQUEST, 1708 "__module__": "vector_service_pb2" 1709 # @@protoc_insertion_point(class_scope:QueryRequest) 1710 }, 1711) 1712_sym_db.RegisterMessage(QueryRequest) 1713 1714SingleQueryResults = _reflection.GeneratedProtocolMessageType( 1715 "SingleQueryResults", 1716 (_message.Message,), 1717 { 1718 "DESCRIPTOR": _SINGLEQUERYRESULTS, 1719 "__module__": "vector_service_pb2" 1720 # @@protoc_insertion_point(class_scope:SingleQueryResults) 1721 }, 1722) 1723_sym_db.RegisterMessage(SingleQueryResults) 1724 1725QueryResponse = _reflection.GeneratedProtocolMessageType( 1726 "QueryResponse", 1727 (_message.Message,), 1728 { 1729 "DESCRIPTOR": _QUERYRESPONSE, 1730 "__module__": "vector_service_pb2" 1731 # @@protoc_insertion_point(class_scope:QueryResponse) 1732 }, 1733) 1734_sym_db.RegisterMessage(QueryResponse) 1735 1736UpdateRequest = _reflection.GeneratedProtocolMessageType( 1737 "UpdateRequest", 1738 (_message.Message,), 1739 { 1740 "DESCRIPTOR": _UPDATEREQUEST, 1741 "__module__": "vector_service_pb2" 1742 # @@protoc_insertion_point(class_scope:UpdateRequest) 1743 }, 1744) 1745_sym_db.RegisterMessage(UpdateRequest) 1746 1747UpdateResponse = _reflection.GeneratedProtocolMessageType( 1748 "UpdateResponse", 1749 (_message.Message,), 1750 { 1751 "DESCRIPTOR": _UPDATERESPONSE, 1752 "__module__": "vector_service_pb2" 1753 # @@protoc_insertion_point(class_scope:UpdateResponse) 1754 }, 1755) 1756_sym_db.RegisterMessage(UpdateResponse) 1757 1758DescribeIndexStatsRequest = _reflection.GeneratedProtocolMessageType( 1759 "DescribeIndexStatsRequest", 1760 (_message.Message,), 1761 { 1762 "DESCRIPTOR": _DESCRIBEINDEXSTATSREQUEST, 1763 "__module__": "vector_service_pb2" 1764 # @@protoc_insertion_point(class_scope:DescribeIndexStatsRequest) 1765 }, 1766) 1767_sym_db.RegisterMessage(DescribeIndexStatsRequest) 1768 1769NamespaceSummary = _reflection.GeneratedProtocolMessageType( 1770 "NamespaceSummary", 1771 (_message.Message,), 1772 { 1773 "DESCRIPTOR": _NAMESPACESUMMARY, 1774 "__module__": "vector_service_pb2" 1775 # @@protoc_insertion_point(class_scope:NamespaceSummary) 1776 }, 1777) 1778_sym_db.RegisterMessage(NamespaceSummary) 1779 1780DescribeIndexStatsResponse = _reflection.GeneratedProtocolMessageType( 1781 "DescribeIndexStatsResponse", 1782 (_message.Message,), 1783 { 1784 "NamespacesEntry": _reflection.GeneratedProtocolMessageType( 1785 "NamespacesEntry", 1786 (_message.Message,), 1787 { 1788 "DESCRIPTOR": _DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY, 1789 "__module__": "vector_service_pb2" 1790 # @@protoc_insertion_point(class_scope:DescribeIndexStatsResponse.NamespacesEntry) 1791 }, 1792 ), 1793 "DESCRIPTOR": _DESCRIBEINDEXSTATSRESPONSE, 1794 "__module__": "vector_service_pb2" 1795 # @@protoc_insertion_point(class_scope:DescribeIndexStatsResponse) 1796 }, 1797) 1798_sym_db.RegisterMessage(DescribeIndexStatsResponse) 1799_sym_db.RegisterMessage(DescribeIndexStatsResponse.NamespacesEntry) 1800 1801 1802DESCRIPTOR._options = None 1803_SPARSEVALUES.fields_by_name["indices"]._options = None 1804_SPARSEVALUES.fields_by_name["values"]._options = None 1805_VECTOR.fields_by_name["id"]._options = None 1806_VECTOR.fields_by_name["values"]._options = None 1807_VECTOR.fields_by_name["metadata"]._options = None 1808_SCOREDVECTOR.fields_by_name["id"]._options = None 1809_SCOREDVECTOR.fields_by_name["score"]._options = None 1810_SCOREDVECTOR.fields_by_name["values"]._options = None 1811_SCOREDVECTOR.fields_by_name["metadata"]._options = None 1812_UPSERTREQUEST.fields_by_name["vectors"]._options = None 1813_UPSERTREQUEST.fields_by_name["namespace"]._options = None 1814_UPSERTRESPONSE.fields_by_name["upserted_count"]._options = None 1815_DELETEREQUEST.fields_by_name["ids"]._options = None 1816_DELETEREQUEST.fields_by_name["delete_all"]._options = None 1817_DELETEREQUEST.fields_by_name["namespace"]._options = None 1818_FETCHREQUEST.fields_by_name["ids"]._options = None 1819_FETCHREQUEST.fields_by_name["namespace"]._options = None 1820_FETCHRESPONSE_VECTORSENTRY._options = None 1821_FETCHRESPONSE.fields_by_name["namespace"]._options = None 1822_QUERYVECTOR.fields_by_name["values"]._options = None 1823_QUERYVECTOR.fields_by_name["top_k"]._options = None 1824_QUERYVECTOR.fields_by_name["namespace"]._options = None 1825_QUERYVECTOR.fields_by_name["filter"]._options = None 1826_QUERYREQUEST.fields_by_name["namespace"]._options = None 1827_QUERYREQUEST.fields_by_name["top_k"]._options = None 1828_QUERYREQUEST.fields_by_name["filter"]._options = None 1829_QUERYREQUEST.fields_by_name["include_values"]._options = None 1830_QUERYREQUEST.fields_by_name["include_metadata"]._options = None 1831_QUERYREQUEST.fields_by_name["queries"]._options = None 1832_QUERYREQUEST.fields_by_name["vector"]._options = None 1833_QUERYREQUEST.fields_by_name["id"]._options = None 1834_SINGLEQUERYRESULTS.fields_by_name["namespace"]._options = None 1835_QUERYRESPONSE.fields_by_name["results"]._options = None 1836_UPDATEREQUEST.fields_by_name["id"]._options = None 1837_UPDATEREQUEST.fields_by_name["values"]._options = None 1838_UPDATEREQUEST.fields_by_name["set_metadata"]._options = None 1839_UPDATEREQUEST.fields_by_name["namespace"]._options = None 1840_NAMESPACESUMMARY.fields_by_name["vector_count"]._options = None 1841_DESCRIBEINDEXSTATSRESPONSE_NAMESPACESENTRY._options = None 1842_DESCRIBEINDEXSTATSRESPONSE.fields_by_name["dimension"]._options = None 1843_DESCRIBEINDEXSTATSRESPONSE.fields_by_name["index_fullness"]._options = None 1844_DESCRIBEINDEXSTATSRESPONSE.fields_by_name["total_vector_count"]._options = None 1845_DESCRIBEINDEXSTATSRESPONSE._options = None 1846 1847_VECTORSERVICE = _descriptor.ServiceDescriptor( 1848 name="VectorService", 1849 full_name="VectorService", 1850 file=DESCRIPTOR, 1851 index=0, 1852 serialized_options=None, 1853 create_key=_descriptor._internal_create_key, 1854 serialized_start=3377, 1855 serialized_end=4078, 1856 methods=[ 1857 _descriptor.MethodDescriptor( 1858 name="Upsert", 1859 full_name="VectorService.Upsert", 1860 index=0, 1861 containing_service=None, 1862 input_type=_UPSERTREQUEST, 1863 output_type=_UPSERTRESPONSE, 1864 serialized_options=b'\202\323\344\223\002\024"\017/vectors/upsert:\001*\222A\033\n\021Vector Operations*\006upsert', 1865 create_key=_descriptor._internal_create_key, 1866 ), 1867 _descriptor.MethodDescriptor( 1868 name="Delete", 1869 full_name="VectorService.Delete", 1870 index=1, 1871 containing_service=None, 1872 input_type=_DELETEREQUEST, 1873 output_type=_DELETERESPONSE, 1874 serialized_options=b"\202\323\344\223\002'\"\017/vectors/delete:\001*Z\021*\017/vectors/delete\222A\033\n\021Vector Operations*\006delete", 1875 create_key=_descriptor._internal_create_key, 1876 ), 1877 _descriptor.MethodDescriptor( 1878 name="Fetch", 1879 full_name="VectorService.Fetch", 1880 index=2, 1881 containing_service=None, 1882 input_type=_FETCHREQUEST, 1883 output_type=_FETCHRESPONSE, 1884 serialized_options=b"\202\323\344\223\002\020\022\016/vectors/fetch\222A\032\n\021Vector Operations*\005fetch", 1885 create_key=_descriptor._internal_create_key, 1886 ), 1887 _descriptor.MethodDescriptor( 1888 name="Query", 1889 full_name="VectorService.Query", 1890 index=3, 1891 containing_service=None, 1892 input_type=_QUERYREQUEST, 1893 output_type=_QUERYRESPONSE, 1894 serialized_options=b'\202\323\344\223\002\013"\006/query:\001*\222A\032\n\021Vector Operations*\005query', 1895 create_key=_descriptor._internal_create_key, 1896 ), 1897 _descriptor.MethodDescriptor( 1898 name="Update", 1899 full_name="VectorService.Update", 1900 index=4, 1901 containing_service=None, 1902 input_type=_UPDATEREQUEST, 1903 output_type=_UPDATERESPONSE, 1904 serialized_options=b'\202\323\344\223\002\024"\017/vectors/update:\001*\222A\033\n\021Vector Operations*\006update', 1905 create_key=_descriptor._internal_create_key, 1906 ), 1907 _descriptor.MethodDescriptor( 1908 name="DescribeIndexStats", 1909 full_name="VectorService.DescribeIndexStats", 1910 index=5, 1911 containing_service=None, 1912 input_type=_DESCRIBEINDEXSTATSREQUEST, 1913 output_type=_DESCRIBEINDEXSTATSRESPONSE, 1914 serialized_options=b'\202\323\344\223\0023"\025/describe_index_stats:\001*Z\027\022\025/describe_index_stats\222A)\n\021Vector Operations*\024describe_index_stats', 1915 create_key=_descriptor._internal_create_key, 1916 ), 1917 ], 1918) 1919_sym_db.RegisterServiceDescriptor(_VECTORSERVICE) 1920 1921DESCRIPTOR.services_by_name["VectorService"] = _VECTORSERVICE 1922 1923# @@protoc_insertion_point(module_scope)
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for vectors.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for vectors.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
Inherited Members
- vector_service_pb2.VectorsEntry
- VectorsEntry
- DESCRIPTOR
- MergeFrom
- Clear
- SetInParent
- IsInitialized
- MergeFromString
- SerializeToString
- SerializePartialToString
- ListFields
- HasField
- ClearField
- WhichOneof
- UnknownFields
- DiscardUnknownFields
- ByteSize
- FromString
- RegisterExtension
- KEY_FIELD_NUMBER
- key
- VALUE_FIELD_NUMBER
- value
- FindInitializationErrors
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for queries.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for matches.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for results.
Getter for matches.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
499 def init(self, **kwargs): 500 self._cached_byte_size = 0 501 self._cached_byte_size_dirty = len(kwargs) > 0 502 self._fields = {} 503 # Contains a mapping from oneof field descriptors to the descriptor 504 # of the currently set field in that oneof field. 505 self._oneofs = {} 506 507 # _unknown_fields is () when empty for efficiency, and will be turned into 508 # a list if fields are added. 509 self._unknown_fields = () 510 # _unknown_field_set is None when empty for efficiency, and will be 511 # turned into UnknownFieldSet struct if fields are added. 512 self._unknown_field_set = None # pylint: disable=protected-access 513 self._is_present_in_parent = False 514 self._listener = message_listener_mod.NullMessageListener() 515 self._listener_for_children = _Listener(self) 516 for field_name, field_value in kwargs.items(): 517 field = _GetFieldByName(message_descriptor, field_name) 518 if field is None: 519 raise TypeError('%s() got an unexpected keyword argument "%s"' % 520 (message_descriptor.name, field_name)) 521 if field_value is None: 522 # field=None is the same as no field at all. 523 continue 524 if field.label == _FieldDescriptor.LABEL_REPEATED: 525 copy = field._default_constructor(self) 526 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 527 if _IsMapField(field): 528 if _IsMessageMapField(field): 529 for key in field_value: 530 copy[key].MergeFrom(field_value[key]) 531 else: 532 copy.update(field_value) 533 else: 534 for val in field_value: 535 if isinstance(val, dict): 536 copy.add(**val) 537 else: 538 copy.add().MergeFrom(val) 539 else: # Scalar 540 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 541 field_value = [_GetIntegerEnumValue(field.enum_type, val) 542 for val in field_value] 543 copy.extend(field_value) 544 self._fields[field] = copy 545 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 546 copy = field._default_constructor(self) 547 new_val = field_value 548 if isinstance(field_value, dict): 549 new_val = field.message_type._concrete_class(**field_value) 550 try: 551 copy.MergeFrom(new_val) 552 except TypeError: 553 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 554 self._fields[field] = copy 555 else: 556 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 557 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 558 try: 559 setattr(self, field_name, field_value) 560 except TypeError: 561 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1310 def MergeFrom(self, msg): 1311 if not isinstance(msg, cls): 1312 raise TypeError( 1313 'Parameter to MergeFrom() must be instance of same class: ' 1314 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1315 _FullyQualifiedClassName(msg.__class__))) 1316 1317 assert msg is not self 1318 self._Modified() 1319 1320 fields = self._fields 1321 1322 for field, value in msg._fields.items(): 1323 if field.label == LABEL_REPEATED: 1324 field_value = fields.get(field) 1325 if field_value is None: 1326 # Construct a new object to represent this field. 1327 field_value = field._default_constructor(self) 1328 fields[field] = field_value 1329 field_value.MergeFrom(value) 1330 elif field.cpp_type == CPPTYPE_MESSAGE: 1331 if value._is_present_in_parent: 1332 field_value = fields.get(field) 1333 if field_value is None: 1334 # Construct a new object to represent this field. 1335 field_value = field._default_constructor(self) 1336 fields[field] = field_value 1337 field_value.MergeFrom(value) 1338 else: 1339 self._fields[field] = value 1340 if field.containing_oneof: 1341 self._UpdateOneofState(field) 1342 1343 if msg._unknown_fields: 1344 if not self._unknown_fields: 1345 self._unknown_fields = [] 1346 self._unknown_fields.extend(msg._unknown_fields) 1347 # pylint: disable=protected-access 1348 if self._unknown_field_set is None: 1349 self._unknown_field_set = containers.UnknownFieldSet() 1350 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1373def _Clear(self): 1374 # Clear fields. 1375 self._fields = {} 1376 self._unknown_fields = () 1377 # pylint: disable=protected-access 1378 if self._unknown_field_set is not None: 1379 self._unknown_field_set._clear() 1380 self._unknown_field_set = None 1381 1382 self._oneofs = {} 1383 self._Modified()
Clears all data that was set in the message.
1445 def Modified(self): 1446 """Sets the _cached_byte_size_dirty bit to true, 1447 and propagates this to our listener iff this was a state change. 1448 """ 1449 1450 # Note: Some callers check _cached_byte_size_dirty before calling 1451 # _Modified() as an extra optimization. So, if this method is ever 1452 # changed such that it does stuff even when _cached_byte_size_dirty is 1453 # already true, the callers need to be updated. 1454 if not self._cached_byte_size_dirty: 1455 self._cached_byte_size_dirty = True 1456 self._listener_for_children.dirty = True 1457 self._is_present_in_parent = True 1458 self._listener.Modified()
Mark this as present in the parent.
This normally happens automatically when you assign a field of a sub-message, but sometimes you want to make the sub-message present while keeping it empty. If you find yourself using this, you may want to reconsider your design.
1210 def IsInitialized(self, errors=None): 1211 """Checks if all required fields of a message are set. 1212 1213 Args: 1214 errors: A list which, if provided, will be populated with the field 1215 paths of all missing required fields. 1216 1217 Returns: 1218 True iff the specified message has all required fields set. 1219 """ 1220 1221 # Performance is critical so we avoid HasField() and ListFields(). 1222 1223 for field in required_fields: 1224 if (field not in self._fields or 1225 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1226 not self._fields[field]._is_present_in_parent)): 1227 if errors is not None: 1228 errors.extend(self.FindInitializationErrors()) 1229 return False 1230 1231 for field, value in list(self._fields.items()): # dict can change size! 1232 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1233 if field.label == _FieldDescriptor.LABEL_REPEATED: 1234 if (field.message_type.has_options and 1235 field.message_type.GetOptions().map_entry): 1236 continue 1237 for element in value: 1238 if not element.IsInitialized(): 1239 if errors is not None: 1240 errors.extend(self.FindInitializationErrors()) 1241 return False 1242 elif value._is_present_in_parent and not value.IsInitialized(): 1243 if errors is not None: 1244 errors.extend(self.FindInitializationErrors()) 1245 return False 1246 1247 return True
Checks if the message is initialized.
Returns:
bool: The method returns True if the message is initialized (i.e. all of its required fields are set).
1125 def MergeFromString(self, serialized): 1126 serialized = memoryview(serialized) 1127 length = len(serialized) 1128 try: 1129 if self._InternalParse(serialized, 0, length) != length: 1130 # The only reason _InternalParse would return early is if it 1131 # encountered an end-group tag. 1132 raise message_mod.DecodeError('Unexpected end-group tag.') 1133 except (IndexError, TypeError): 1134 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1135 raise message_mod.DecodeError('Truncated message.') 1136 except struct.error as e: 1137 raise message_mod.DecodeError(e) 1138 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1081 def SerializeToString(self, **kwargs): 1082 # Check if the message has all of its required fields set. 1083 if not self.IsInitialized(): 1084 raise message_mod.EncodeError( 1085 'Message %s is missing required fields: %s' % ( 1086 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1087 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1094 def SerializePartialToString(self, **kwargs): 1095 out = BytesIO() 1096 self._InternalSerialize(out.write, **kwargs) 1097 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
822 def ListFields(self): 823 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 824 all_fields.sort(key = lambda item: item[0].number) 825 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
854 def HasField(self, field_name): 855 try: 856 field = hassable_fields[field_name] 857 except KeyError: 858 raise ValueError(error_msg % (message_descriptor.full_name, field_name)) 859 860 if isinstance(field, descriptor_mod.OneofDescriptor): 861 try: 862 return HasField(self, self._oneofs[field].name) 863 except KeyError: 864 return False 865 else: 866 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 867 value = self._fields.get(field) 868 return value is not None and value._is_present_in_parent 869 else: 870 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
877 def ClearField(self, field_name): 878 try: 879 field = message_descriptor.fields_by_name[field_name] 880 except KeyError: 881 try: 882 field = message_descriptor.oneofs_by_name[field_name] 883 if field in self._oneofs: 884 field = self._oneofs[field] 885 else: 886 return 887 except KeyError: 888 raise ValueError('Protocol message %s has no "%s" field.' % 889 (message_descriptor.name, field_name)) 890 891 if field in self._fields: 892 # To match the C++ implementation, we need to invalidate iterators 893 # for map fields when ClearField() happens. 894 if hasattr(self._fields[field], 'InvalidateIterators'): 895 self._fields[field].InvalidateIterators() 896 897 # Note: If the field is a sub-message, its listener will still point 898 # at us. That's fine, because the worst than can happen is that it 899 # will call _Modified() and invalidate our byte size. Big deal. 900 del self._fields[field] 901 902 if self._oneofs.get(field.containing_oneof, None) is field: 903 del self._oneofs[field.containing_oneof] 904 905 # Always call _Modified() -- even if nothing was changed, this is 906 # a mutating method, and thus calling it should cause the field to become 907 # present in the parent message. 908 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1356 def WhichOneof(self, oneof_name): 1357 """Returns the name of the currently set field inside a oneof, or None.""" 1358 try: 1359 field = message_descriptor.oneofs_by_name[oneof_name] 1360 except KeyError: 1361 raise ValueError( 1362 'Protocol message has no oneof "%s" field.' % oneof_name) 1363 1364 nested_field = self._oneofs.get(field, None) 1365 if nested_field is not None and self.HasField(nested_field.name): 1366 return nested_field.name 1367 else: 1368 return None
Returns the name of the field that is set inside a oneof group.
If no field is set, returns None.
Arguments:
- oneof_group (str): the name of the oneof group to check.
Returns:
str or None: The name of the group that is set, or None.
Raises:
- ValueError: no group with the given name exists
1386def _UnknownFields(self): 1387 if self._unknown_field_set is None: # pylint: disable=protected-access 1388 # pylint: disable=protected-access 1389 self._unknown_field_set = containers.UnknownFieldSet() 1390 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1393def _DiscardUnknownFields(self): 1394 self._unknown_fields = [] 1395 self._unknown_field_set = None # pylint: disable=protected-access 1396 for field, value in self.ListFields(): 1397 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1398 if _IsMapField(field): 1399 if _IsMessageMapField(field): 1400 for key in value: 1401 value[key].DiscardUnknownFields() 1402 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1403 for sub_message in value: 1404 sub_message.DiscardUnknownFields() 1405 else: 1406 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1054 def ByteSize(self): 1055 if not self._cached_byte_size_dirty: 1056 return self._cached_byte_size 1057 1058 size = 0 1059 descriptor = self.DESCRIPTOR 1060 if descriptor.GetOptions().map_entry: 1061 # Fields of map entry should always be serialized. 1062 size = descriptor.fields_by_name['key']._sizer(self.key) 1063 size += descriptor.fields_by_name['value']._sizer(self.value) 1064 else: 1065 for field_descriptor, field_value in self.ListFields(): 1066 size += field_descriptor._sizer(field_value) 1067 for tag_bytes, value_bytes in self._unknown_fields: 1068 size += len(tag_bytes) + len(value_bytes) 1069 1070 self._cached_byte_size = size 1071 self._cached_byte_size_dirty = False 1072 self._listener_for_children.dirty = False 1073 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
792 def RegisterExtension(extension_handle): 793 extension_handle.containing_type = cls.DESCRIPTOR 794 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 795 # pylint: disable=protected-access 796 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(extension_handle) 797 _AttachFieldHelpers(cls, extension_handle)
Getter for namespaces.
1251 def FindInitializationErrors(self): 1252 """Finds required fields which are not initialized. 1253 1254 Returns: 1255 A list of strings. Each string is a path to an uninitialized field from 1256 the top-level message, e.g. "foo.bar[5].baz". 1257 """ 1258 1259 errors = [] # simplify things 1260 1261 for field in required_fields: 1262 if not self.HasField(field.name): 1263 errors.append(field.name) 1264 1265 for field, value in self.ListFields(): 1266 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1267 if field.is_extension: 1268 name = '(%s)' % field.full_name 1269 else: 1270 name = field.name 1271 1272 if _IsMapField(field): 1273 if _IsMessageMapField(field): 1274 for key in value: 1275 element = value[key] 1276 prefix = '%s[%s].' % (name, key) 1277 sub_errors = element.FindInitializationErrors() 1278 errors += [prefix + error for error in sub_errors] 1279 else: 1280 # ScalarMaps can't have any initialization errors. 1281 pass 1282 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1283 for i in range(len(value)): 1284 element = value[i] 1285 prefix = '%s[%d].' % (name, i) 1286 sub_errors = element.FindInitializationErrors() 1287 errors += [prefix + error for error in sub_errors] 1288 else: 1289 prefix = name + '.' 1290 sub_errors = value.FindInitializationErrors() 1291 errors += [prefix + error for error in sub_errors] 1292 1293 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
Inherited Members
- vector_service_pb2.NamespacesEntry
- NamespacesEntry
- DESCRIPTOR
- MergeFrom
- Clear
- SetInParent
- IsInitialized
- MergeFromString
- SerializeToString
- SerializePartialToString
- ListFields
- HasField
- ClearField
- WhichOneof
- UnknownFields
- DiscardUnknownFields
- ByteSize
- FromString
- RegisterExtension
- KEY_FIELD_NUMBER
- key
- VALUE_FIELD_NUMBER
- value
- FindInitializationErrors
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension