Closed honzap closed 7 months ago
protoreflect.Message.Get
behaves mostly equivalently to accessor methods, which return the default value for unpopulated scalar fields. For example, in your case foo.GetField()
returns ""
if the field field
is unset.
You can test to see if a field is set with foo.ProtoReflect().Has(paramField)
.
The protoreflect
package operates on the protobuf data model, not the Go data model, so it doesn't have a concept of a field being nil
. (For example, in generated messages, the representation of an unset optional string field is (*string)(nil)
, but a dynamicpb.Message
uses a different representation. protoreflect
can work with either.)
Thanks for explanation. So my line of code getting to know isNil
is basically correct, right?
For me, the curious thing is that protoreflect.Value
inside works with nilType
but basically, this can never happen, right?
The internal implementation of protoreflect.Value
may need to consider things like a value being nil
as it is sort of the “glue” between the protobuf data model, and the Go data model. But it only surfaces strictly the protobuf data model, and the internal Go data model is strictly an obscured implementation detail.
Thank you for clarification. I'm closing this topic now.
Hello,
I have more over a question about the behavior of
protoreflect.Value
foroptional
fields in proto message.Let's have a simple message:
This one generates Go code struct like this:
The
Param
here is nillable as expected.When I try to read the value of that field via
protoreflect
, I'm basically not able to get the fact that theParam
isnil
. I'd expect thatValue.IsValid()
returnsfalse
because it can operate withnilType
.Let's have this test snippet:
I'd expect that this test will pass but
IsValid
returns true and callingValue.Interface()
returns empty string""
(probably a default value of that field).My goal is to know that the value is
nil
for the fieldParam
. I achieved the result by this code, but I'm not sure it's the correct one:Could you please explain me this behavior and why there is
nilType
not used in this case?Thanks a lot.
Best,
Honza