go data_error ai_generated true

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

ID: go/proto-oneof-set-conflict

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-01-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf 1.30+ active — — —

Root Cause

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

中文

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

Workarounds

  1. 95% success
    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% success
    Validate with proto.CheckInitialized and inspect the active variant before sending:
    if msg.GetText() != "" && msg.GetBlob() != nil {
        return status.Error(codes.InvalidArgument, "oneof conflict")
    }

Dead Ends

Common approaches that don't work:

  1. 85% fail

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

  2. 90% fail

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