go data_error ai_generated true

proto:字段 "pb.Config.Timeout" 已在 oneof "pb.Config.value" 中设置

proto: field "pb.Config.Timeout" is already set in oneof "pb.Config.value"

ID: go/proto-oneof-set-twice

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2024-09-11首次发现

版本兼容性

版本状态引入弃用备注
google.golang.org/protobuf 1.28+ active

根因分析

同一消息上的 oneof 字段被赋值两次。在 google.golang.org/protobuf 中,设置 oneof 字段会清除其他字段,但通过反射对同一字段设置两次可能在某些生成代码路径中触发此错误。

English

A oneof field was assigned twice on the same message. In google.golang.org/protobuf, setting a oneof field clears the others but setting the same field via reflection twice can trigger this in some generated code paths.

generic

解决方案

  1. 95% 成功率
    Use the generated oneof wrapper types explicitly:
    cfg := &pb.Config{}
    cfg.Value = &pb.Config_Timeout{Timeout: 5 * time.Second}
  2. 85% 成功率
    Check which variant is set before assigning:
    if _, ok := cfg.Value.(*pb.Config_Timeout); !ok {
        cfg.Value = &pb.Config_Timeout{Timeout: d}
    }

无效尝试

常见但无效的做法:

  1. 70% 失败

    proto.Reset clears all fields, losing unrelated data you already populated.

  2. 80% 失败

    The panic leaves the message in an inconsistent state; subsequent marshaling may produce corrupt output.