@@ -562,7 +562,15 @@ def _generate_NewMessage(
562562 # option_var is the original Option variable name for checking .Valid
563563 oneof_groups : dict [str , list [tuple [str , str , str ]]] = {}
564564
565- from .target import OptionType , Var
565+ from .target import Builtin , OptionType , Var
566+
567+ def _go_optional_string_unwrap_needs_ptr (field_expr : TargetExpr ) -> bool :
568+ """unwrap_option_or yields a plain string; optional proto fields need *string."""
569+ return (
570+ isinstance (field_expr , Call )
571+ and isinstance (field_expr .func , Builtin )
572+ and field_expr .func .name == "unwrap_option_or"
573+ )
566574
567575 def unwrap_if_option (field_expr , field_value : str ) -> tuple [str , str ]:
568576 """Unwrap Option type values for struct field assignment.
@@ -654,6 +662,22 @@ def unwrap_if_option(field_expr, field_value: str) -> tuple[str, str]:
654662 assert field_value is not None
655663 pascal_field = to_pascal_case (field_name )
656664 _ , field_value = unwrap_if_option (field_expr , field_value )
665+
666+ proto_msg = self .proto_messages .get ((expr .module , expr .name ))
667+ if proto_msg is not None :
668+ pf = next (
669+ (f for f in proto_msg .fields if f .name == field_name ),
670+ None ,
671+ )
672+ if (
673+ pf is not None
674+ and pf .is_optional
675+ and not pf .is_repeated
676+ and pf .type == "string"
677+ and _go_optional_string_unwrap_needs_ptr (field_expr )
678+ ):
679+ field_value = f"ptr({ field_value } )"
680+
657681 regular_assignments .append (f"{ pascal_field } : { field_value } " )
658682
659683 # Generate struct literal with regular fields only
@@ -920,8 +944,9 @@ def _generate_Assign(self, expr, lines: list[str], indent: str) -> str:
920944 ):
921945 var_type = self .gen_type (expr .var .type )
922946 if var_type in ("int64" , "int32" , "uint32" , "uint64" ):
923- lines .append (f"{ indent } { var_name } := { var_type } ({ expr .expr .value } )" )
924- self .mark_declared (var_name )
947+ lines .append (
948+ f"{ indent } { self .gen_assignment (var_name , f'{ var_type } ({ expr .expr .value } )' )} "
949+ )
925950 return self .gen_none ()
926951
927952 # Regular assignment
0 commit comments