Add access to computed attributes that are not AttributeType

This commit is contained in:
Mathew Polzin
2018-12-07 21:19:08 -08:00
parent 41a2a01788
commit 3047e2d23a
2 changed files with 21 additions and 0 deletions
+11
View File
@@ -407,8 +407,19 @@ public extension EntityProxy {
/// allows you to write `entity[\.propertyName]` instead
/// of `entity.relationships.propertyName`.
subscript<T, TFRM: Transformer, U>(_ path: KeyPath<Description.Attributes, TransformedAttribute<T, TFRM>?>) -> U? where TFRM.To == 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
/// allows you to write `entity[\.propertyName]` instead
/// of `entity.relationships.propertyName`.
subscript<T>(_ path: KeyPath<Description.Attributes, T>) -> T {
// Implementation Note: Handles attributes that are not
// AttributeType. These should only exist as computed properties.
return attributes[keyPath: path]
}
}
// MARK: Relationship Access
@@ -29,6 +29,12 @@ class ComputedPropertiesTests: XCTestCase {
XCTAssertEqual(entity[\.computed], "Sarah2")
}
func test_ComputedNonAttributeAccess() {
let entity = decoded(type: TestType.self, data: computed_property_attribute)
XCTAssertEqual(entity[\.computed2], "Sarah2")
}
func test_ComputedRelationshipAccess() {
let entity = decoded(type: TestType.self, data: computed_property_attribute)
@@ -46,6 +52,10 @@ extension ComputedPropertiesTests {
public var computed: Attribute<String> {
return name.map { $0 + "2" }
}
public var computed2: String {
return computed.value
}
}
public struct Relationships: JSONAPI.Relationships {