From 648195c6eb91d53a49c02986b18a6c451252e795 Mon Sep 17 00:00:00 2001 From: fredericboisguerin-tagheuer <97434366+fredericboisguerin-tagheuer@users.noreply.github.com> Date: Fri, 19 Aug 2022 11:23:37 +0200 Subject: [PATCH] Fix "Key Error: 11" for dict mappings See https://bytemeta.vip/repo/Ed-XCF/protobuf2pydantic/issues/2 --- protobuf2pydantic/biz.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/protobuf2pydantic/biz.py b/protobuf2pydantic/biz.py index 1e410ac..ca6e05b 100644 --- a/protobuf2pydantic/biz.py +++ b/protobuf2pydantic/biz.py @@ -34,6 +34,7 @@ def convert_field(level: int, field: FieldDescriptor) -> str: level += 1 field_type = field.type field_label = field.label + was_mapping = False extra = None if field_type == FieldDescriptor.TYPE_ENUM: @@ -46,11 +47,16 @@ def convert_field(level: int, field: FieldDescriptor) -> str: ) extra = linesep.join([class_statement, *field_statements]) factory = "int" + elif field_type == FieldDescriptor.TYPE_MESSAGE: type_statement: str = field.message_type.name if type_statement.endswith("Entry"): key, value = field.message_type.fields # type: FieldDescriptor - type_statement = f"Dict[{m(key)}, {m(value)}]" + if value.type != 11: + type_statement = f"Dict[{m(key)}, {m(value)}]" + else: + was_mapping = True + type_statement = f"Dict[{m(key)}, {value.message_type.name}]" factory = "dict" elif type_statement == "Struct": type_statement = "Dict[str, Any]" @@ -62,7 +68,7 @@ def convert_field(level: int, field: FieldDescriptor) -> str: type_statement = m(field) factory = type_statement - if field_label == FieldDescriptor.LABEL_REPEATED: + if field_label == FieldDescriptor.LABEL_REPEATED and not was_mapping: type_statement = f"List[{type_statement}]" factory = "list"