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

Method writeMessage

proto/text_encode.go:261–352  ·  view source on GitHub ↗
(m protoreflect.Message)

Source from the content-addressed store, hash-verified

259}
260
261func (w *textWriter) writeMessage(m protoreflect.Message) error {
262 md := m.Descriptor()
263 if w.expandAny && md.FullName() == "google.protobuf.Any" {
264 if canExpand, err := w.writeProto3Any(m); canExpand {
265 return err
266 }
267 }
268
269 fds := md.Fields()
270 for i := 0; i < fds.Len(); {
271 fd := fds.Get(i)
272 if od := fd.ContainingOneof(); od != nil {
273 fd = m.WhichOneof(od)
274 i += od.Fields().Len()
275 } else {
276 i++
277 }
278 if fd == nil || !m.Has(fd) {
279 continue
280 }
281
282 switch {
283 case fd.IsList():
284 lv := m.Get(fd).List()
285 for j := 0; j < lv.Len(); j++ {
286 w.writeName(fd)
287 v := lv.Get(j)
288 if err := w.writeSingularValue(v, fd); err != nil {
289 return err
290 }
291 w.WriteByte('\n')
292 }
293 case fd.IsMap():
294 kfd := fd.MapKey()
295 vfd := fd.MapValue()
296 mv := m.Get(fd).Map()
297
298 type entry struct{ key, val protoreflect.Value }
299 var entries []entry
300 mv.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
301 entries = append(entries, entry{k.Value(), v})
302 return true
303 })
304 sort.Slice(entries, func(i, j int) bool {
305 switch kfd.Kind() {
306 case protoreflect.BoolKind:
307 return !entries[i].key.Bool() && entries[j].key.Bool()
308 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind, protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
309 return entries[i].key.Int() < entries[j].key.Int()
310 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind, protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
311 return entries[i].key.Uint() < entries[j].key.Uint()
312 case protoreflect.StringKind:
313 return entries[i].key.String() < entries[j].key.String()
314 default:
315 panic("invalid kind")
316 }
317 })
318 for _, entry := range entries {

Callers 3

marshalMethod · 0.95
writeProto3AnyMethod · 0.95
writeSingularValueMethod · 0.95

Calls 10

writeProto3AnyMethod · 0.95
writeNameMethod · 0.95
writeSingularValueMethod · 0.95
WriteByteMethod · 0.95
writeUnknownFieldsMethod · 0.95
writeExtensionsMethod · 0.95
HasMethod · 0.80
DescriptorMethod · 0.65
LenMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected