-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathParsedEntity.swift
More file actions
200 lines (175 loc) · 7 KB
/
ParsedEntity.swift
File metadata and controls
200 lines (175 loc) · 7 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// Copyright (c) 2018. Uber Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Algorithms
/// Metadata containing unique models and potential init params ready to be rendered for output
struct ResolvedEntity {
var key: String
var entity: Entity
var uniqueModels: [(String, Model)]
var attributes: [String]
var inheritedTypes: [String]
var declaredInits: [MethodModel] {
return uniqueModels.compactMap { (_, model) in
guard let model = model as? MethodModel,
model.isInitializer else { return nil }
return model
}
}
var initParamCandidates: [VariableModel] {
return sortedInitVars(
in: uniqueModels.compactMap{ $0.1 as? VariableModel }
)
}
var inheritsActorProtocol: Bool {
return inheritedTypes.contains(.actorProtocol)
}
/// Returns models that can be used as parameters to an initializer
/// @param models The models of the current entity including unprocessed (ones to generate) and
/// processed (already mocked by a previous run if any) models.
/// @returns A list of init parameter models
private func sortedInitVars(`in` models: [VariableModel]) -> [VariableModel] {
let (unprocessed, processed) = models.filter(\.canBeInitParam).partitioned(by: \.processed)
// Named params in init should be unique. Add a duplicate param check to ensure it.
let curVarsSorted = unprocessed.sorted(path: \.offset, fallback: \.name)
let curVarNames = curVarsSorted.map(\.name)
let parentVars = processed.filter {!curVarNames.contains($0.name)}
let parentVarsSorted = parentVars.sorted(path: \.offset, fallback: \.name)
let result = [curVarsSorted, parentVarsSorted].flatMap{$0}
return result
}
var requiresSendable: Bool {
return inheritedTypes.contains(.sendable) || inheritedTypes.contains(.error)
}
func model() -> Model {
let metadata = entity.metadata
// Combine protocol-level attributes with member-level attributes
let protocolLevelAttributes =
entity.entityNode.attributesDescription.isEmpty
? [] : [entity.entityNode.attributesDescription]
let combinedAttributes = protocolLevelAttributes + attributes
return NominalModel(selfType: .init(name: metadata?.nameOverride ?? (key + "Mock")),
namespaces: entity.entityNode.namespaces,
acl: entity.entityNode.accessLevel,
declKindOfMockAnnotatedBaseType: entity.entityNode.declKind,
declKind: inheritsActorProtocol ? .actor : .class,
attributes: combinedAttributes,
offset: entity.entityNode.offset,
inheritedTypeName: (entity.metadata?.module?.withDot ?? "") + key,
genericWhereConstraints: entity.entityNode.genericWhereConstraints,
initParamCandidates: initParamCandidates,
declaredInits: declaredInits,
entities: uniqueModels,
requiresSendable: requiresSendable)
}
}
struct ResolvedEntityContainer {
var entity: ResolvedEntity
var paths: [String]
}
protocol EntityNode {
var namespaces: [String] { get }
var nameText: String { get }
var mayHaveGlobalActor: Bool { get }
var accessLevel: String { get }
var attributesDescription: String { get }
var declKind: NominalTypeDeclKind { get }
var inheritedTypes: [String] { get }
var genericWhereConstraints: [String] { get }
var offset: Int64 { get }
var hasBlankInit: Bool { get }
func subContainer(metadata: AnnotationMetadata?, declKind: NominalTypeDeclKind, path: String?, isProcessed: Bool) -> EntityNodeSubContainer
}
struct EntityNodeSubContainer {
var attributes: [String]
var members: [Model]
var hasInit: Bool
}
public enum CombineType {
case passthroughSubject
case currentValueSubject
case property(wrapper: String, name: String)
var typeName: String {
switch self {
case .passthroughSubject:
return .passthroughSubject
case .currentValueSubject:
return .currentValueSubject
case .property:
return ""
}
}
}
/// Contains arguments to annotation
/// e.g. @mockable(module: prefix = Foo; typealias: T = Any; U = String; rx: barStream = PublishSubject; history: bazFunc = true; modifiers: someVar = weak; combine: fooPublisher = CurrentValueSubject; otherPublisher = @Published otherProperty, override: name = FooMock)
struct AnnotationMetadata {
var nameOverride: String?
var module: String?
var typeAliases: [String: String]?
var varTypes: [String: String]?
var funcsWithArgsHistory: [String]?
var modifiers: [String: Modifier]?
var combineTypes: [String: CombineType]?
}
struct GenerationArguments {
var useTemplateFunc: Bool
var allowSetCallCount: Bool
var mockFinal: Bool
var enableFuncArgsHistory: Bool
var disableCombineDefaultValues: Bool
static let `default` = GenerationArguments(
useTemplateFunc: false,
allowSetCallCount: false,
mockFinal: false,
enableFuncArgsHistory: false,
disableCombineDefaultValues: false
)
}
typealias ImportMap = [String: [ImportContent]]
/// Metadata for a type being mocked
public final class Entity {
let entityNode: EntityNode
let filepath: String
let metadata: AnnotationMetadata?
let isProcessed: Bool
var isAnnotated: Bool {
return metadata != nil
}
static func node(with entityNode: EntityNode,
filepath: String,
isPrivate: Bool,
isFinal: Bool,
metadata: AnnotationMetadata?,
processed: Bool) -> Entity? {
guard !isPrivate, !isFinal else {return nil}
return Entity(entityNode: entityNode,
filepath: filepath,
metadata: metadata,
isProcessed: processed)
}
init(entityNode: EntityNode,
filepath: String,
metadata: AnnotationMetadata?,
isProcessed: Bool) {
self.entityNode = entityNode
self.filepath = filepath
self.metadata = metadata
self.isProcessed = isProcessed
}
}
enum Modifier: String {
case weak = "weak"
case dynamic = "dynamic"
}