Skip to content

Commit

Permalink
Lift the constraint that Attributes and Relationships are Codable for…
Browse files Browse the repository at this point in the history
… EntityProxies.
  • Loading branch information
mattpolzin committed Dec 24, 2018
1 parent 52eb123 commit fc962f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
16 changes: 10 additions & 6 deletions Sources/JSONAPI/Resource/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ public protocol JSONTyped {
static var type: String { get }
}

/// An `EntityProxyDescription` is an `EntityDescription`
/// without Codable conformance.
public protocol EntityProxyDescription: JSONTyped {
associatedtype Attributes: Equatable
associatedtype Relationships: Equatable
}

/// An `EntityDescription` describes a JSON API
/// Resource Object. The Resource Object
/// itself is encoded and decoded as an
/// `Entity`, which gets specialized on an
/// `EntityDescription`.
public protocol EntityDescription: JSONTyped {
associatedtype Attributes: JSONAPI.Attributes
associatedtype Relationships: JSONAPI.Relationships
}
public protocol EntityDescription: EntityProxyDescription where Attributes: JSONAPI.Attributes, Relationships: JSONAPI.Relationships {}

/// EntityProxy is a protocol that can be used to create
/// types that _act_ like Entities but cannot be encoded
/// or decoded as Entities.
public protocol EntityProxy: Equatable, JSONTyped {
associatedtype Description: EntityDescription
associatedtype Description: EntityProxyDescription
associatedtype EntityRawIdType: JSONAPI.MaybeRawId

typealias Id = JSONAPI.Id<EntityRawIdType, Self>
Expand Down Expand Up @@ -75,7 +79,7 @@ extension EntityProxy {
/// EntityType is the protocol that Entity conforms to. This
/// protocol lets other types accept any Entity as a generic
/// specialization.
public protocol EntityType: EntityProxy, PrimaryResource {
public protocol EntityType: EntityProxy, PrimaryResource where Description: EntityDescription {
associatedtype Meta: JSONAPI.Meta
associatedtype Links: JSONAPI.Links
}
Expand Down
15 changes: 11 additions & 4 deletions Tests/JSONAPITests/Poly/PolyProxyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PolyProxyTests: XCTestCase {
XCTAssertEqual(polyUserA[\.name], "Ken Moore")
XCTAssertEqual(polyUserA.id, "1")
XCTAssertEqual(polyUserA.relationships, .none)
XCTAssertEqual(polyUserA[\.x], .init(x: "y"))
}

func test_UserAAndBEncodeEquality() {
Expand Down Expand Up @@ -57,6 +58,7 @@ public class PolyProxyTests: XCTestCase {
XCTAssertEqual(polyUserB[\.name], "Ken Less")
XCTAssertEqual(polyUserB.id, "2")
XCTAssertEqual(polyUserB.relationships, .none)
XCTAssertEqual(polyUserB[\.x], .init(x: "y"))
}
}

Expand Down Expand Up @@ -111,24 +113,29 @@ extension Poly2: EntityProxy, JSONTyped where A == PolyProxyTests.UserA, B == Po
public var attributes: SharedUserDescription.Attributes {
switch self {
case .a(let a):
return .init(name: .init(value: "\(a[\.firstName]) \(a[\.lastName])"))
return .init(name: .init(value: "\(a[\.firstName]) \(a[\.lastName])"), x: .init(x: "y"))
case .b(let b):
return .init(name: .init(value: b[\.name].joined(separator: " ")))
return .init(name: .init(value: b[\.name].joined(separator: " ")), x: .init(x: "y"))
}
}

public var relationships: NoRelationships {
return .none
}

public enum SharedUserDescription: EntityDescription {
public enum SharedUserDescription: EntityProxyDescription {
public static var type: String { return A.type }

public struct Attributes: JSONAPI.Attributes {
public struct Attributes: Equatable {
let name: Attribute<String>
let x: SomeRandomThingThatIsNotCodable
}

public typealias Relationships = NoRelationships

public struct SomeRandomThingThatIsNotCodable: Equatable {
let x: String
}
}

public typealias Description = SharedUserDescription
Expand Down

0 comments on commit fc962f9

Please sign in to comment.