MCPcopy
hub / github.com/protocolbuffers/protobuf-go / marshalMessage

Method marshalMessage

encoding/prototext/encode.go:162–199  ·  view source on GitHub ↗

marshalMessage marshals the given protoreflect.Message.

(m protoreflect.Message, inclDelims bool)

Source from the content-addressed store, hash-verified

160
161// marshalMessage marshals the given protoreflect.Message.
162func (e encoder) marshalMessage(m protoreflect.Message, inclDelims bool) error {
163 messageDesc := m.Descriptor()
164 if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
165 return errors.New("no support for proto1 MessageSets")
166 }
167
168 if inclDelims {
169 e.StartMessage()
170 defer e.EndMessage()
171 }
172
173 // Handle Any expansion.
174 if messageDesc.FullName() == genid.Any_message_fullname {
175 if e.marshalAny(m) {
176 return nil
177 }
178 // If unable to expand, continue on to marshal Any as a regular message.
179 }
180
181 // Marshal fields.
182 var err error
183 order.RangeFields(m, order.IndexNameFieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
184 if err = e.marshalField(fd.TextName(), v, fd); err != nil {
185 return false
186 }
187 return true
188 })
189 if err != nil {
190 return err
191 }
192
193 // Marshal unknown fields.
194 if e.opts.EmitUnknown {
195 e.marshalUnknown(m.GetUnknown())
196 }
197
198 return nil
199}
200
201// marshalField marshals the given field with protoreflect.Value.
202func (e encoder) marshalField(name string, val protoreflect.Value, fd protoreflect.FieldDescriptor) error {

Callers 3

marshalMethod · 0.95
marshalSingularMethod · 0.95
marshalAnyMethod · 0.95

Calls 12

marshalAnyMethod · 0.95
marshalFieldMethod · 0.95
marshalUnknownMethod · 0.95
IsMessageSetFunction · 0.92
NewFunction · 0.92
RangeFieldsFunction · 0.92
StartMessageMethod · 0.80
EndMessageMethod · 0.80
DescriptorMethod · 0.65
FullNameMethod · 0.65
TextNameMethod · 0.65
GetUnknownMethod · 0.65

Tested by

no test coverage detected