Skip to content

Commit

Permalink
Improve cycle log. Fix Cycle validation bug, if one object register w…
Browse files Browse the repository at this point in the history
…ithout initial method
  • Loading branch information
ivlevAstef committed Oct 2, 2017
1 parent 9d23e7c commit 29d8e10
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v3.0.4
* bugfix: Improve validate graph cycle, and logs.

# v3.0.3
* bugfix: Support recursive inject into ViewControllers.
* bugfix: Fix component bundle source.
Expand Down
2 changes: 1 addition & 1 deletion DITranquillity.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'DITranquillity'
s.version = '3.0.3'
s.version = '3.0.4'
s.summary = 'DITranquillity - Dependency injection for iOS/macOS/tvOS (Swift) '

s.description = <<-DESC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ private class RInit3Inject {
var test: RInit1!
}

private class RCycle {
init(_ two: RCycle2) {}
}
private class RCycle2 {
var inject: RCycle!
}


class DITranquillityTests_Build: XCTestCase {
Expand Down Expand Up @@ -253,4 +259,17 @@ class DITranquillityTests_Build: XCTestCase {

XCTAssert(!container.validate())
}

func test06_CycleWithoutInit() {
let container = DIContainer()

container.register(RCycle.init)
.lifetime(.prototype)

container.register(RCycle2.self)
.injection{ $0.inject = $1 }
.lifetime(.objectGraph)

XCTAssert(!container.validate())
}
}
18 changes: 11 additions & 7 deletions Sources/Core/Public/DIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,29 +260,30 @@ extension DIContainer {
return true
}

let infos = stack.map{ $0.component.info }
let infos = stack.dropLast().map{ $0.component.info }
let short = infos.map{ "\($0.type)" }.joined(separator: " - ")

let allInitials = !stack.contains{ !($0.initial && !$0.many) }
if allInitials {
log(.error, msg: "You have a cycle: \(infos) consisting entirely of initialization methods.")
log(.error, msg: "You have a cycle: \(short) consisting entirely of initialization methods. Full: \(infos)")
return false
}

let hasGap = stack.contains{ $0.cycle || ($0.initial && $0.many) }
if !hasGap {
log(.error, msg: "Cycle has no discontinuities. Please install at least one explosion in the cycle: \(infos) using `injection(cycle: true) { ... }`")
log(.error, msg: "Cycle has no discontinuities. Please install at least one explosion in the cycle: \(short) using `injection(cycle: true) { ... }`. Full: \(infos)")
return false
}

let allPrototypes = !stack.contains{ $0.component.lifeTime != .prototype }
if allPrototypes {
log(.error, msg: "You cycle: \(infos) consists only of object with lifetime - prototype. Please change at least one object lifetime to another.")
log(.error, msg: "You cycle: \(short) consists only of object with lifetime - prototype. Please change at least one object lifetime to another. Full: \(infos)")
return false
}

let containsPrototype = stack.contains{ $0.component.lifeTime == .prototype }
if containsPrototype {
log(.warning, msg: "You cycle: \(infos) contains an object with lifetime - prototype. In some cases this can lead to an udesirable effect.")
log(.warning, msg: "You cycle: \(short) contains an object with lifetime - prototype. In some cases this can lead to an udesirable effect. Full: \(infos)")
}

return true
Expand All @@ -300,10 +301,13 @@ extension DIContainer {

func callDfs(by parameters: [MethodSignature.Parameter], initial: Bool, cycle: Bool) {
for parameter in parameters {
let many = parameter.many
let candidates = resolver.findComponents(by: parameter.type, with: parameter.name, from: bundle)
let filtered = resolver.removeWhoDoesNotHaveInitialMethod(components: candidates)
if candidates.isEmpty {
continue
}

let filtered = candidates.filter{ nil != $0.initial || ($0.lifeTime != .prototype && visited.contains($0)) }
let many = parameter.many
for subcomponent in filtered {
var stack = stack
stack.append((subcomponent, initial, cycle, many))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.0.3</string>
<string>3.0.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down

0 comments on commit 29d8e10

Please sign in to comment.