@@ -2,83 +2,6 @@ import Foundation
2
2
3
3
internal extension Configuration {
4
4
struct FileGraph : Hashable {
5
- // MARK: - Subtypes
6
- public enum FilePath : Hashable { // swiftlint:disable:this nesting
7
- case promised( urlString: String )
8
- case existing( path: String )
9
- }
10
-
11
- private class Vertix : Hashable { // swiftlint:disable:this nesting
12
- internal var originatesFromRemote : Bool { return originalRemoteString != nil }
13
- internal let originalRemoteString : String ?
14
-
15
- private( set) var filePath : FilePath
16
-
17
- private( set) var configurationString : String = " "
18
- private( set) var configurationDict : [ String : Any ] = [ : ]
19
-
20
- init ( string: String , rootDirectory: String ) {
21
- if string. hasPrefix ( " http:// " ) || string. hasPrefix ( " https:// " ) {
22
- originalRemoteString = string
23
- filePath = . promised( urlString: string)
24
- } else {
25
- originalRemoteString = nil
26
- filePath = . existing(
27
- path: string. bridge ( ) . absolutePathRepresentation ( rootDirectory: rootDirectory)
28
- )
29
- }
30
- }
31
-
32
- internal func build(
33
- remoteConfigTimeout: TimeInterval ,
34
- remoteConfigTimeoutIfCached: TimeInterval
35
- ) throws {
36
- let path = try filePath. resolve (
37
- remoteConfigTimeout: remoteConfigTimeout,
38
- remoteConfigTimeoutIfCached: remoteConfigTimeoutIfCached
39
- )
40
-
41
- filePath = . existing( path: path)
42
- configurationString = try read ( at: path)
43
- configurationDict = try YamlParser . parse ( configurationString)
44
- }
45
-
46
- private func read( at path: String ) throws -> String {
47
- guard !path. isEmpty && FileManager . default. fileExists ( atPath: path) else {
48
- throw ConfigurationError . generic ( " File \( path) can't be found. " )
49
- }
50
-
51
- return try String ( contentsOfFile: path, encoding: . utf8)
52
- }
53
-
54
- internal static func == ( lhs: Vertix , rhs: Vertix ) -> Bool {
55
- return lhs. filePath == rhs. filePath
56
- }
57
-
58
- internal func hash( into hasher: inout Hasher ) {
59
- hasher. combine ( filePath)
60
- }
61
- }
62
-
63
- private struct Edge : Hashable { // swiftlint:disable:this nesting
64
- var parent : Vertix !
65
- var child : Vertix !
66
-
67
- internal static func == ( lhs: Edge , rhs: Edge ) -> Bool {
68
- return lhs. parent == rhs. parent && lhs. child == rhs. child
69
- }
70
-
71
- internal func hash( into hasher: inout Hasher ) {
72
- hasher. combine ( parent)
73
- hasher. combine ( child)
74
- }
75
- }
76
-
77
- private enum EdgeType : Hashable { // swiftlint:disable:this nesting
78
- case childConfig
79
- case parentConfig
80
- }
81
-
82
5
// MARK: - Properties
83
6
private static let defaultRemoteConfigTimeout : TimeInterval = 2
84
7
private static let defaultRemoteConfigTimeoutIfCached : TimeInterval = 1
@@ -209,11 +132,12 @@ internal extension Configuration {
209
132
throw ConfigurationError . generic ( " Remote configs are not allowed to reference local configs. " )
210
133
} else {
211
134
let existingVertix = findPossiblyExistingVertix ( sameAs: referencedVertix)
135
+ let existingVertixCopy = existingVertix. map { $0. copy ( withNewRootDirectory: rootDirectory) }
212
136
213
137
edges. insert (
214
138
type == . childConfig
215
- ? Edge ( parent: vertix, child: existingVertix ?? referencedVertix)
216
- : Edge ( parent: existingVertix ?? referencedVertix, child: vertix)
139
+ ? Edge ( parent: vertix, child: existingVertixCopy ?? referencedVertix)
140
+ : Edge ( parent: existingVertixCopy ?? referencedVertix, child: vertix)
217
141
)
218
142
219
143
if existingVertix == nil {
@@ -251,6 +175,7 @@ internal extension Configuration {
251
175
private func validate( ) throws -> [ ( configurationDict: [ String : Any ] , rootDirectory: String ) ] {
252
176
// Detect cycles via back-edge detection during DFS
253
177
func walkDown( stack: [ Vertix ] ) throws {
178
+ // Please note that the equality check (`==`), not the identity check (`===`) is used
254
179
let neighbours = edges. filter { $0. parent == stack. last } . map { $0. child! }
255
180
if stack. contains ( where: neighbours. contains) {
256
181
throw ConfigurationError . generic ( " There's a cycle of child / parent config references. "
@@ -296,19 +221,10 @@ internal extension Configuration {
296
221
verticesToMerge. append ( vertix)
297
222
}
298
223
299
- return try verticesToMerge. map {
300
- var rootDirectory = " "
301
- if case let . existing( path) = $0. filePath {
302
- rootDirectory = path. bridge ( ) . deletingLastPathComponent
303
- } else {
304
- throw ConfigurationError . generic (
305
- " Internal error: Processing promised config that doesn't exist yet "
306
- )
307
- }
308
-
224
+ return verticesToMerge. map {
309
225
return (
310
226
configurationDict: $0. configurationDict,
311
- rootDirectory: rootDirectory
227
+ rootDirectory: $0 . rootDirectory
312
228
)
313
229
}
314
230
}
0 commit comments