ProtobufGroup.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // ProtobufRuntime/Sources/Protobuf/ProtobufGroup.swift - Group support
  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. /// These are the core protocols that are implemented by generated group types.
  14. ///
  15. // -----------------------------------------------------------------------------
  16. import Swift
  17. ///
  18. /// Generated Groups conform to ProtobufGroup
  19. ///
  20. public protocol ProtobufGroupBase: CustomDebugStringConvertible, ProtobufTraversable {
  21. init()
  22. mutating func decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool
  23. mutating func decodeFromJSONObject(jsonDecoder: inout ProtobufJSONDecoder) throws
  24. var isEmpty: Bool { get }
  25. var swiftClassName: String { get }
  26. }
  27. public extension ProtobufGroupBase {
  28. var hashValue: Int {
  29. return ProtobufHashVisitor(group: self).hashValue
  30. }
  31. var debugDescription: String {
  32. return ProtobufDebugDescriptionVisitor(group: self).description
  33. }
  34. }
  35. public protocol ProtobufGroup: ProtobufGroupBase, Hashable {
  36. func isEqualTo(other: Self) -> Bool
  37. var jsonFieldNames: [String: Int] {get}
  38. var protoFieldNames: [String: Int] {get}
  39. }
  40. public func ==<G: ProtobufGroup>(lhs: G, rhs: G) -> Bool {
  41. return lhs.isEqualTo(other: rhs)
  42. }
  43. public protocol ProtobufGeneratedGroup: ProtobufGroup {
  44. // The compiler actually generates the following methods.
  45. // Default implementations below redirect the standard names.
  46. // This allows developers to override the standard names to
  47. // customize the behavior.
  48. mutating func _protoc_generated_decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool
  49. func _protoc_generated_traverse(visitor: inout ProtobufVisitor) throws
  50. var _protoc_generated_isEmpty: Bool { get }
  51. func _protoc_generated_isEqualTo(other: Self) -> Bool
  52. }
  53. public extension ProtobufGeneratedGroup {
  54. // Default implementations simply redirect to the generated versions.
  55. func traverse(visitor: inout ProtobufVisitor) throws {
  56. try _protoc_generated_traverse(visitor: &visitor)
  57. }
  58. mutating func decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
  59. return try _protoc_generated_decodeField(setter: &setter, protoFieldNumber: protoFieldNumber)
  60. }
  61. var isEmpty: Bool { get { return _protoc_generated_isEmpty } }
  62. func isEqualTo(other: Self) -> Bool {
  63. return _protoc_generated_isEqualTo(other: other)
  64. }
  65. }
  66. // TODO: This is a transition aid, remove this in August 2016.
  67. public typealias ProtobufGeneratedGroupType = ProtobufGeneratedGroup