MCPcopy
hub / github.com/golang/protobuf / writeProto3Any

Method writeProto3Any

proto/text_encode.go:221–259  ·  view source on GitHub ↗

writeProto3Any writes an expanded google.protobuf.Any message. It returns (false, nil) if sv value can't be unmarshaled (e.g. because required messages are not linked in). It returns (true, error) when sv was written in expanded format or an error was encountered.

(m protoreflect.Message)

Source from the content-addressed store, hash-verified

219// It returns (true, error) when sv was written in expanded format or an error
220// was encountered.
221func (w *textWriter) writeProto3Any(m protoreflect.Message) (bool, error) {
222 md := m.Descriptor()
223 fdURL := md.Fields().ByName("type_url")
224 fdVal := md.Fields().ByName("value")
225
226 url := m.Get(fdURL).String()
227 mt, err := protoregistry.GlobalTypes.FindMessageByURL(url)
228 if err != nil {
229 return false, nil
230 }
231
232 b := m.Get(fdVal).Bytes()
233 m2 := mt.New()
234 if err := proto.Unmarshal(b, m2.Interface()); err != nil {
235 return false, nil
236 }
237 w.Write([]byte("["))
238 if requiresQuotes(url) {
239 w.writeQuotedString(url)
240 } else {
241 w.Write([]byte(url))
242 }
243 if w.compact {
244 w.Write([]byte("]:<"))
245 } else {
246 w.Write([]byte("]: <\n"))
247 w.indent++
248 }
249 if err := w.writeMessage(m2); err != nil {
250 return true, err
251 }
252 if w.compact {
253 w.Write([]byte("> "))
254 } else {
255 w.indent--
256 w.Write([]byte(">\n"))
257 }
258 return true, nil
259}
260
261func (w *textWriter) writeMessage(m protoreflect.Message) error {
262 md := m.Descriptor()

Callers 1

writeMessageMethod · 0.95

Calls 11

WriteMethod · 0.95
writeQuotedStringMethod · 0.95
writeMessageMethod · 0.95
requiresQuotesFunction · 0.85
FindMessageByURLMethod · 0.80
BytesMethod · 0.80
InterfaceMethod · 0.80
DescriptorMethod · 0.65
UnmarshalMethod · 0.65
StringMethod · 0.45
NewMethod · 0.45

Tested by

no test coverage detected