UnknownStorage.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Sources/SwiftProtobuf/UnknownStorage.swift - Handling unknown fields
  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. /// Proto2 binary coding requires storing and recoding of unknown fields.
  12. /// This simple support class handles that requirement. A property of this type
  13. /// is compiled into every proto2 message.
  14. ///
  15. // -----------------------------------------------------------------------------
  16. import Foundation
  17. /// Contains any unknown fields in a decoded message; that is, fields that were
  18. /// sent on the wire but were not recognized by the generated message
  19. /// implementation or were valid field numbers but with mismatching wire
  20. /// formats (for example, a field encoded as a varint when a fixed32 integer
  21. /// was expected).
  22. public struct UnknownStorage: Equatable, Sendable {
  23. /// The raw protocol buffer binary-encoded bytes that represent the unknown
  24. /// fields of a decoded message.
  25. public private(set) var data = Data()
  26. public init() {}
  27. package mutating func append(protobufData: Data) {
  28. data.append(protobufData)
  29. }
  30. public func traverse<V: Visitor>(visitor: inout V) throws {
  31. if !data.isEmpty {
  32. try visitor.visitUnknown(bytes: data)
  33. }
  34. }
  35. }