Documentation
¶
Overview ¶
Package protomessage contains helpers for working with proto.Message values from generic and/or dynamic code.
This includes converting a message to a particular concrete type (like converting a dynamic message to a generated type), re-parsing unrecognized fields once the extensions they represent are known, and walking a message to visit all of the messages nested inside it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
func As[M PointerMessage[T], T any](msg proto.Message) (M, error)
As returns the given message as type M. If the given message is type M, it is returned as-is. Otherwise (like if the given message is a dynamic message), it will be marshalled to bytes and then unmarshalled into a value of type M. If M and msg do not share the same message type (e.g. same fully qualified message name), an error is returned.
func ReparseUnrecognized ¶
func ReparseUnrecognized(msg proto.Message, resolver protoresolve.SerializationResolver) bool
ReparseUnrecognized is a helper function for re-parsing unknown fields of a message, resolving any extensions therein using the given resolver. This is particularly useful for unmarshalling FileDescriptorProto and FileDescriptorSet messages. With these messages, custom options may not be statically known by the unmarshalling program, but would be defined in the descriptor protos. So when initially unmarshalling, custom options would be left unrecognized. After unmarshalling, the resulting descriptor protos can be used to create a resolver (like using protoresolve.FromFileDescriptorSet). That resolver can in turn be supplied to this function, to re-parse the descriptor protos, thereby recognizing and interpreting custom options therein.
func Walk ¶
func Walk(root protoreflect.Message, action func(path []any, val protoreflect.Message) bool)
Walk traverses the given root messages, iterating through its fields and through all values in maps and lists, calling the given action for all message values encountered. The given action is called for root first before being called for any contained message values.
The path provided to the callback is the sequence of field numbers, list indices, and map keys that identifies the location of the given message. It is empty when called for the root message. The types of values in the slice will be protoreflect.FieldNumber, int (an index into a list field), or protoreflect.MapKey (indicating which entry in a map field).
The callback must not retain the path slice, since it is re-used for subsequent calls. It must make a copy if it needs to retain it.
If the callback returns false, the traversal is terminated and the callback will not be invoked again.
Types ¶
type PointerMessage ¶
PointerMessage is a pointer type that implements proto.Message.