diff --git a/Package.resolved b/Package.resolved index d2b976b..c0de976 100644 --- a/Package.resolved +++ b/Package.resolved @@ -28,6 +28,15 @@ "version": "1.0.0" } }, + { + "package": "Sampleable", + "repositoryURL": "https://github.com/mattpolzin/Sampleable.git", + "state": { + "branch": null, + "revision": "6572998ac30685d8bb73e6b725596d07d891a75a", + "version": "1.0.0" + } + }, { "package": "SwiftCheck", "repositoryURL": "https://github.com/typelift/SwiftCheck.git", diff --git a/Package.swift b/Package.swift index 9359a14..8ea47ab 100644 --- a/Package.swift +++ b/Package.swift @@ -21,7 +21,8 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/mattpolzin/Poly.git", from: "1.0.0"), - .package(url: "https://github.com/Flight-School/AnyCodable.git", from: "0.1.0"), + .package(url: "https://github.com/mattpolzin/Sampleable.git", from: "1.0.0"), + .package(url: "https://github.com/Flight-School/AnyCodable.git", from: "0.1.0"), .package(url: "https://github.com/typelift/SwiftCheck.git", from: "0.11.0") ], targets: [ @@ -36,7 +37,7 @@ let package = Package( dependencies: ["JSONAPI", "SwiftCheck"]), .target( name: "JSONAPIOpenAPI", - dependencies: ["JSONAPI", "AnyCodable", "JSONAPIArbitrary"]), + dependencies: ["JSONAPI", "AnyCodable", "JSONAPIArbitrary", "Sampleable"]), .testTarget( name: "JSONAPITests", dependencies: ["JSONAPI", "JSONAPITesting"]), diff --git a/Sources/JSONAPIOpenAPI/JSONAPI/JSONAPITypes+OpenAPI.swift b/Sources/JSONAPIOpenAPI/JSONAPI/JSONAPITypes+OpenAPI.swift index 7bdd58c..cdd75c0 100644 --- a/Sources/JSONAPIOpenAPI/JSONAPI/JSONAPITypes+OpenAPI.swift +++ b/Sources/JSONAPIOpenAPI/JSONAPI/JSONAPITypes+OpenAPI.swift @@ -8,6 +8,7 @@ import JSONAPI import Foundation import AnyCodable +import Sampleable private protocol _Optional {} extension Optional: _Optional {} diff --git a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift index 9e37ad1..1485026 100644 --- a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift +++ b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift @@ -8,6 +8,7 @@ import AnyCodable import Foundation import Poly +import Sampleable // MARK: Node (i.e. schema) Protocols diff --git a/Sources/JSONAPIOpenAPI/Sampleable/Include+Sampleable.swift b/Sources/JSONAPIOpenAPI/Sampleable/Include+Sampleable.swift index 895e561..750958a 100644 --- a/Sources/JSONAPIOpenAPI/Sampleable/Include+Sampleable.swift +++ b/Sources/JSONAPIOpenAPI/Sampleable/Include+Sampleable.swift @@ -6,6 +6,7 @@ // import JSONAPI +import Sampleable extension Includes: Sampleable where I: Sampleable { public static var sample: Includes { diff --git a/Sources/JSONAPIOpenAPI/Sampleable/JSONAPI+Sampleable.swift b/Sources/JSONAPIOpenAPI/Sampleable/JSONAPI+Sampleable.swift index bcb832b..d0d24b7 100644 --- a/Sources/JSONAPIOpenAPI/Sampleable/JSONAPI+Sampleable.swift +++ b/Sources/JSONAPIOpenAPI/Sampleable/JSONAPI+Sampleable.swift @@ -6,6 +6,7 @@ // import JSONAPI +import Sampleable extension NoAttributes: Sampleable { public static var sample: NoAttributes { diff --git a/Sources/JSONAPIOpenAPI/Sampleable/Sampleable+OpenAPI.swift b/Sources/JSONAPIOpenAPI/Sampleable/Sampleable+OpenAPI.swift index 070a6db..ee69d29 100644 --- a/Sources/JSONAPIOpenAPI/Sampleable/Sampleable+OpenAPI.swift +++ b/Sources/JSONAPIOpenAPI/Sampleable/Sampleable+OpenAPI.swift @@ -7,6 +7,7 @@ import Foundation import AnyCodable +import Sampleable public typealias SampleableOpenAPIType = Sampleable & GenericOpenAPINodeType diff --git a/Sources/JSONAPIOpenAPI/Sampleable/Sampleable.swift b/Sources/JSONAPIOpenAPI/Sampleable/Sampleable.swift deleted file mode 100644 index 8923032..0000000 --- a/Sources/JSONAPIOpenAPI/Sampleable/Sampleable.swift +++ /dev/null @@ -1,108 +0,0 @@ -// -// Sampleable.swift -// JSONAPIOpenAPI -// -// Created by Mathew Polzin on 1/15/19. -// - -import Foundation - -/// A Sampleable type can provide a sample value. -/// This is useful for reflection. -public protocol Sampleable { - /// Get a sample value of type Self. This can be the - /// same value every time, or it can be an arbitrarily random - /// value each time. - static var sample: Self { get } - - /// Get an example of success, if that is meaningful and - /// available. If not, will be nil. - /// - /// (optional) - /// - /// The default implementation returns `nil`. - static var successSample: Self? { get } - - /// Get an example of failure, if that is meaningful and - /// available. If not, will be nil. - /// - /// (optional) - /// - /// The default implementation returns `nil`. - static var failureSample: Self? { get } - - /// An array of samples of this Type. - /// - /// (optional) - /// - /// The default implementation returns - /// an array with just the result of - /// `Self.sample` in it. - static var samples: [Self] { get } -} - -public extension Sampleable { - // default implementation: - public static var successSample: Self? { return nil } - - // default implementation: - public static var failureSample: Self? { return nil } - - // default implementation: - public static var samples: [Self] { return [Self.sample] } -} - -extension Sampleable { - public static func samples(using s1: S1.Type, with constructor: (S1) -> Self) -> [Self] { - return S1.samples.map(constructor) - } - - public static func samples(using s1: S1.Type, _ s2: S2.Type, with constructor: (S1, S2) -> Self) -> [Self] { - return zip(S1.samples, S2.samples).map(constructor) - } - - public static func samples(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, with constructor: (S1, S2, S3) -> Self) -> [Self] { - return zip3(S1.samples, S2.samples, S3.samples).map(constructor) - } - - public static func samples(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, with constructor: (S1, S2, S3, S4) -> Self) -> [Self] { - return zip4(S1.samples, S2.samples, S3.samples, S4.samples).map(constructor) - } - - public static func samples(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, with constructor: (S1, S2, S3, S4, S5) -> Self) -> [Self] { - return zip5(S1.samples, S2.samples, S3.samples, S4.samples, S5.samples).map(constructor) - } - - public static func samples(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, _ s6: S6.Type, with constructor: (S1, S2, S3, S4, S5, S6) -> Self) -> [Self] { - // the compiler craps out at zip6. breaking it down makes the difference. - let firstZip = zip3(S1.samples, S2.samples, S3.samples) - let secondZip = zip3(S4.samples, S5.samples, S6.samples) - return zip(firstZip, secondZip).map { arg in (arg.0.0, arg.0.1, arg.0.2, arg.1.0, arg.1.1, arg.1.2) }.map(constructor) - } - - public static func samples(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, _ s6: S6.Type, _ s7: S7.Type, with constructor: (S1, S2, S3, S4, S5, S6, S7) -> Self) -> [Self] { - // the compiler craps out at zip6. breaking it down makes the difference. - let firstZip = zip3(S1.samples, S2.samples, S3.samples) - let secondZip = zip4(S4.samples, S5.samples, S6.samples, S7.samples) - return zip(firstZip, secondZip).map { arg in (arg.0.0, arg.0.1, arg.0.2, arg.1.0, arg.1.1, arg.1.2, arg.1.3) }.map(constructor) - } - - public static func samples(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, _ s6: S6.Type, _ s7: S7.Type, _ s8: S8.Type, with constructor: (S1, S2, S3, S4, S5, S6, S7, S8) -> Self) -> [Self] { - // the compiler craps out at zip6. breaking it down makes the difference. - let firstZip = zip4(S1.samples, S2.samples, S3.samples, S4.samples) - let secondZip = zip4(S5.samples, S6.samples, S7.samples, S8.samples) - return zip(firstZip, secondZip).map { arg in (arg.0.0, arg.0.1, arg.0.2, arg.0.3, arg.1.0, arg.1.1, arg.1.2, arg.1.3) }.map(constructor) - } - - @inlinable static func zip3(_ a: A, _ b: B, _ c: C) -> [(A.Element, B.Element, C.Element)] { - return zip(a, zip(b, c)).map { arg in (arg.0, arg.1.0, arg.1.1) } - } - - @inlinable static func zip4(_ a: A, _ b: B, _ c: C, _ d: D) -> [(A.Element, B.Element, C.Element, D.Element)] { - return zip(a, zip(b, zip(c, d))).map { arg in (arg.0, arg.1.0, arg.1.1.0, arg.1.1.1) } - } - - @inlinable static func zip5(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E) -> [(A.Element, B.Element, C.Element, D.Element, E.Element)] { - return zip(a, zip(b, zip(c, zip(d, e)))).map { arg in (arg.0, arg.1.0, arg.1.1.0, arg.1.1.1.0, arg.1.1.1.1) } - } -} diff --git a/Tests/JSONAPIOpenAPITests/JSONAPIDocumentOpenAPITests.swift b/Tests/JSONAPIOpenAPITests/JSONAPIDocumentOpenAPITests.swift index f961fc5..c2dad8c 100644 --- a/Tests/JSONAPIOpenAPITests/JSONAPIDocumentOpenAPITests.swift +++ b/Tests/JSONAPIOpenAPITests/JSONAPIDocumentOpenAPITests.swift @@ -9,6 +9,7 @@ import XCTest import SwiftCheck import JSONAPI import JSONAPIOpenAPI +import Sampleable class JSONAPIDocumentOpenAPITests: XCTestCase { func test_SingleResourceDocument() { diff --git a/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift b/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift index 2321c02..dde8cbb 100644 --- a/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift +++ b/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift @@ -9,6 +9,7 @@ import XCTest import JSONAPI import JSONAPIOpenAPI import AnyCodable +import Sampleable class JSONAPIEntityOpenAPITests: XCTestCase { func test_EmptyEntity() {