ProtobufTraversal.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // ProtobufRuntime/Sources/Protobuf/ProtobufTraversal.swift - Basic serialization machinery
  2. //
  3. // This source file is part of the Swift.org open source project
  4. //
  5. // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See http://swift.org/LICENSE.txt for license information
  9. // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  10. //
  11. // -----------------------------------------------------------------------------
  12. ///
  13. /// Protocol for traversing the object tree.
  14. ///
  15. /// This is used by:
  16. /// = Protobuf serialization
  17. /// = JSON serialization (with some twists to account for specialty JSON encodings)
  18. /// = hashValue computation
  19. /// = mirror generation
  20. ///
  21. /// Conceptually, serializers create visitor objects that are
  22. /// then passed recursively to every message and field via generated
  23. /// 'traverse' methods. The details get a little involved due to
  24. /// the need to allow particular messages to override particular
  25. /// behaviors for specific encodings, but the general idea is quite simple.
  26. ///
  27. // -----------------------------------------------------------------------------
  28. import Swift
  29. public protocol ProtobufTraversable {
  30. func traverse(visitor: inout ProtobufVisitor) throws
  31. }
  32. public protocol ProtobufVisitor {
  33. /// For proto2, visitors get to see the raw bytes for any unknown fields
  34. mutating func visitUnknown(bytes: [UInt8])
  35. mutating func visitSingularField<S: ProtobufTypeProperties>(fieldType: S.Type, value: S.BaseType, protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws
  36. mutating func visitRepeatedField<S: ProtobufTypeProperties>(fieldType: S.Type, value: [S.BaseType], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws
  37. mutating func visitPackedField<S: ProtobufTypeProperties>(fieldType: S.Type, value: [S.BaseType], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws
  38. mutating func visitSingularMessageField<M: ProtobufMessage>(value: M, protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws
  39. mutating func visitRepeatedMessageField<M: ProtobufMessage>(value: [M], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws
  40. mutating func visitSingularGroupField<G: ProtobufGroup>(value: G, protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws
  41. mutating func visitRepeatedGroupField<G: ProtobufGroup>(value: [G], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws
  42. mutating func visitMapField<KeyType: ProtobufMapKeyType, ValueType: ProtobufMapValueType>(fieldType: ProtobufMap<KeyType, ValueType>.Type, value: ProtobufMap<KeyType, ValueType>.BaseType, protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws where KeyType.BaseType: Hashable
  43. }