Skip to content

implement package-collections business logic #3028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions Sources/PackageCollections/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ public protocol PackageCollectionsProtocol {
/// the ordering of `PackageCollection`s should be preserved and respected during conflict resolution.
///
/// - Parameters:
/// - identifers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
/// - identifiers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke when result becomes available
func listPackageCollections(
identifers: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
func listCollections(
identifiers: Set<PackageCollectionsModel.CollectionIdentifier>?,
in profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<[PackageCollectionsModel.PackageCollection], Error>) -> Void
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void
)

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

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

/// Removes a package collection.
Expand All @@ -72,8 +72,8 @@ public protocol PackageCollectionsProtocol {
/// - source: The package collection's source
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke with the updated `PackageCollection`s
func removePackageCollection(
_ source: PackageCollectionsModel.PackageCollectionSource,
func removeCollection(
_ source: PackageCollectionsModel.CollectionSource,
from profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<Void, Error>) -> Void
)
Expand All @@ -85,8 +85,8 @@ public protocol PackageCollectionsProtocol {
/// - order: The new order that the `PackageCollection` should be positioned after the move
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke with the updated `PackageCollection`s
func movePackageCollection(
_ source: PackageCollectionsModel.PackageCollectionSource,
func moveCollection(
_ source: PackageCollectionsModel.CollectionSource,
to order: Int,
in profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<Void, Error>) -> Void
Expand All @@ -98,9 +98,9 @@ public protocol PackageCollectionsProtocol {
/// - Parameters:
/// - source: The package collection's source
/// - callback: The closure to invoke with the `PackageCollection`
func getPackageCollection(
_ source: PackageCollectionsModel.PackageCollectionSource,
callback: @escaping (Result<PackageCollectionsModel.PackageCollection, Error>) -> Void
func getCollection(
_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
)

// MARK: - Package APIs
Expand Down Expand Up @@ -133,7 +133,7 @@ public protocol PackageCollectionsProtocol {
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke when result becomes available
func listTargets(
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
in profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
)
Expand All @@ -152,7 +152,7 @@ public protocol PackageCollectionsProtocol {
/// - callback: The closure to invoke when result becomes available
func findPackages(
_ query: String,
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
)
Expand All @@ -172,7 +172,7 @@ public protocol PackageCollectionsProtocol {
func findTargets(
_ query: String,
searchType: PackageCollectionsModel.TargetSearchType?,
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
)
Expand Down
40 changes: 40 additions & 0 deletions Sources/PackageCollections/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2020 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_library(PackageCollections
Model/CVE.swift
Model/Collection.swift
Model/License.swift
Model/Package.swift
Model/Profile.swift
Model/Search.swift
Model/TargetListResult.swift
PackageCollectionProvider.swift
PackageCollections+Configuration.swift
PackageCollections+Storage.swift
PackageCollections+Vallidation.swift
PackageCollections.swift
Storage/PackageCollectionsProfileStorage.swift
Storage/PackageCollectionsStorage.swift
Utility.swift)
target_link_libraries(PackageCollections PUBLIC
TSCBasic
TSCUtility
PackageModel
SourceControl)
# NOTE(compnerd) workaround for CMake not setting up include flags yet
set_target_properties(PackageCollections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

if(USE_CMAKE_INSTALL)
install(TARGETS PackageCollections
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
endif()
set_property(GLOBAL APPEND PROPERTY SwiftPM_EXPORTS PackageCollections)
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public enum PackageCollectionsModel {}

extension PackageCollectionsModel {
/// A `PackageCollection` is a collection of packages.
public struct PackageCollection: Codable {
public typealias Identifier = PackageCollectionIdentifier
public typealias Source = PackageCollectionSource
public struct Collection: Equatable, Codable {
public typealias Identifier = CollectionIdentifier
public typealias Source = CollectionSource

/// The identifier of the group
public let identifier: Identifier
Expand Down Expand Up @@ -57,7 +57,7 @@ extension PackageCollectionsModel {
createdAt: Date,
lastProcessedAt: Date = Date()
) {
self.identifier = .init(source: source)
self.identifier = .init(from: source)
self.source = source
self.name = name
self.description = description
Expand All @@ -71,52 +71,69 @@ extension PackageCollectionsModel {

extension PackageCollectionsModel {
/// Represents the source of a `PackageCollection`
public enum PackageCollectionSource: Equatable {
/// Package feed at URL
case feed(URL)
public struct CollectionSource: Equatable, Hashable, Codable {
/// Source type
public let type: CollectionSourceType

/// URL of the source file
public let url: URL

public init(type: CollectionSourceType, url: URL) {
self.type = type
self.url = url
}
}
}

extension PackageCollectionsModel.PackageCollectionSource: Codable {
public enum DiscriminatorKeys: String, Codable {
/// Represents the source type of a `PackageCollection`
public enum CollectionSourceType: Equatable, CaseIterable {
case feed
}
}

extension PackageCollectionsModel.CollectionSourceType: Codable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the whole purpose of this just to be able to throw UnknownType error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yim-lee are you asking about the purpose of CollectionSourceType? If so, it for us to be able to support multiple type of providers. e.g. feed, index, etc

Copy link
Contributor

@yim-lee yim-lee Nov 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use synthesized conformance and remove this extension block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I dont believe enums synthesize conformance. am I missing anything?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could if CollectionSourceType: String, Codable but not sure we want to add : String though.

public enum CodingKeys: CodingKey {
case _case
case url
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
switch try container.decode(DiscriminatorKeys.self, forKey: ._case) {
case .feed:
let url = try container.decode(URL.self, forKey: .url)
self = .feed(url)
let value = try container.decode(String.self, forKey: ._case)
switch value {
case "feed":
self = .feed
default:
throw UnknownType(value)
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .feed(let url):
try container.encode(DiscriminatorKeys.feed, forKey: ._case)
try container.encode(url, forKey: .url)
case .feed:
try container.encode("feed", forKey: ._case)
}
}

struct UnknownType: Error {
let type: String

init(_ type: String) {
self.type = type
}
}
}

extension PackageCollectionsModel {
/// Represents the identifier of a `PackageCollection`
public enum PackageCollectionIdentifier: Hashable, Comparable {
public enum CollectionIdentifier: Hashable, Comparable {
/// Package feed at URL
case feed(URL)

/// Creates an `Identifier` from `Source`
init(source: PackageCollectionSource) {
switch source {
case .feed(let url):
self = .feed(url)
init(from source: CollectionSource) {
switch source.type {
case .feed:
self = .feed(source.url)
}
}

Expand All @@ -129,7 +146,7 @@ extension PackageCollectionsModel {
}
}

extension PackageCollectionsModel.PackageCollection.Identifier: Codable {
extension PackageCollectionsModel.CollectionIdentifier: Codable {
public enum DiscriminatorKeys: String, Codable {
case feed
}
Expand Down Expand Up @@ -158,9 +175,9 @@ extension PackageCollectionsModel.PackageCollection.Identifier: Codable {
}
}

extension PackageCollectionsModel.PackageCollection {
extension PackageCollectionsModel.Collection {
/// A representation of package metadata
public struct Package: Codable {
public struct Package: Equatable, Codable {
public typealias Version = PackageVersion

/// Package reference
Expand Down Expand Up @@ -194,9 +211,9 @@ extension PackageCollectionsModel.PackageCollection {
}
}

extension PackageCollectionsModel.PackageCollection {
extension PackageCollectionsModel.Collection {
/// A representation of package version
public struct PackageVersion: Codable {
public struct PackageVersion: Equatable, Codable {
public typealias Target = PackageCollectionsModel.PackageTarget
public typealias Product = PackageCollectionsModel.PackageProduct

Expand Down
4 changes: 2 additions & 2 deletions Sources/PackageCollections/Model/License.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import struct Foundation.URL

extension PackageCollectionsModel {
/// A representation of a package license
public struct License: Codable {
public struct License: Equatable, Codable {
/// License type
public let type: LicenseType

Expand All @@ -21,7 +21,7 @@ extension PackageCollectionsModel {
}

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

/// Apache License 2.0
Expand Down
6 changes: 3 additions & 3 deletions Sources/PackageCollections/Model/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ extension PackageCollectionsModel.Package {

extension PackageCollectionsModel {
/// A representation of package target
public struct PackageTarget: Codable {
public struct PackageTarget: Equatable, Codable {
/// The target name
public let name: String

Expand All @@ -135,7 +135,7 @@ extension PackageCollectionsModel {

extension PackageCollectionsModel {
/// A representation of package product
public struct PackageProduct: Codable {
public struct PackageProduct: Equatable, Codable {
/// The product name
public let name: String

Expand Down Expand Up @@ -168,5 +168,5 @@ extension PackageCollectionsModel.Package {
}

extension PackageCollectionsModel {
public typealias PackageMetadata = (package: PackageCollectionsModel.Package, collections: [PackageCollectionsModel.PackageCollectionIdentifier])
public typealias PackageMetadata = (package: PackageCollectionsModel.Package, collections: [PackageCollectionsModel.CollectionIdentifier])
}
7 changes: 5 additions & 2 deletions Sources/PackageCollections/Model/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension PackageCollectionsModel {
public let package: PackageCollectionsModel.Package

/// Package collections that contain the package
public let collections: [PackageCollectionsModel.PackageCollectionIdentifier]
public let collections: [PackageCollectionsModel.CollectionIdentifier]
}
}

Expand All @@ -32,5 +32,8 @@ extension PackageCollectionsModel {
}

/// A representation of target in search result
public typealias TargetSearchResult = TargetListResult
public struct TargetSearchResult {
/// Result items of the search
public let items: TargetListResult
}
}
14 changes: 8 additions & 6 deletions Sources/PackageCollections/Model/TargetListResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import SourceControl
import TSCUtility

extension PackageCollectionsModel {
/// Target metadata
public typealias TargetListResult = [TargetListItem]

public struct TargetListItem {
public typealias Target = PackageCollectionsModel.PackageTarget
public typealias Package = PackageCollectionsModel.TargetListResult.Package

/// Target
public let target: PackageCollectionsModel.PackageTarget
public let target: Target

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

extension PackageCollectionsModel.TargetListResult {
/// Metadata of package that contains the target
public struct Package {
public typealias Version = PackageVersion
public struct Package: Hashable {
public typealias Version = PackageCollectionsModel.TargetListResult.PackageVersion

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

/// Package collections that contain this package and at least one of the `versions`
public let collections: [PackageCollectionsModel.PackageCollectionIdentifier]
public let collections: [PackageCollectionsModel.CollectionIdentifier]
}
}

extension PackageCollectionsModel.TargetListResult {
/// Represents a package version
public struct PackageVersion {
public struct PackageVersion: Hashable {
/// The version
public let version: TSCUtility.Version

Expand Down
Loading