encodeField encodes a single proto field to buf.
(s *compiler.S, ptr, buf *codegen.Value, id serialization.ProtoFieldID, ty semantic.Type)
| 342 | |
| 343 | // encodeField encodes a single proto field to buf. |
| 344 | func (e *encoder) encodeField(s *compiler.S, ptr, buf *codegen.Value, id serialization.ProtoFieldID, ty semantic.Type) { |
| 345 | e.debug(s, "encoding field at %p: '%s' id: %d, ty: %s", ptr, ptr.Name(), id, ty.Name()) |
| 346 | |
| 347 | ent := e.ent(ty) |
| 348 | doEncode := func(s *compiler.S) { |
| 349 | switch ty := semantic.Underlying(ty).(type) { |
| 350 | case *semantic.StaticArray: |
| 351 | if ent := e.ent(ty.ValueType); ent.isPacked() { |
| 352 | e.writeWireAndTag(s, buf, proto.WireBytes, id) |
| 353 | e.writeBlob(s, buf, func(buf *codegen.Value) { |
| 354 | for i := uint32(0); i < ty.Size; i++ { |
| 355 | e.encodeValue(s, ptr.Index(0, int(i)), buf, ty.ValueType) |
| 356 | } |
| 357 | }) |
| 358 | } else { |
| 359 | for i := uint32(0); i < ty.Size; i++ { |
| 360 | e.writeWireAndTag(s, buf, ent.wireTy, id) |
| 361 | e.encodeValue(s, ptr.Index(0, int(i)), buf, ty.ValueType) |
| 362 | } |
| 363 | } |
| 364 | default: |
| 365 | e.writeWireAndTag(s, buf, ent.wireTy, id) |
| 366 | e.encodeValue(s, ptr, buf, ty) |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if cond := e.shouldEncode(s, ptr, ty); cond != nil { |
| 371 | s.If(cond, doEncode) |
| 372 | } else { |
| 373 | doEncode(s) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // shouldEncode returns a boolean value which determines whether the proto value |
| 378 | // should be encoded. Fields that are zero or nil are not encoded. |
no test coverage detected