Skip to content

Commit 986f88b

Browse files
committed
implement package-collections business logic
motivation: continue the implmentation of package-collections changes: * implemented PackageCollectionsProtocol which is the main business logic of the feature * added basic configuration model for the feature * added basic validation rules for collection source * simplfied model names to be shorter * added relevants tests
1 parent aaab855 commit 986f88b

18 files changed

+1368
-132
lines changed

Sources/PackageCollections/API.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ public protocol PackageCollectionsProtocol {
3232
/// the ordering of `PackageCollection`s should be preserved and respected during conflict resolution.
3333
///
3434
/// - Parameters:
35-
/// - identifers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
35+
/// - identifiers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
3636
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
3737
/// - callback: The closure to invoke when result becomes available
38-
func listPackageCollections(
39-
identifers: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
38+
func listCollections(
39+
identifiers: Set<PackageCollectionsModel.CollectionIdentifier>?,
4040
in profile: PackageCollectionsModel.Profile?,
41-
callback: @escaping (Result<[PackageCollectionsModel.PackageCollection], Error>) -> Void
41+
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void
4242
)
4343

4444
/// Refreshes all configured package collections.
4545
///
4646
/// - Parameters:
4747
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
4848
/// - callback: The closure to invoke after triggering a refresh for the configured package collections.
49-
func refreshPackageCollections(
49+
func refreshCollections(
5050
in profile: PackageCollectionsModel.Profile?,
51-
callback: @escaping (Result<[PackageCollectionsModel.PackageCollectionSource], Error>) -> Void
51+
callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void
5252
)
5353

5454
/// Adds a package collection.
@@ -59,11 +59,11 @@ public protocol PackageCollectionsProtocol {
5959
/// By default the new collection is appended to the end (i.e., the least relevant order).
6060
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
6161
/// - callback: The closure to invoke with the updated `PackageCollection`s
62-
func addPackageCollection(
63-
_ source: PackageCollectionsModel.PackageCollectionSource,
62+
func addCollection(
63+
_ source: PackageCollectionsModel.CollectionSource,
6464
order: Int?,
6565
to profile: PackageCollectionsModel.Profile?,
66-
callback: @escaping (Result<PackageCollectionsModel.PackageCollection, Error>) -> Void
66+
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
6767
)
6868

6969
/// Removes a package collection.
@@ -72,8 +72,8 @@ public protocol PackageCollectionsProtocol {
7272
/// - source: The package collection's source
7373
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
7474
/// - callback: The closure to invoke with the updated `PackageCollection`s
75-
func removePackageCollection(
76-
_ source: PackageCollectionsModel.PackageCollectionSource,
75+
func removeCollection(
76+
_ source: PackageCollectionsModel.CollectionSource,
7777
from profile: PackageCollectionsModel.Profile?,
7878
callback: @escaping (Result<Void, Error>) -> Void
7979
)
@@ -85,8 +85,8 @@ public protocol PackageCollectionsProtocol {
8585
/// - order: The new order that the `PackageCollection` should be positioned after the move
8686
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
8787
/// - callback: The closure to invoke with the updated `PackageCollection`s
88-
func movePackageCollection(
89-
_ source: PackageCollectionsModel.PackageCollectionSource,
88+
func moveCollection(
89+
_ source: PackageCollectionsModel.CollectionSource,
9090
to order: Int,
9191
in profile: PackageCollectionsModel.Profile?,
9292
callback: @escaping (Result<Void, Error>) -> Void
@@ -98,9 +98,9 @@ public protocol PackageCollectionsProtocol {
9898
/// - Parameters:
9999
/// - source: The package collection's source
100100
/// - callback: The closure to invoke with the `PackageCollection`
101-
func getPackageCollection(
102-
_ source: PackageCollectionsModel.PackageCollectionSource,
103-
callback: @escaping (Result<PackageCollectionsModel.PackageCollection, Error>) -> Void
101+
func getCollection(
102+
_ source: PackageCollectionsModel.CollectionSource,
103+
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
104104
)
105105

106106
// MARK: - Package APIs
@@ -133,7 +133,7 @@ public protocol PackageCollectionsProtocol {
133133
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
134134
/// - callback: The closure to invoke when result becomes available
135135
func listTargets(
136-
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
136+
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
137137
in profile: PackageCollectionsModel.Profile?,
138138
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
139139
)
@@ -152,7 +152,7 @@ public protocol PackageCollectionsProtocol {
152152
/// - callback: The closure to invoke when result becomes available
153153
func findPackages(
154154
_ query: String,
155-
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
155+
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
156156
profile: PackageCollectionsModel.Profile?,
157157
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
158158
)
@@ -172,7 +172,7 @@ public protocol PackageCollectionsProtocol {
172172
func findTargets(
173173
_ query: String,
174174
searchType: PackageCollectionsModel.TargetSearchType?,
175-
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
175+
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
176176
profile: PackageCollectionsModel.Profile?,
177177
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
178178
)

Sources/PackageCollections/Model/PackageSet.swift renamed to Sources/PackageCollections/Model/Collection.swift

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public enum PackageCollectionsModel {}
1919

2020
extension PackageCollectionsModel {
2121
/// A `PackageCollection` is a collection of packages.
22-
public struct PackageCollection: Codable {
23-
public typealias Identifier = PackageCollectionIdentifier
24-
public typealias Source = PackageCollectionSource
22+
public struct Collection: Equatable, Codable {
23+
public typealias Identifier = CollectionIdentifier
24+
public typealias Source = CollectionSource
2525

2626
/// The identifier of the group
2727
public let identifier: Identifier
@@ -57,7 +57,7 @@ extension PackageCollectionsModel {
5757
createdAt: Date,
5858
lastProcessedAt: Date = Date()
5959
) {
60-
self.identifier = .init(source: source)
60+
self.identifier = .init(from: source)
6161
self.source = source
6262
self.name = name
6363
self.description = description
@@ -71,52 +71,57 @@ extension PackageCollectionsModel {
7171

7272
extension PackageCollectionsModel {
7373
/// Represents the source of a `PackageCollection`
74-
public enum PackageCollectionSource: Equatable {
75-
/// Package feed at URL
76-
case feed(URL)
74+
public struct CollectionSource: Equatable, Hashable, Codable {
75+
/// Source type
76+
public let type: CollectionSourceType
77+
78+
/// URL of the source file
79+
public let url: URL
80+
}
81+
82+
/// Represents the source type of a `PackageCollection`
83+
public enum CollectionSourceType: Equatable, CaseIterable {
84+
case feed
7785
}
7886
}
7987

80-
extension PackageCollectionsModel.PackageCollectionSource: Codable {
88+
extension PackageCollectionsModel.CollectionSourceType: Codable {
8189
public enum DiscriminatorKeys: String, Codable {
8290
case feed
8391
}
8492

8593
public enum CodingKeys: CodingKey {
8694
case _case
87-
case url
8895
}
8996

9097
public init(from decoder: Decoder) throws {
9198
let container = try decoder.container(keyedBy: CodingKeys.self)
9299
switch try container.decode(DiscriminatorKeys.self, forKey: ._case) {
93100
case .feed:
94-
let url = try container.decode(URL.self, forKey: .url)
95-
self = .feed(url)
101+
self = .feed
96102
}
97103
}
98104

99105
public func encode(to encoder: Encoder) throws {
100106
var container = encoder.container(keyedBy: CodingKeys.self)
101107
switch self {
102-
case .feed(let url):
108+
case .feed:
103109
try container.encode(DiscriminatorKeys.feed, forKey: ._case)
104-
try container.encode(url, forKey: .url)
105110
}
106111
}
107112
}
108113

109114
extension PackageCollectionsModel {
110115
/// Represents the identifier of a `PackageCollection`
111-
public enum PackageCollectionIdentifier: Hashable, Comparable {
116+
public enum CollectionIdentifier: Hashable, Comparable {
112117
/// Package feed at URL
113118
case feed(URL)
114119

115120
/// Creates an `Identifier` from `Source`
116-
init(source: PackageCollectionSource) {
117-
switch source {
118-
case .feed(let url):
119-
self = .feed(url)
121+
init(from source: CollectionSource) {
122+
switch source.type {
123+
case .feed:
124+
self = .feed(source.url)
120125
}
121126
}
122127

@@ -129,7 +134,7 @@ extension PackageCollectionsModel {
129134
}
130135
}
131136

132-
extension PackageCollectionsModel.PackageCollection.Identifier: Codable {
137+
extension PackageCollectionsModel.CollectionIdentifier: Codable {
133138
public enum DiscriminatorKeys: String, Codable {
134139
case feed
135140
}
@@ -158,9 +163,9 @@ extension PackageCollectionsModel.PackageCollection.Identifier: Codable {
158163
}
159164
}
160165

161-
extension PackageCollectionsModel.PackageCollection {
166+
extension PackageCollectionsModel.Collection {
162167
/// A representation of package metadata
163-
public struct Package: Codable {
168+
public struct Package: Equatable, Codable {
164169
public typealias Version = PackageVersion
165170

166171
/// Package reference
@@ -194,9 +199,9 @@ extension PackageCollectionsModel.PackageCollection {
194199
}
195200
}
196201

197-
extension PackageCollectionsModel.PackageCollection {
202+
extension PackageCollectionsModel.Collection {
198203
/// A representation of package version
199-
public struct PackageVersion: Codable {
204+
public struct PackageVersion: Equatable, Codable {
200205
public typealias Target = PackageCollectionsModel.PackageTarget
201206
public typealias Product = PackageCollectionsModel.PackageProduct
202207

Sources/PackageCollections/Model/License.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import struct Foundation.URL
1212

1313
extension PackageCollectionsModel {
1414
/// A representation of a package license
15-
public struct License: Codable {
15+
public struct License: Equatable, Codable {
1616
/// License type
1717
public let type: LicenseType
1818

@@ -21,7 +21,7 @@ extension PackageCollectionsModel {
2121
}
2222

2323
/// An enum of license types
24-
public enum LicenseType: CaseIterable, CustomStringConvertible {
24+
public enum LicenseType: Equatable, CaseIterable, CustomStringConvertible {
2525
// This list is taken from https://opensource.org/licenses
2626

2727
/// Apache License 2.0

Sources/PackageCollections/Model/Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ extension PackageCollectionsModel.Package {
124124

125125
extension PackageCollectionsModel {
126126
/// A representation of package target
127-
public struct PackageTarget: Codable {
127+
public struct PackageTarget: Equatable, Codable {
128128
/// The target name
129129
public let name: String
130130

@@ -135,7 +135,7 @@ extension PackageCollectionsModel {
135135

136136
extension PackageCollectionsModel {
137137
/// A representation of package product
138-
public struct PackageProduct: Codable {
138+
public struct PackageProduct: Equatable, Codable {
139139
/// The product name
140140
public let name: String
141141

@@ -168,5 +168,5 @@ extension PackageCollectionsModel.Package {
168168
}
169169

170170
extension PackageCollectionsModel {
171-
public typealias PackageMetadata = (package: PackageCollectionsModel.Package, collections: [PackageCollectionsModel.PackageCollectionIdentifier])
171+
public typealias PackageMetadata = (package: PackageCollectionsModel.Package, collections: [PackageCollectionsModel.CollectionIdentifier])
172172
}

Sources/PackageCollections/Model/Search.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension PackageCollectionsModel {
2121
public let package: PackageCollectionsModel.Package
2222

2323
/// Package collections that contain the package
24-
public let collections: [PackageCollectionsModel.PackageCollectionIdentifier]
24+
public let collections: [PackageCollectionsModel.CollectionIdentifier]
2525
}
2626
}
2727

@@ -32,5 +32,8 @@ extension PackageCollectionsModel {
3232
}
3333

3434
/// A representation of target in search result
35-
public typealias TargetSearchResult = TargetListResult
35+
public struct TargetSearchResult {
36+
/// Result items of the search
37+
public let items: TargetListResult
38+
}
3639
}

Sources/PackageCollections/Model/TargetListResult.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import SourceControl
1414
import TSCUtility
1515

1616
extension PackageCollectionsModel {
17-
/// Target metadata
1817
public typealias TargetListResult = [TargetListItem]
1918

2019
public struct TargetListItem {
20+
public typealias Target = PackageCollectionsModel.PackageTarget
21+
public typealias Package = PackageCollectionsModel.TargetListResult.Package
22+
2123
/// Target
22-
public let target: PackageCollectionsModel.PackageTarget
24+
public let target: Target
2325

2426
/// Packages where the target is found
2527
public let packages: [Package]
@@ -28,8 +30,8 @@ extension PackageCollectionsModel {
2830

2931
extension PackageCollectionsModel.TargetListResult {
3032
/// Metadata of package that contains the target
31-
public struct Package {
32-
public typealias Version = PackageVersion
33+
public struct Package: Hashable {
34+
public typealias Version = PackageCollectionsModel.TargetListResult.PackageVersion
3335

3436
/// Package's repository address
3537
public let repository: RepositorySpecifier
@@ -41,13 +43,13 @@ extension PackageCollectionsModel.TargetListResult {
4143
public let versions: [Version]
4244

4345
/// Package collections that contain this package and at least one of the `versions`
44-
public let collections: [PackageCollectionsModel.PackageCollectionIdentifier]
46+
public let collections: [PackageCollectionsModel.CollectionIdentifier]
4547
}
4648
}
4749

4850
extension PackageCollectionsModel.TargetListResult {
4951
/// Represents a package version
50-
public struct PackageVersion {
52+
public struct PackageVersion: Hashable {
5153
/// The version
5254
public let version: TSCUtility.Version
5355

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
// TODO: how do we read default config values? ENV variables? user settings?
12+
extension PackageCollections {
13+
struct Configuration {
14+
let collectionMaximumByteCount: Int = 100_000
15+
let maximumPackagesPerCollection: Int = 50
16+
let maximumVersionsPerPackage: Int = 6 // 2 majors, 3 minors per major
17+
let defaultSources: [PackageCollectionsModel.CollectionSource] = []
18+
}
19+
}

0 commit comments

Comments
 (0)