@@ -373,6 +373,7 @@ def proto_name(self) -> str:
373
373
374
374
@property
375
375
def is_wrapped (self ) -> bool :
376
+ assert self .field_type == FieldDescriptorProtoType .TYPE_MESSAGE
376
377
type_package , type_name = parse_source_type_name (self .proto_obj .type_name , self .output_file .parent_request )
377
378
378
379
return (type_package , type_name ) in WRAPPED_TYPES
@@ -441,6 +442,8 @@ def betterproto_field_args(self) -> list[str]:
441
442
class MapEntryCompiler (FieldCompiler ):
442
443
py_k_type : str = ""
443
444
py_v_type : str = ""
445
+ unwrap_v : str | None = None
446
+
444
447
proto_k_type : str = ""
445
448
proto_v_type : str = ""
446
449
@@ -450,18 +453,30 @@ def ready(self) -> None:
450
453
for nested in self .message .proto_obj .nested_type :
451
454
if nested .name .replace ("_" , "" ).lower () == map_entry and unwrap (nested .options ).map_entry :
452
455
# Get Python types
456
+ assert nested .field [0 ].name == "key"
453
457
self .py_k_type = FieldCompiler (
454
458
source_file = self .source_file ,
455
459
proto_obj = nested .field [0 ], # key
456
460
path = [],
457
461
message = self .message ,
458
462
).py_type
459
- self .py_v_type = FieldCompiler (
463
+
464
+ assert nested .field [1 ].name == "value"
465
+ value_field_compiler = FieldCompiler (
460
466
source_file = self .source_file ,
461
467
proto_obj = nested .field [1 ], # value
462
468
path = [],
463
469
message = self .message ,
464
- ).py_type
470
+ )
471
+
472
+ self .py_v_type = value_field_compiler .py_type
473
+ if (
474
+ value_field_compiler .field_type == FieldDescriptorProtoType .TYPE_MESSAGE
475
+ and value_field_compiler .is_wrapped
476
+ ):
477
+ self .unwrap_v = value_field_compiler .unwrapped_py_type
478
+ else :
479
+ self .unwrap_v = None
465
480
466
481
# Get proto types
467
482
self .proto_k_type = unwrap (FieldDescriptorProtoType (nested .field [0 ].type ).name )
@@ -472,11 +487,19 @@ def ready(self) -> None:
472
487
473
488
def get_field_string (self ) -> str :
474
489
"""Construct string representation of this field as a field."""
490
+ proto_type_1 = f"betterproto2.{ self .proto_k_type } "
491
+ proto_type_2 = f"betterproto2.{ self .proto_v_type } "
492
+
493
+ unwrap_2 = ""
494
+ if self .unwrap_v :
495
+ unwrap_2 = f", unwrap_2=lambda: { self .unwrap_v } "
496
+
475
497
betterproto_field_type = (
476
- f"betterproto2.field({ self .proto_obj .number } , "
498
+ "betterproto2.field("
499
+ f"{ self .proto_obj .number } , "
477
500
"betterproto2.TYPE_MAP, "
478
- f"map_types=( betterproto2.{ self . proto_k_type } , "
479
- f"betterproto2. { self . proto_v_type } ) )"
501
+ f"map_meta= betterproto2.map_meta( { proto_type_1 } , { proto_type_2 } { unwrap_2 } ) "
502
+ " )"
480
503
)
481
504
if self .py_name in dir (builtins ):
482
505
self .message .builtins_types .add (self .py_name )
0 commit comments