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
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.
解决方案
-
95% 成功率
Use the generated oneof wrapper types explicitly: cfg := &pb.Config{} cfg.Value = &pb.Config_Timeout{Timeout: 5 * time.Second} -
85% 成功率
Check which variant is set before assigning: if _, ok := cfg.Value.(*pb.Config_Timeout); !ok { cfg.Value = &pb.Config_Timeout{Timeout: d} }
无效尝试
常见但无效的做法:
-
70% 失败
proto.Reset clears all fields, losing unrelated data you already populated.
-
80% 失败
The panic leaves the message in an inconsistent state; subsequent marshaling may produce corrupt output.