WireFormat.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Sources/SwiftProtobuf/WireFormat.swift - Describes proto wire formats
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Types related to binary wire formats of encoded values.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. /// Denotes the wire format by which a value is encoded in binary form.
  15. internal enum WireFormat: UInt8 {
  16. case varint = 0
  17. case fixed64 = 1
  18. case lengthDelimited = 2
  19. case startGroup = 3
  20. case endGroup = 4
  21. case fixed32 = 5
  22. }
  23. extension WireFormat {
  24. /// Information about the "MessageSet" format. Used when a Message has
  25. /// the message_set_wire_format option enabled.
  26. ///
  27. /// Writing in MessageSet form means instead of writing the Extesions
  28. /// normally as a simple fields, each gets written wrapped in a group:
  29. /// repeated group Item = 1 {
  30. /// required int32 type_id = 2;
  31. /// required bytes message = 3;
  32. /// }
  33. /// Where the field number is the type_id, and the message is serilaized
  34. /// into the bytes.
  35. ///
  36. /// The handling of unknown fields is ill defined. In proto1, they were
  37. /// dropped. In the C++ for proto2, since it stores them in the unknowns
  38. /// storage, if preserves any that are length delimited data (since that's
  39. /// how the message also goes out). While the C++ is parsing, where the
  40. /// unknowns fall in the flow of the group, sorta decides what happens.
  41. /// Since it is ill defined, currently SwiftProtobuf will reflect out
  42. /// anything set in the unknownStorage. During parsing, unknowns on the
  43. /// message are preserved, but unknowns within the group are dropped (like
  44. /// map items). Any extension in the MessageSet that isn't in the Regisry
  45. /// being used at parse time will remain in a group and go into the
  46. /// Messages's unknown fields (this way it reflects back out correctly).
  47. internal enum MessageSet {
  48. enum FieldNumbers {
  49. static let item = 1
  50. static let typeId = 2
  51. static let message = 3
  52. }
  53. enum Tags {
  54. static let itemStart = FieldTag(fieldNumber: FieldNumbers.item, wireFormat: .startGroup)
  55. static let itemEnd = FieldTag(fieldNumber: FieldNumbers.item, wireFormat: .endGroup)
  56. static let typeId = FieldTag(fieldNumber: FieldNumbers.typeId, wireFormat: .varint)
  57. static let message = FieldTag(fieldNumber: FieldNumbers.message, wireFormat: .lengthDelimited)
  58. }
  59. // The size of all the tags needed to write out an Extension in MessageSet format.
  60. static let itemTagsEncodedSize =
  61. Tags.itemStart.encodedSize + Tags.itemEnd.encodedSize + Tags.typeId.encodedSize + Tags.message.encodedSize
  62. }
  63. }