Closed finelog closed 1 year ago
This is happening because you’re specifying that the oneof
has a type of IntVal
, even if you’re not setting the IntVal value, and a oneof
can only have a type, if a value is actually specified. If you think about it, how could it tell from the protowire
format that there was an IntVal
type in the oneof
? The only way to represent this situation is with a zero-value IntVal
.
Compare the output of your code to this:
package main
import (
"fmt"
proto "github.com/puellanivis/clone-proto/proto"
)
func main() {
mixVal := &proto.TestClone{
ValueOneof: &proto.TestClone_IntVal{},
}
fmt.Printf("%#v, %#v\n", mixVal.GetIntVal().GetKey(), mixVal.GetIntVal().GetVal())
mixVal = proto.Clone(mixVal).(*proto.TestClone)
fmt.Printf("%#v, %#v\n", mixVal.GetIntVal().GetKey(), mixVal.GetIntVal().GetVal())
}
I think I can see the point, beause my view is based on the go type system, but in the protobuf's view, such behavir is expected, I will close this then. thanks for the explanation! @puellanivis
I'm not sure if this is a bug, and I don't see others have reported this An unexpected behavir occur to me when I was using the
proto.Clone
method with oneof field, if an oneof field is nil, afterproto.Clone
, the field in new message is not nil this is kind weird... take me sometime found out while debug my progroam this morning.anyway, I have put the code to reproduce behavir described above, please check it out, thanks!
What version of protobuf and what language are you using? Version: v1.28.0
What did you do? pbhelper.proto:
pbhelper.go
main.go
What did you expect to see?
What did you see instead?
Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).
Anything else we should know about your project / environment?
protoc version: v3.6.1
go env