From 6b6f40c968fbf336b83f9e46ef41c0fc4771ac92 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Thu, 18 Apr 2019 22:40:12 -0700 Subject: [PATCH] breaking change: change spelling of Entity subscript accessor that returns values of properties within Attributes without digging into the AttributeType. This change will make it possible for the compiler to unambiguously determine the type of subscript access being made before the value is stored, cast, or compared. --- Sources/JSONAPI/Resource/Entity.swift | 15 +++++++++------ .../ComputedPropertiesTests.swift | 4 ++-- Tests/JSONAPITests/Poly/PolyProxyTests.swift | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Sources/JSONAPI/Resource/Entity.swift b/Sources/JSONAPI/Resource/Entity.swift index f596612..b3f56ca 100644 --- a/Sources/JSONAPI/Resource/Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -440,31 +440,34 @@ public extension Entity where EntityRawIdType: CreatableRawIdType { public extension EntityProxy { /// Access the attribute at the given keypath. This just /// allows you to write `entity[\.propertyName]` instead - /// of `entity.relationships.propertyName`. + /// of `entity.attributes.propertyName`. subscript(_ path: KeyPath) -> T.ValueType { return attributes[keyPath: path].value } /// Access the attribute at the given keypath. This just /// allows you to write `entity[\.propertyName]` instead - /// of `entity.relationships.propertyName`. + /// of `entity.attributes.propertyName`. subscript(_ path: KeyPath) -> T.ValueType? { return attributes[keyPath: path]?.value } /// Access the attribute at the given keypath. This just /// allows you to write `entity[\.propertyName]` instead - /// of `entity.relationships.propertyName`. + /// of `entity.attributes.propertyName`. subscript(_ path: KeyPath) -> U? where T.ValueType == U? { // Implementation Note: Handles Transform that returns optional // type. return attributes[keyPath: path].flatMap { $0.value } } - /// Access the computed attribute at the given keypath. This just + /// Access the storage of the attribute at the given keypath. This just /// allows you to write `entity[\.propertyName]` instead - /// of `entity.relationships.propertyName`. - subscript(_ path: KeyPath) -> T { + /// of `entity.attributes.propertyName`. + /// Most of the subscripts dig into an `AttributeType`. This subscript + /// returns the `AttributeType` (or another type, if you are accessing + /// an attribute that is not stored in an `AttributeType`). + subscript(direct path: KeyPath) -> T { // Implementation Note: Handles attributes that are not // AttributeType. These should only exist as computed properties. return attributes[keyPath: path] diff --git a/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift index aaf626d..13aaa97 100644 --- a/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift +++ b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift @@ -27,13 +27,13 @@ class ComputedPropertiesTests: XCTestCase { let entity = decoded(type: TestType.self, data: computed_property_attribute) XCTAssertEqual(entity[\.computed], "Sarah2") - XCTAssertEqual(entity[\.secretsOut], "shhhh") + XCTAssertEqual(entity[direct: \.secretsOut], "shhhh") } func test_ComputedNonAttributeAccess() { let entity = decoded(type: TestType.self, data: computed_property_attribute) - XCTAssertEqual(entity[\.computed2], "Sarah2") + XCTAssertEqual(entity[direct: \.computed2], "Sarah2") } func test_ComputedRelationshipAccess() { diff --git a/Tests/JSONAPITests/Poly/PolyProxyTests.swift b/Tests/JSONAPITests/Poly/PolyProxyTests.swift index a294f4e..2a20f1c 100644 --- a/Tests/JSONAPITests/Poly/PolyProxyTests.swift +++ b/Tests/JSONAPITests/Poly/PolyProxyTests.swift @@ -24,7 +24,7 @@ public class PolyProxyTests: XCTestCase { XCTAssertEqual(polyUserA[\.name], "Ken Moore") XCTAssertEqual(polyUserA.id, "1") XCTAssertEqual(polyUserA.relationships, .none) - XCTAssertEqual(polyUserA[\.x], .init(x: "y")) + XCTAssertEqual(polyUserA[direct: \.x], .init(x: "y")) } func test_UserAAndBEncodeEquality() { @@ -59,7 +59,7 @@ public class PolyProxyTests: XCTestCase { XCTAssertEqual(polyUserB[\.name], "Ken Less") XCTAssertEqual(polyUserB.id, "2") XCTAssertEqual(polyUserB.relationships, .none) - XCTAssertEqual(polyUserB[\.x], .init(x: "y")) + XCTAssertEqual(polyUserB[direct: \.x], .init(x: "y")) } }