Skip to content

Commit b2a0504

Browse files
authored
Merge pull request #314 from cashapp/skorulis/parameter-packs-17
Convert SwinjectContainer generated argument registration functions to use parameter packs
2 parents 61849dd + b81a4d4 commit b2a0504

4 files changed

Lines changed: 28 additions & 226 deletions

File tree

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import PackageDescription
77
let package = Package(
88
name: "Knit",
99
platforms: [
10-
.macOS(.v14),
11-
.iOS(.v16),
10+
.macOS(.v15),
11+
.iOS(.v17),
1212
],
1313
products: [
1414
.library(name: "Knit", targets: ["Knit"]),

Sources/Swinject/SwinjectContainer.Arguments.erb

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,6 @@
1515

1616
import Foundation
1717

18-
// MARK: - Registration with Arguments
19-
20-
extension SwinjectContainer {
21-
<% (1..arg_count).each do |i| %>
22-
<% arg_types = (1..i).map { |n| "Arg#{n}" }.join(", ") %>
23-
<% arg_description = i == 1 ? "#{i} argument" : "#{i} arguments" %>
24-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
25-
///
26-
/// - Parameters:
27-
/// - serviceType: The service type to register.
28-
/// - name: A registration name, which is used to differentiate from other registrations
29-
/// that have the same service and factory types.
30-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
31-
/// It is invoked when the `Container` needs to instantiate the instance.
32-
/// It takes a `SwinjectResolver` instance and <%= arg_description %> to inject dependencies to the instance,
33-
/// and returns the instance of the component type for the service.
34-
///
35-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
36-
@discardableResult
37-
public func register<Service, <%= arg_types %>>(
38-
_ serviceType: Service.Type,
39-
name: String? = nil,
40-
factory: @escaping (SwinjectResolver, <%= arg_types %>) -> Service
41-
) -> ServiceEntry<Service> {
42-
return _register(serviceType, factory: factory, name: name)
43-
}
44-
45-
<% end %>
46-
}
47-
4818
// MARK: - SwinjectResolver with Arguments
4919

5020
extension SwinjectContainer {

Sources/Swinject/SwinjectContainer.Arguments.swift

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -14,200 +14,6 @@
1414

1515
import Foundation
1616

17-
// MARK: - Registration with Arguments
18-
19-
extension SwinjectContainer {
20-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
21-
///
22-
/// - Parameters:
23-
/// - serviceType: The service type to register.
24-
/// - name: A registration name, which is used to differentiate from other registrations
25-
/// that have the same service and factory types.
26-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
27-
/// It is invoked when the `Container` needs to instantiate the instance.
28-
/// It takes a `SwinjectResolver` instance and 1 argument to inject dependencies to the instance,
29-
/// and returns the instance of the component type for the service.
30-
///
31-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
32-
@discardableResult
33-
public func register<Service, Arg1>(
34-
_ serviceType: Service.Type,
35-
name: String? = nil,
36-
factory: @escaping (SwinjectResolver, Arg1) -> Service
37-
) -> ServiceEntry<Service> {
38-
return _register(serviceType, factory: factory, name: name)
39-
}
40-
41-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
42-
///
43-
/// - Parameters:
44-
/// - serviceType: The service type to register.
45-
/// - name: A registration name, which is used to differentiate from other registrations
46-
/// that have the same service and factory types.
47-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
48-
/// It is invoked when the `Container` needs to instantiate the instance.
49-
/// It takes a `SwinjectResolver` instance and 2 arguments to inject dependencies to the instance,
50-
/// and returns the instance of the component type for the service.
51-
///
52-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
53-
@discardableResult
54-
public func register<Service, Arg1, Arg2>(
55-
_ serviceType: Service.Type,
56-
name: String? = nil,
57-
factory: @escaping (SwinjectResolver, Arg1, Arg2) -> Service
58-
) -> ServiceEntry<Service> {
59-
return _register(serviceType, factory: factory, name: name)
60-
}
61-
62-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
63-
///
64-
/// - Parameters:
65-
/// - serviceType: The service type to register.
66-
/// - name: A registration name, which is used to differentiate from other registrations
67-
/// that have the same service and factory types.
68-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
69-
/// It is invoked when the `Container` needs to instantiate the instance.
70-
/// It takes a `SwinjectResolver` instance and 3 arguments to inject dependencies to the instance,
71-
/// and returns the instance of the component type for the service.
72-
///
73-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
74-
@discardableResult
75-
public func register<Service, Arg1, Arg2, Arg3>(
76-
_ serviceType: Service.Type,
77-
name: String? = nil,
78-
factory: @escaping (SwinjectResolver, Arg1, Arg2, Arg3) -> Service
79-
) -> ServiceEntry<Service> {
80-
return _register(serviceType, factory: factory, name: name)
81-
}
82-
83-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
84-
///
85-
/// - Parameters:
86-
/// - serviceType: The service type to register.
87-
/// - name: A registration name, which is used to differentiate from other registrations
88-
/// that have the same service and factory types.
89-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
90-
/// It is invoked when the `Container` needs to instantiate the instance.
91-
/// It takes a `SwinjectResolver` instance and 4 arguments to inject dependencies to the instance,
92-
/// and returns the instance of the component type for the service.
93-
///
94-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
95-
@discardableResult
96-
public func register<Service, Arg1, Arg2, Arg3, Arg4>(
97-
_ serviceType: Service.Type,
98-
name: String? = nil,
99-
factory: @escaping (SwinjectResolver, Arg1, Arg2, Arg3, Arg4) -> Service
100-
) -> ServiceEntry<Service> {
101-
return _register(serviceType, factory: factory, name: name)
102-
}
103-
104-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
105-
///
106-
/// - Parameters:
107-
/// - serviceType: The service type to register.
108-
/// - name: A registration name, which is used to differentiate from other registrations
109-
/// that have the same service and factory types.
110-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
111-
/// It is invoked when the `Container` needs to instantiate the instance.
112-
/// It takes a `SwinjectResolver` instance and 5 arguments to inject dependencies to the instance,
113-
/// and returns the instance of the component type for the service.
114-
///
115-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
116-
@discardableResult
117-
public func register<Service, Arg1, Arg2, Arg3, Arg4, Arg5>(
118-
_ serviceType: Service.Type,
119-
name: String? = nil,
120-
factory: @escaping (SwinjectResolver, Arg1, Arg2, Arg3, Arg4, Arg5) -> Service
121-
) -> ServiceEntry<Service> {
122-
return _register(serviceType, factory: factory, name: name)
123-
}
124-
125-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
126-
///
127-
/// - Parameters:
128-
/// - serviceType: The service type to register.
129-
/// - name: A registration name, which is used to differentiate from other registrations
130-
/// that have the same service and factory types.
131-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
132-
/// It is invoked when the `Container` needs to instantiate the instance.
133-
/// It takes a `SwinjectResolver` instance and 6 arguments to inject dependencies to the instance,
134-
/// and returns the instance of the component type for the service.
135-
///
136-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
137-
@discardableResult
138-
public func register<Service, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(
139-
_ serviceType: Service.Type,
140-
name: String? = nil,
141-
factory: @escaping (SwinjectResolver, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) -> Service
142-
) -> ServiceEntry<Service> {
143-
return _register(serviceType, factory: factory, name: name)
144-
}
145-
146-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
147-
///
148-
/// - Parameters:
149-
/// - serviceType: The service type to register.
150-
/// - name: A registration name, which is used to differentiate from other registrations
151-
/// that have the same service and factory types.
152-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
153-
/// It is invoked when the `Container` needs to instantiate the instance.
154-
/// It takes a `SwinjectResolver` instance and 7 arguments to inject dependencies to the instance,
155-
/// and returns the instance of the component type for the service.
156-
///
157-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
158-
@discardableResult
159-
public func register<Service, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(
160-
_ serviceType: Service.Type,
161-
name: String? = nil,
162-
factory: @escaping (SwinjectResolver, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) -> Service
163-
) -> ServiceEntry<Service> {
164-
return _register(serviceType, factory: factory, name: name)
165-
}
166-
167-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
168-
///
169-
/// - Parameters:
170-
/// - serviceType: The service type to register.
171-
/// - name: A registration name, which is used to differentiate from other registrations
172-
/// that have the same service and factory types.
173-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
174-
/// It is invoked when the `Container` needs to instantiate the instance.
175-
/// It takes a `SwinjectResolver` instance and 8 arguments to inject dependencies to the instance,
176-
/// and returns the instance of the component type for the service.
177-
///
178-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
179-
@discardableResult
180-
public func register<Service, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8>(
181-
_ serviceType: Service.Type,
182-
name: String? = nil,
183-
factory: @escaping (SwinjectResolver, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8) -> Service
184-
) -> ServiceEntry<Service> {
185-
return _register(serviceType, factory: factory, name: name)
186-
}
187-
188-
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
189-
///
190-
/// - Parameters:
191-
/// - serviceType: The service type to register.
192-
/// - name: A registration name, which is used to differentiate from other registrations
193-
/// that have the same service and factory types.
194-
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
195-
/// It is invoked when the `Container` needs to instantiate the instance.
196-
/// It takes a `SwinjectResolver` instance and 9 arguments to inject dependencies to the instance,
197-
/// and returns the instance of the component type for the service.
198-
///
199-
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
200-
@discardableResult
201-
public func register<Service, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9>(
202-
_ serviceType: Service.Type,
203-
name: String? = nil,
204-
factory: @escaping (SwinjectResolver, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9) -> Service
205-
) -> ServiceEntry<Service> {
206-
return _register(serviceType, factory: factory, name: name)
207-
}
208-
209-
}
210-
21117
// MARK: - SwinjectResolver with Arguments
21218

21319
extension SwinjectContainer {

Sources/Swinject/SwinjectContainer.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,29 @@ extension SwinjectContainer: CustomStringConvertible {
427427
private extension SwinjectContainer {
428428
static let graphIdentifierKey = ServiceKey(serviceType: GraphIdentifier.self, argumentsType: SwinjectResolver.self)
429429
}
430+
431+
432+
// MARK: Registration
433+
434+
extension SwinjectContainer {
435+
436+
/// Adds a registration for the specified service with the factory closure to specify how the service is resolved with dependencies.
437+
///
438+
/// - Parameters:
439+
/// - serviceType: The service type to register.
440+
/// - name: A registration name, which is used to differentiate from other registrations
441+
/// that have the same service and factory types.
442+
/// - factory: The closure to specify how the service type is resolved with the dependencies of the type.
443+
/// It is invoked when the ``Container`` needs to instantiate the instance.
444+
/// It takes a ``Resolver`` instance and 1 argument to inject dependencies to the instance,
445+
/// and returns the instance of the component type for the service.
446+
///
447+
/// - Returns: A registered `ServiceEntry` to configure more settings with method chaining.
448+
@discardableResult public func register<Service, FirstArgument, each ArgumentType>(
449+
_ serviceType: Service.Type,
450+
name: String? = nil,
451+
factory: @escaping (SwinjectResolver, FirstArgument, repeat each ArgumentType) -> Service
452+
) -> ServiceEntry<Service> {
453+
return _register(serviceType, factory: factory, name: name)
454+
}
455+
}

0 commit comments

Comments
 (0)