SimpleExtensionMap.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Sources/SwiftProtobuf/SimpleExtensionMap.swift - Extension support
  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. /// A default implementation of ExtensionMap.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. // Note: The generated code only relies on ExpressibleByArrayLiteral
  15. public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral {
  16. public typealias Element = AnyMessageExtension
  17. // Since type objects aren't Hashable, we can't do much better than this...
  18. package var fields = [Int: [any AnyMessageExtension]]()
  19. public init() {}
  20. public init(arrayLiteral: any Element...) {
  21. insert(contentsOf: arrayLiteral)
  22. }
  23. public init(_ others: SimpleExtensionMap...) {
  24. for other in others {
  25. formUnion(other)
  26. }
  27. }
  28. public subscript(messageType: any Message.Type, fieldNumber: Int) -> (any AnyMessageExtension)? {
  29. get {
  30. if let l = fields[fieldNumber] {
  31. for e in l {
  32. if messageType == e.messageType {
  33. return e
  34. }
  35. }
  36. }
  37. return nil
  38. }
  39. }
  40. public func fieldNumberForProto(messageType: any Message.Type, protoFieldName: String) -> Int? {
  41. // TODO: Make this faster...
  42. for (_, list) in fields {
  43. for e in list {
  44. if e.fieldName == protoFieldName && e.messageType == messageType {
  45. return e.fieldNumber
  46. }
  47. }
  48. }
  49. return nil
  50. }
  51. public mutating func insert(_ newValue: any Element) {
  52. let fieldNumber = newValue.fieldNumber
  53. if let l = fields[fieldNumber] {
  54. let messageType = newValue.messageType
  55. var newL = l.filter { $0.messageType != messageType }
  56. newL.append(newValue)
  57. fields[fieldNumber] = newL
  58. } else {
  59. fields[fieldNumber] = [newValue]
  60. }
  61. }
  62. public mutating func insert(contentsOf: [any Element]) {
  63. for e in contentsOf {
  64. insert(e)
  65. }
  66. }
  67. public mutating func formUnion(_ other: SimpleExtensionMap) {
  68. for (fieldNumber, otherList) in other.fields {
  69. if let list = fields[fieldNumber] {
  70. var newList = list.filter {
  71. for o in otherList {
  72. if $0.messageType == o.messageType { return false }
  73. }
  74. return true
  75. }
  76. newList.append(contentsOf: otherList)
  77. fields[fieldNumber] = newList
  78. } else {
  79. fields[fieldNumber] = otherList
  80. }
  81. }
  82. }
  83. public func union(_ other: SimpleExtensionMap) -> SimpleExtensionMap {
  84. var out = self
  85. out.formUnion(other)
  86. return out
  87. }
  88. }
  89. extension SimpleExtensionMap: CustomDebugStringConvertible {
  90. public var debugDescription: String {
  91. #if DEBUG
  92. var names = [String]()
  93. for (_, list) in fields {
  94. for e in list {
  95. names.append("\(e.fieldName):(\(e.fieldNumber))")
  96. }
  97. }
  98. let d = names.joined(separator: ",")
  99. return "SimpleExtensionMap(\(d))"
  100. #else
  101. return String(reflecting: type(of: self))
  102. #endif
  103. }
  104. }