Skip to content

Update JSON package collection models #3067

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 3 commits into from
Nov 20, 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
13 changes: 10 additions & 3 deletions Fixtures/Collections/JSON/good.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
"name": "Sample Package Collection",
"description": "This is a sample package collection listing made-up packages.",
"overview": "This is a sample package collection listing made-up packages.",
"keywords": ["sample package collection"],
"formatVersion": "1.0",
"revision": 3,
"generatedAt": "2020-10-22T06:03:52Z",
"generatedBy": {
"name": "Jane Doe"
},
"packages": [
{
"url": "https://www.example.com/repos/RepoOne.git",
"description": "Package One",
"summary": "Package One",
"keywords": ["sample package"],
"readmeURL": "https://www.example.com/repos/RepoOne/README",
"versions": [
{
Expand All @@ -30,6 +34,9 @@
}
],
"toolsVersion": "5.1",
"minimumPlatformVersions": [
{ "name": "macOS", "version": "10.15" }
],
"verifiedPlatforms": [
{ "name": "macOS" },
{ "name": "iOS" },
Expand All @@ -45,7 +52,7 @@
},
{
"url": "https://www.example.com/repos/RepoTwo.git",
"description": "Package Two",
"summary": "Package Two",
"readmeURL": "https://www.example.com/repos/RepoTwo/README",
"versions": [
{
Expand Down
4 changes: 3 additions & 1 deletion Sources/PackageCollections/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_library(PackageCollections
Model/CVE.swift
JSONModel/JSONCollection.swift
JSONModel/JSONCollection+v1.swift
Model/Collection.swift
Model/CVE.swift
Model/License.swift
Model/Package.swift
Model/Search.swift
Expand Down
243 changes: 243 additions & 0 deletions Sources/PackageCollections/JSONModel/JSONCollection+v1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*
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
*/

import struct Foundation.Date
import struct Foundation.URL

import PackageModel

extension JSONPackageCollectionModel {
public enum V1 {}
}

extension JSONPackageCollectionModel.V1 {
public struct Collection: Equatable, Codable {
/// The name of the package collection, for display purposes only.
public let name: String

/// A description of the package collection.
public let overview: String?

/// An array of keywords that the collection is associated with.
public let keywords: [String]?
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this optional so it doesn't get coded at all if empty, or is there a semantic difference? (noting for example that packages below isn't optional — though admittedly we don't expect to have no packages the way there might be no keywords)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is optional so it doesn't get coded if nil and deserialization doesn't fail if the key is missing. [] still gets coded.

packages is required. Decode will fail if the key is missing or value is nil. [] will get through and we will probably need validation to trap that case.

Copy link
Contributor

Choose a reason for hiding this comment

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

The distinction between optional and required arrays makes a lot of sense. Thanks for explaining!


/// An array of package metadata objects
public let packages: [JSONPackageCollectionModel.V1.Collection.Package]

/// The version of the format to which the collection conforms.
public let formatVersion: JSONPackageCollectionModel.FormatVersion

/// The revision number of this package collection.
public let revision: Int?

/// The ISO 8601-formatted datetime string when the package collection was generated.
public let generatedAt: Date

/// The author of this package collection.
public let generatedBy: Author?

/// Creates a `Collection`
public init(
name: String,
overview: String? = nil,
keywords: [String]? = nil,
packages: [JSONPackageCollectionModel.V1.Collection.Package],
formatVersion: JSONPackageCollectionModel.FormatVersion,
revision: Int? = nil,
generatedAt: Date = Date(),
generatedBy: Author? = nil
) {
precondition(formatVersion == .v1_0, "Unsupported format version: \(formatVersion)")

self.name = name
self.overview = overview
self.keywords = keywords
self.packages = packages
self.formatVersion = formatVersion
self.revision = revision
self.generatedAt = generatedAt
self.generatedBy = generatedBy
}

public struct Author: Equatable, Codable {
/// The author name.
public let name: String

/// Creates an `Author`
public init(name: String) {
self.name = name
}
}
}
}

extension JSONPackageCollectionModel.V1.Collection {
public struct Package: Equatable, Codable {
/// The URL of the package. Currently only Git repository URLs are supported.
public let url: Foundation.URL

/// A description of the package.
public let summary: String?

/// An array of keywords that the package is associated with.
public let keywords: [String]?

/// An array of version objects representing the most recent and/or relevant releases of the package.
public let versions: [JSONPackageCollectionModel.V1.Collection.Package.Version]

/// The URL of the package's README.
public let readmeURL: Foundation.URL?

/// Creates a `Package`
public init(
url: URL,
summary: String? = nil,
keywords: [String]? = nil,
versions: [JSONPackageCollectionModel.V1.Collection.Package.Version],
readmeURL: URL? = nil
) {
self.url = url
self.summary = summary
self.keywords = keywords
self.versions = versions
self.readmeURL = readmeURL
}
}
}

extension JSONPackageCollectionModel.V1.Collection.Package {
public struct Version: Equatable, Codable {
/// The semantic version string.
public let version: String

/// The name of the package.
public let packageName: String

/// An array of the package version's targets.
public let targets: [JSONPackageCollectionModel.V1.Target]

/// An array of the package version's products.
public let products: [JSONPackageCollectionModel.V1.Product]

/// The tools (semantic) version specified in `Package.swift`.
public let toolsVersion: String

/// An array of the package version’s supported platforms specified in `Package.swift`.
public let minimumPlatformVersions: [JSONPackageCollectionModel.V1.PlatformVersion]?

/// An array of platforms in which the package version has been tested and verified.
public let verifiedPlatforms: [JSONPackageCollectionModel.V1.Platform]?

/// An array of Swift versions that the package version has been tested and verified for.
public let verifiedSwiftVersions: [String]?

/// The package version's license.
public let license: JSONPackageCollectionModel.V1.License?

/// Creates a `Version`
public init(
version: String,
packageName: String,
targets: [JSONPackageCollectionModel.V1.Target],
products: [JSONPackageCollectionModel.V1.Product],
toolsVersion: String,
minimumPlatformVersions: [JSONPackageCollectionModel.V1.PlatformVersion]? = nil,
verifiedPlatforms: [JSONPackageCollectionModel.V1.Platform]? = nil,
verifiedSwiftVersions: [String]? = nil,
license: JSONPackageCollectionModel.V1.License? = nil
) {
self.version = version
self.packageName = packageName
self.targets = targets
self.products = products
self.toolsVersion = toolsVersion
self.minimumPlatformVersions = minimumPlatformVersions
self.verifiedPlatforms = verifiedPlatforms
self.verifiedSwiftVersions = verifiedSwiftVersions
self.license = license
}
}
}

extension JSONPackageCollectionModel.V1 {
public struct Target: Equatable, Codable {
/// The target name.
public let name: String

/// The module name if this target can be imported as a module.
public let moduleName: String?

/// Creates a `Target`
public init(name: String, moduleName: String? = nil) {
self.name = name
self.moduleName = moduleName
}
}

public struct Product: Equatable, Codable {
/// The product name.
public let name: String

/// The product type.
public let type: ProductType

/// An array of the product’s targets.
public let targets: [String]

/// Creates a `Product`
public init(
name: String,
type: ProductType,
targets: [String]
) {
self.name = name
self.type = type
self.targets = targets
}
}

public struct PlatformVersion: Equatable, Codable {
/// The name of the platform (e.g., macOS, Linux, etc.).
public let name: String

/// The semantic version of the platform.
public let version: String

/// Creates a `PlatformVersion`
public init(name: String, version: String) {
self.name = name
self.version = version
}
}

public struct Platform: Equatable, Codable {
/// The name of the platform (e.g., macOS, Linux, etc.).
public let name: String

/// Creates a `Platform`
public init(name: String) {
self.name = name
}
}

public struct License: Equatable, Codable {
/// License name (e.g., Apache-2.0, MIT, etc.)
public let name: String

/// The URL of the license file.
public let url: URL

/// Creates a `License`
public init(name: String, url: URL) {
self.name = name
self.url = url
}
}
}
18 changes: 18 additions & 0 deletions Sources/PackageCollections/JSONModel/JSONCollection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
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
*/

public enum JSONPackageCollectionModel {}

extension JSONPackageCollectionModel {
/// Representation of `PackageCollection` JSON schema version
public enum FormatVersion: String, Codable {
case v1_0 = "1.0"
}
}
Loading