202 } |
201 } |
203 } |
202 } |
204 |
203 |
205 // marshalSingular marshals the given non-repeated field value. This includes |
204 // marshalSingular marshals the given non-repeated field value. This includes |
206 // all scalar types, enums, messages, and groups. |
205 // all scalar types, enums, messages, and groups. |
207 func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error { |
206 func (e encoder) marshalSingular(val protoreflect.Value, fd protoreflect.FieldDescriptor) error { |
208 kind := fd.Kind() |
207 kind := fd.Kind() |
209 switch kind { |
208 switch kind { |
210 case pref.BoolKind: |
209 case protoreflect.BoolKind: |
211 e.WriteBool(val.Bool()) |
210 e.WriteBool(val.Bool()) |
212 |
211 |
213 case pref.StringKind: |
212 case protoreflect.StringKind: |
214 s := val.String() |
213 s := val.String() |
215 if !e.opts.allowInvalidUTF8 && strs.EnforceUTF8(fd) && !utf8.ValidString(s) { |
214 if !e.opts.allowInvalidUTF8 && strs.EnforceUTF8(fd) && !utf8.ValidString(s) { |
216 return errors.InvalidUTF8(string(fd.FullName())) |
215 return errors.InvalidUTF8(string(fd.FullName())) |
217 } |
216 } |
218 e.WriteString(s) |
217 e.WriteString(s) |
219 |
218 |
220 case pref.Int32Kind, pref.Int64Kind, |
219 case protoreflect.Int32Kind, protoreflect.Int64Kind, |
221 pref.Sint32Kind, pref.Sint64Kind, |
220 protoreflect.Sint32Kind, protoreflect.Sint64Kind, |
222 pref.Sfixed32Kind, pref.Sfixed64Kind: |
221 protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind: |
223 e.WriteInt(val.Int()) |
222 e.WriteInt(val.Int()) |
224 |
223 |
225 case pref.Uint32Kind, pref.Uint64Kind, |
224 case protoreflect.Uint32Kind, protoreflect.Uint64Kind, |
226 pref.Fixed32Kind, pref.Fixed64Kind: |
225 protoreflect.Fixed32Kind, protoreflect.Fixed64Kind: |
227 e.WriteUint(val.Uint()) |
226 e.WriteUint(val.Uint()) |
228 |
227 |
229 case pref.FloatKind: |
228 case protoreflect.FloatKind: |
230 // Encoder.WriteFloat handles the special numbers NaN and infinites. |
229 // Encoder.WriteFloat handles the special numbers NaN and infinites. |
231 e.WriteFloat(val.Float(), 32) |
230 e.WriteFloat(val.Float(), 32) |
232 |
231 |
233 case pref.DoubleKind: |
232 case protoreflect.DoubleKind: |
234 // Encoder.WriteFloat handles the special numbers NaN and infinites. |
233 // Encoder.WriteFloat handles the special numbers NaN and infinites. |
235 e.WriteFloat(val.Float(), 64) |
234 e.WriteFloat(val.Float(), 64) |
236 |
235 |
237 case pref.BytesKind: |
236 case protoreflect.BytesKind: |
238 e.WriteString(string(val.Bytes())) |
237 e.WriteString(string(val.Bytes())) |
239 |
238 |
240 case pref.EnumKind: |
239 case protoreflect.EnumKind: |
241 num := val.Enum() |
240 num := val.Enum() |
242 if desc := fd.Enum().Values().ByNumber(num); desc != nil { |
241 if desc := fd.Enum().Values().ByNumber(num); desc != nil { |
243 e.WriteLiteral(string(desc.Name())) |
242 e.WriteLiteral(string(desc.Name())) |
244 } else { |
243 } else { |
245 // Use numeric value if there is no enum description. |
244 // Use numeric value if there is no enum description. |
246 e.WriteInt(int64(num)) |
245 e.WriteInt(int64(num)) |
247 } |
246 } |
248 |
247 |
249 case pref.MessageKind, pref.GroupKind: |
248 case protoreflect.MessageKind, protoreflect.GroupKind: |
250 return e.marshalMessage(val.Message(), true) |
249 return e.marshalMessage(val.Message(), true) |
251 |
250 |
252 default: |
251 default: |
253 panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind)) |
252 panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind)) |
254 } |
253 } |
255 return nil |
254 return nil |
256 } |
255 } |
257 |
256 |
258 // marshalList marshals the given protoreflect.List as multiple name-value fields. |
257 // marshalList marshals the given protoreflect.List as multiple name-value fields. |
259 func (e encoder) marshalList(name string, list pref.List, fd pref.FieldDescriptor) error { |
258 func (e encoder) marshalList(name string, list protoreflect.List, fd protoreflect.FieldDescriptor) error { |
260 size := list.Len() |
259 size := list.Len() |
261 for i := 0; i < size; i++ { |
260 for i := 0; i < size; i++ { |
262 e.WriteName(name) |
261 e.WriteName(name) |
263 if err := e.marshalSingular(list.Get(i), fd); err != nil { |
262 if err := e.marshalSingular(list.Get(i), fd); err != nil { |
264 return err |
263 return err |
332 } |
331 } |
333 } |
332 } |
334 |
333 |
335 // marshalAny marshals the given google.protobuf.Any message in expanded form. |
334 // marshalAny marshals the given google.protobuf.Any message in expanded form. |
336 // It returns true if it was able to marshal, else false. |
335 // It returns true if it was able to marshal, else false. |
337 func (e encoder) marshalAny(any pref.Message) bool { |
336 func (e encoder) marshalAny(any protoreflect.Message) bool { |
338 // Construct the embedded message. |
337 // Construct the embedded message. |
339 fds := any.Descriptor().Fields() |
338 fds := any.Descriptor().Fields() |
340 fdType := fds.ByNumber(genid.Any_TypeUrl_field_number) |
339 fdType := fds.ByNumber(genid.Any_TypeUrl_field_number) |
341 typeURL := any.Get(fdType).String() |
340 typeURL := any.Get(fdType).String() |
342 mt, err := e.opts.Resolver.FindMessageByURL(typeURL) |
341 mt, err := e.opts.Resolver.FindMessageByURL(typeURL) |