Skip to content

Commit

Permalink
More tests. CombinedScope -> ScopeGenerator. Still need more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Dec 9, 2023
1 parent 2bbc934 commit d9fb05d
Show file tree
Hide file tree
Showing 7 changed files with 717 additions and 306 deletions.
37 changes: 12 additions & 25 deletions Sources/SafeDICore/Generators/DependencyTreeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,22 @@ public final class DependencyTreeGenerator {

let typeDescriptionToScopeMap = try createTypeDescriptionToScopeMapping()
try validateUndeclaredReceivedProperties(typeDescriptionToScopeMap: typeDescriptionToScopeMap)
let rootCombinedScopes = try rootInstantiableTypes
let rootScopeGenerators = try rootInstantiableTypes
.sorted()
.compactMap { try typeDescriptionToScopeMap[$0]?.createCombinedScope() }
.compactMap { try typeDescriptionToScopeMap[$0]?.createScopeGenerator() }

let dependencyTree = try await withThrowingTaskGroup(
of: String.self,
returning: String.self
) { taskGroup in
for rootCombinedScope in rootCombinedScopes {
if rootCombinedScope.instantiable.dependencies.isEmpty {
// Nothing to do here! We already have an empty initializer.
} else {
taskGroup.addTask {
try await """
extension \(rootCombinedScope.instantiable.concreteInstantiableType.asSource) {
\(rootCombinedScope.instantiable.isClass ? "@convenience " : "")init() {
\(rootCombinedScope.generateCode(leadingWhitespace: " "))
self.init(\(rootCombinedScope.instantiable.initializer.createInitializerArgumentList(given: rootCombinedScope.instantiable.dependencies)))
}
}
"""
}
}
for rootScopeGenerator in rootScopeGenerators {
taskGroup.addTask { try await rootScopeGenerator.generateCode() }
}
var generatedCombinedScopes = [String]()
for try await generatedCombinedScope in taskGroup {
generatedCombinedScopes.append(generatedCombinedScope)
var generatedRoots = [String]()
for try await generatedRoot in taskGroup {
generatedRoots.append(generatedRoot)
}
return generatedCombinedScopes.sorted().joined(separator: "\n\n")
return generatedRoots.sorted().joined(separator: "\n\n").trimmingCharacters(in: .whitespacesAndNewlines)
}

return """
Expand Down Expand Up @@ -220,7 +207,7 @@ public final class DependencyTreeGenerator {
property: instantiatedProperty,
instantiable: instantiable,
scope: instantiatedScope,
type: instantiatedProperty.nonLazyPropertyType
type: instantiatedProperty.propertyType
))
}
for instantiatedProperty in scope.instantiable.lazyInstantiatedProperties {
Expand All @@ -247,7 +234,7 @@ public final class DependencyTreeGenerator {

private func validateUndeclaredReceivedProperties(typeDescriptionToScopeMap: [TypeDescription: Scope]) throws {
var unfulfillableProperties = Set<DependencyTreeGeneratorError.UnfulfillableProperty>()
func propagateUndeclaredReceivedProperties(
func validateUndeclaredReceivedProperties(
on scope: Scope,
receivableProperties: Set<Property>,
instantiables: OrderedSet<Instantiable>
Expand All @@ -273,7 +260,7 @@ public final class DependencyTreeGenerator {
var instantiables = instantiables
instantiables.insert(scope.instantiable, at: 0)

propagateUndeclaredReceivedProperties(
validateUndeclaredReceivedProperties(
on: childScope,
receivableProperties: receivableProperties.union(scope.properties),
instantiables: instantiables
Expand All @@ -282,7 +269,7 @@ public final class DependencyTreeGenerator {
}

for rootScope in rootInstantiableTypes.compactMap({ typeDescriptionToScopeMap[$0] }) {
propagateUndeclaredReceivedProperties(
validateUndeclaredReceivedProperties(
on: rootScope,
receivableProperties: Set(rootScope.properties),
instantiables: []
Expand Down
195 changes: 0 additions & 195 deletions Sources/SafeDICore/Models/CombinedScope.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Sources/SafeDICore/Models/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import SwiftSyntax

/// A representation of a property.
/// e.g. `let myProperty: MyProperty`
public struct Property: Codable, Hashable, Comparable {
public struct Property: Codable, Hashable, Comparable, Sendable {

// MARK: Initialization

Expand All @@ -39,7 +39,7 @@ public struct Property: Codable, Hashable, Comparable {
/// The label by which the property is referenced.
public let label: String
/// The type to which the property conforms.
public var typeDescription: TypeDescription
public let typeDescription: TypeDescription

// MARK: Hashable

Expand All @@ -61,7 +61,7 @@ public struct Property: Codable, Hashable, Comparable {
)
}

var nonLazyPropertyType: PropertyType {
var propertyType: PropertyType {
switch typeDescription {
case let .simple(name, _):
if name == Dependency.instantiatorType {
Expand Down
Loading

0 comments on commit d9fb05d

Please sign in to comment.