From d667e91a6a6cd4af019a04123e67b84982e34792 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Mon, 3 Dec 2018 19:08:09 -0800 Subject: [PATCH] Add some missing initializers for Entity and fix Playground sources. --- JSONAPI.playground/Sources/Entities.swift | 6 +++--- Sources/JSONAPI/Resource/Entity.swift | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/JSONAPI.playground/Sources/Entities.swift b/JSONAPI.playground/Sources/Entities.swift index 484073b..6df63c9 100644 --- a/JSONAPI.playground/Sources/Entities.swift +++ b/JSONAPI.playground/Sources/Entities.swift @@ -24,7 +24,7 @@ extension String: CreatableRawIdType { } // MARK: - Entity typealias for convenience -public typealias ExampleEntity = Entity +public typealias ExampleEntity = Entity // MARK: - A few resource objects (entities) public enum PersonDescription: EntityDescription { @@ -60,7 +60,7 @@ public enum PersonDescription: EntityDescription { public typealias Person = ExampleEntity -public extension Entity where Description == PersonDescription, EntityRawIdType == String { +public extension Entity where Description == PersonDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String { public init(id: Person.Id? = nil,name: [String], favoriteColor: String, friends: [Person], dogs: [Dog], home: House) throws { self = try Person(id: id ?? Person.Id(), attributes: .init(name: .init(rawValue: name), favoriteColor: .init(rawValue: favoriteColor)), relationships: .init(friends: .init(entities: friends), dogs: .init(entities: dogs), home: .init(entity: home))) } @@ -89,7 +89,7 @@ public enum DogDescription: EntityDescription { public typealias Dog = ExampleEntity -public extension Entity where Description == DogDescription, EntityRawIdType == String { +public extension Entity where Description == DogDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String { public init(name: String, owner: Person?) throws { self = try Dog(attributes: .init(name: .init(rawValue: name)), relationships: DogDescription.Relationships(owner: .init(entity: owner))) } diff --git a/Sources/JSONAPI/Resource/Entity.swift b/Sources/JSONAPI/Resource/Entity.swift index 6fe7a03..59ceb30 100644 --- a/Sources/JSONAPI/Resource/Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -320,18 +320,36 @@ extension Entity where MetaType == NoMetadata { } } +extension Entity where MetaType == NoMetadata, EntityRawIdType: CreatableRawIdType { + public init(attributes: Description.Attributes, relationships: Description.Relationships, links: LinksType) { + self.init(attributes: attributes, relationships: relationships, meta: .none, links: links) + } +} + extension Entity where LinksType == NoLinks { public init(id: Entity.Id, attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType) { self.init(id: id, attributes: attributes, relationships: relationships, meta: meta, links: .none) } } +extension Entity where LinksType == NoLinks, EntityRawIdType: CreatableRawIdType { + public init(attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType) { + self.init(attributes: attributes, relationships: relationships, meta: meta, links: .none) + } +} + extension Entity where MetaType == NoMetadata, LinksType == NoLinks { public init(id: Entity.Id, attributes: Description.Attributes, relationships: Description.Relationships) { self.init(id: id, attributes: attributes, relationships: relationships, meta: .none, links: .none) } } +extension Entity where MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType { + public init(attributes: Description.Attributes, relationships: Description.Relationships) { + self.init(attributes: attributes, relationships: relationships, meta: .none, links: .none) + } +} + // MARK: Pointer for Relationships use. public extension Entity where EntityRawIdType: JSONAPI.RawIdType { /// Get a pointer to this entity that can be used as a