go data_error ai_generated true

proto:oneof 字段 "pb.Event.payload" 已使用不同类型设置

proto: oneof field "pb.Event.payload" is already set with a different type

ID: go/proto-oneof-set-conflict

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2025-01-08首次发现

版本兼容性

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

根因分析

代码尝试在同一消息上设置两个不同的 oneof 变体。oneof 一次只能持有一个字段;设置新变体会清除前一个。

English

Code attempted to set two different oneof variants on the same message. A oneof can only hold one field at a time; setting a new variant clears the previous one.

generic

解决方案

  1. 95% 成功率
    Use the generated oneof wrapper types and set exactly one variant:
    switch kind {
    case "text":
        msg.Payload = &pb.Event_Text{Text: value}
    case "blob":
        msg.Payload = &pb.Event_Blob{Blob: value}
    }
  2. 90% 成功率
    Validate with proto.CheckInitialized and inspect the active variant before sending:
    if msg.GetText() != "" && msg.GetBlob() != nil {
        return status.Error(codes.InvalidArgument, "oneof conflict")
    }

无效尝试

常见但无效的做法:

  1. 85% 失败

    The oneof semantics still apply to the copy; only the last-set variant survives, silently dropping the first.

  2. 90% 失败

    The message is in an inconsistent state and the wire encoding contains only one variant, causing downstream logic errors.