You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7 KiB

2 years ago
  1. //
  2. // Copyright © 2019 Swinject Contributors. All rights reserved.
  3. //
  4. internal protocol DebugHelper {
  5. func resolutionFailed<Service>(
  6. serviceType: Service.Type,
  7. key: ServiceKey,
  8. availableRegistrations: [ServiceKey: ServiceEntryProtocol]
  9. )
  10. }
  11. internal final class LoggingDebugHelper: DebugHelper {
  12. func resolutionFailed<Service>(
  13. serviceType: Service.Type,
  14. key: ServiceKey,
  15. availableRegistrations: [ServiceKey: ServiceEntryProtocol]
  16. ) {
  17. var output = [
  18. "Swinject: Resolution failed. Expected registration:",
  19. "\t{ \(description(serviceType: serviceType, serviceKey: key)) }",
  20. "Available registrations:",
  21. ]
  22. output += availableRegistrations
  23. .filter { $0.1 is ServiceEntry<Service> }
  24. .map { "\t{ " + $0.1.describeWithKey($0.0) + " }" }
  25. Container.log(output.joined(separator: "\n"))
  26. }
  27. }
  28. internal func description(
  29. serviceType: Any.Type,
  30. serviceKey: ServiceKey,
  31. objectScope: ObjectScopeProtocol? = nil,
  32. initCompleted: [Any] = []
  33. ) -> String {
  34. // The protocol order in "protocol<>" is non-deterministic.
  35. let nameDescription = serviceKey.name.map { ", Name: \"\($0)\"" } ?? ""
  36. let optionDescription = serviceKey.option.map { ", \($0)" } ?? ""
  37. let initCompletedDescription = initCompleted.isEmpty ?
  38. "" : ", InitCompleted: Specified \(initCompleted.count) closures"
  39. let objectScopeDescription = objectScope.map { ", ObjectScope: \($0)" } ?? ""
  40. return "Service: \(serviceType)"
  41. + nameDescription
  42. + optionDescription
  43. + ", Factory: \(serviceKey.argumentsType) -> \(serviceKey.serviceType)"
  44. + objectScopeDescription
  45. + initCompletedDescription
  46. }