Skip to content

Commit

Permalink
Fixed a bug where a dependency container with only registrations wasn…
Browse files Browse the repository at this point in the history
…'t injected properly into its associated type
  • Loading branch information
trupin committed Sep 24, 2018
1 parent 5aaf130 commit e5eadb8
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 78 deletions.
22 changes: 6 additions & 16 deletions Resources/dependency_resolver.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
{% endfor %}
{% endmacro %}

{% macro DependencyContainerBuilder dependency %}{{dependency.type.name}}DependencyContainer(injecting: self{% call ParametersPassInWithSeparator dependency.parameters %}){% endmacro %}

{% macro AttributesDecl dependencies %}
{% for dependency in dependencies %}
let {{dependency.name}}: {{dependency.abstractType}}
Expand Down Expand Up @@ -89,10 +87,10 @@ final class {{dependencyContainer.targetType.name}}DependencyContainer{{dependen
{% if not registration.customRef %}

{% if registration.hasBuilder %}
let dependencies = {% call DependencyContainerBuilder registration %}
let dependencies = {{registration.type.name}}DependencyContainer({% if registration.hasReferences %}injecting: self{% if registration.parameters %}, {% endif %}{% endif %}{% call ParametersPassIn registration.parameters %})
let value = {{registration.type}}(injecting: dependencies)
{% else %}{# registration.hasBuilder #}
let value = {{registration.type}}()
let value = {{registration.type}}({% if registration.hasDependencyContainer %}injecting: {{registration.type.name}}DependencyContainer{{registration.type.generics}}(){% endif %})
{% endif %}{# registration.hasBuilder #}

{% else %}{# not registration.customRef #}
Expand Down Expand Up @@ -138,21 +136,13 @@ final class {{dependencyContainer.targetType.name}}DependencyContainer{{dependen
}

{% for injectableDependency in dependencyContainer.injectableDependencies %}
{% if injectableDependency.references %}
extension {{dependencyContainer.targetType.name}}DependencyContainer: {{injectableDependency.targetType.name}}InputDependencyResolver {}
{% endif %}
{% endfor %}

{% if not dependencyContainer.isRoot %}
protocol {{dependencyContainer.targetType.name}}{% if dependencyContainer.doesSupportObjc %}ObjC{% endif %}DependencyInjectable {
{% for genericName in dependencyContainer.targetType.genericNames %}
associatedtype {{genericName}}
{% endfor %}

{% if dependencyContainer.targetType.genericNames %}
init(injecting dependencies: {{dependencyContainer.targetType.name}}DependencyContainer{{dependencyContainer.targetType.generics}})
{% else %}
init(injecting dependencies: {{dependencyContainer.targetType.name}}DependencyResolver)
{% endif %}
}
{% if dependencyContainer.doesSupportObjc %}
protocol {{dependencyContainer.targetType.name}}ObjCDependencyInjectable {}
{% endif %}

{% if dependencyContainer.isPublic %}
Expand Down
3 changes: 0 additions & 3 deletions Sample/API/Generated/Weaver.API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ final class MovieAPIDependencyContainer: MovieAPIDependencyResolver {
_ = logger
}
}
protocol MovieAPIDependencyInjectable {
init(injecting dependencies: MovieAPIDependencyResolver)
}
final class MovieAPIShimDependencyContainer: MovieAPIInputDependencyResolver {
let urlSession: URLSession
init(urlSession: URLSession) {
Expand Down
3 changes: 0 additions & 3 deletions Sample/API/Generated/Weaver.MovieManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ final class MovieManagerDependencyContainer: MovieManagerDependencyResolver {
}
}
extension MovieManagerDependencyContainer: MovieAPIInputDependencyResolver {}
protocol MovieManagerDependencyInjectable {
init(injecting dependencies: MovieManagerDependencyResolver)
}
final class MovieManagerShimDependencyContainer: MovieManagerInputDependencyResolver {
let logger: Logger
init(logger: Logger) {
Expand Down
5 changes: 1 addition & 4 deletions Sample/Sample/Generated/Weaver.HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,4 @@ final class HomeViewControllerDependencyContainer: HomeViewControllerDependencyR
_ = logger
}
}
extension HomeViewControllerDependencyContainer: MovieViewControllerInputDependencyResolver {}
protocol HomeViewControllerDependencyInjectable {
init(injecting dependencies: HomeViewControllerDependencyResolver)
}
extension HomeViewControllerDependencyContainer: MovieViewControllerInputDependencyResolver {}
5 changes: 1 addition & 4 deletions Sample/Sample/Generated/Weaver.MovieViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,4 @@ final class MovieViewControllerDependencyContainer: MovieViewControllerDependenc
_ = reviewController(movieID: movieID)
}
}
extension MovieViewControllerDependencyContainer: WSReviewViewControllerInputDependencyResolver {}
protocol MovieViewControllerDependencyInjectable {
init(injecting dependencies: MovieViewControllerDependencyResolver)
}
extension MovieViewControllerDependencyContainer: WSReviewViewControllerInputDependencyResolver {}
3 changes: 0 additions & 3 deletions Sample/Sample/Generated/Weaver.PersonManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ final class PersonManagerDependencyContainer: PersonManagerDependencyResolver {
movieAPI = dependencies.movieAPI
_ = logger
}
}
protocol PersonManagerDependencyInjectable {
init(injecting dependencies: PersonManagerDependencyResolver)
}
3 changes: 0 additions & 3 deletions Sample/Sample/Generated/Weaver.ReviewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ final class ReviewManagerDependencyContainer: ReviewManagerDependencyResolver {
movieAPI = dependencies.movieAPI
_ = logger
}
}
protocol ReviewManagerDependencyInjectable {
init(injecting dependencies: ReviewManagerDependencyResolver)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ final class WSReviewViewControllerDependencyContainer: NSObject, WSReviewViewCon
super.init()
}
}
protocol WSReviewViewControllerObjCDependencyInjectable {
init(injecting dependencies: WSReviewViewControllerDependencyResolver)
}
protocol WSReviewViewControllerObjCDependencyInjectable {}
20 changes: 13 additions & 7 deletions Sources/WeaverCodeGen/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ private struct RegistrationViewModel {
let scope: String
let customRef: Bool
let parameters: [DependencyViewModel]
let hasReferences: Bool
let hasBuilder: Bool
let hasDependencyContainer: Bool
let isTransient: Bool
let isWeak: Bool

Expand All @@ -72,12 +74,16 @@ private struct RegistrationViewModel {

if let dependencyContainer = dependencyGraph.dependencyContainersByType[dependency.type.index] {
parameters = dependencyContainer.parameters.map { DependencyViewModel($0, dependencyGraph: dependencyGraph) }
hasBuilder = !dependencyContainer.parameters.isEmpty || !dependencyContainer.references.orderedKeys.isEmpty
hasReferences = !dependencyContainer.allReferences.isEmpty
hasBuilder = dependencyContainer.hasBuilder
hasDependencyContainer = dependencyContainer.hasDependencies
} else {
parameters = []
hasReferences = false
hasBuilder = false
hasDependencyContainer = false
}

isTransient = dependency.scope == .transient
isWeak = dependency.scope == .weak
}
Expand Down Expand Up @@ -183,11 +189,11 @@ private extension DependencyContainer {
}

var isRoot: Bool {
let hasNonCustomReferences = references.orderedValues.contains {
!$0.configuration.customRef
}
let hasParameters = !parameters.isEmpty
return !hasNonCustomReferences && !hasParameters
return sources.isEmpty
}

var hasBuilder: Bool {
return !parameters.isEmpty || !allReferences.isEmpty
}

var isPublic: Bool {
Expand Down
Loading

0 comments on commit e5eadb8

Please sign in to comment.