ProtobufMirror.swift 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // ProtobufRuntime/Sources/Protobuf/ProtobufMirror.swift - Mirror generation
  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. /// Visitor implementation for constructing custom Swift Mirrors
  14. /// for generated message types. This is a pretty straightforward
  15. /// example of the traversal idiom; the only wrinkle being that
  16. /// the visitor does not recurse into child messages.
  17. ///
  18. // -----------------------------------------------------------------------------
  19. import Swift
  20. public struct ProtobufMirrorVisitor: ProtobufVisitor {
  21. private var mirrorChildren: [Mirror.Child] = []
  22. private var message: ProtobufMessageBase
  23. public mutating func fail() {}
  24. public var mirror: Mirror {
  25. get {
  26. return Mirror(message, children: mirrorChildren /* displayStyle: .Struct */)
  27. }
  28. }
  29. public init(message: ProtobufMessageBase) {
  30. self.message = message
  31. withAbstractVisitor {(visitor: inout ProtobufVisitor) in
  32. try message.traverse(visitor: &visitor)
  33. }
  34. }
  35. public mutating func withAbstractVisitor(clause: (inout ProtobufVisitor) throws -> ()) {
  36. var visitor: ProtobufVisitor = self
  37. let _ = try? clause(&visitor)
  38. mirrorChildren.append(contentsOf: (visitor as! ProtobufMirrorVisitor).mirrorChildren)
  39. }
  40. mutating public func visitUnknown(bytes: [UInt8]) {}
  41. mutating public func visitSingularField<S: ProtobufTypeProperties>(fieldType: S.Type, value: S.BaseType, protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
  42. mirrorChildren.append((label: swiftFieldName, value: value))
  43. }
  44. mutating public func visitRepeatedField<S: ProtobufTypeProperties>(fieldType: S.Type, value: [S.BaseType], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
  45. mirrorChildren.append((label: swiftFieldName, value: value))
  46. }
  47. mutating public func visitPackedField<S: ProtobufTypeProperties>(fieldType: S.Type, value: [S.BaseType], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
  48. mirrorChildren.append((label: swiftFieldName, value: value))
  49. }
  50. mutating public func visitSingularMessageField<M: ProtobufMessage>(value: M, protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
  51. mirrorChildren.append((label: swiftFieldName, value: value))
  52. }
  53. mutating public func visitRepeatedMessageField<M: ProtobufMessage>(value:[M], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
  54. mirrorChildren.append((label: swiftFieldName, value: value))
  55. }
  56. mutating public func visitSingularGroupField<G: ProtobufGroup>(value: G, protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
  57. mirrorChildren.append((label: swiftFieldName, value: value))
  58. }
  59. mutating public func visitRepeatedGroupField<G: ProtobufGroup>(value: [G], protoFieldNumber: Int, protoFieldName: String, jsonFieldName: String, swiftFieldName: String) throws {
  60. mirrorChildren.append((label: swiftFieldName, value: value))
  61. }
  62. mutating public 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 {
  63. mirrorChildren.append((label: swiftFieldName, value: value))
  64. }
  65. }