-
Notifications
You must be signed in to change notification settings - Fork 466
/
Copy pathSimpleExtensionMap.swift
117 lines (104 loc) · 3.58 KB
/
SimpleExtensionMap.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Sources/SwiftProtobuf/SimpleExtensionMap.swift - Extension support
//
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
//
// -----------------------------------------------------------------------------
///
/// A default implementation of ExtensionMap.
///
// -----------------------------------------------------------------------------
// Note: The generated code only relies on ExpressibleByArrayLiteral
public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral {
public typealias Element = AnyMessageExtension
// Since type objects aren't Hashable, we can't do much better than this...
internal var fields = [Int: [any AnyMessageExtension]]()
public init() {}
public init(arrayLiteral: any Element...) {
insert(contentsOf: arrayLiteral)
}
public init(_ others: SimpleExtensionMap...) {
for other in others {
formUnion(other)
}
}
public subscript(messageType: any Message.Type, fieldNumber: Int) -> (any AnyMessageExtension)? {
get {
if let l = fields[fieldNumber] {
for e in l {
if messageType == e.messageType {
return e
}
}
}
return nil
}
}
public func fieldNumberForProto(messageType: any Message.Type, protoFieldName: String) -> Int? {
// TODO: Make this faster...
for (_, list) in fields {
for e in list {
if e.fieldName == protoFieldName && e.messageType == messageType {
return e.fieldNumber
}
}
}
return nil
}
public mutating func insert(_ newValue: any Element) {
let fieldNumber = newValue.fieldNumber
if let l = fields[fieldNumber] {
let messageType = newValue.messageType
var newL = l.filter { $0.messageType != messageType }
newL.append(newValue)
fields[fieldNumber] = newL
} else {
fields[fieldNumber] = [newValue]
}
}
public mutating func insert(contentsOf: [any Element]) {
for e in contentsOf {
insert(e)
}
}
public mutating func formUnion(_ other: SimpleExtensionMap) {
for (fieldNumber, otherList) in other.fields {
if let list = fields[fieldNumber] {
var newList = list.filter {
for o in otherList {
if $0.messageType == o.messageType { return false }
}
return true
}
newList.append(contentsOf: otherList)
fields[fieldNumber] = newList
} else {
fields[fieldNumber] = otherList
}
}
}
public func union(_ other: SimpleExtensionMap) -> SimpleExtensionMap {
var out = self
out.formUnion(other)
return out
}
}
extension SimpleExtensionMap: CustomDebugStringConvertible {
public var debugDescription: String {
#if DEBUG
var names = [String]()
for (_, list) in fields {
for e in list {
names.append("\(e.fieldName):(\(e.fieldNumber))")
}
}
let d = names.joined(separator: ",")
return "SimpleExtensionMap(\(d))"
#else
return String(reflecting: type(of: self))
#endif
}
}