// // Examples.swift // JSONAPI // // Created by Mathew Polzin on 11/12/18. // import Foundation import JSONAPI /******* Please enjoy these examples, but allow me the forced casting and the lack of error checking for the sake of brevity. ********/ // MARK: - String as CreatableRawIdType var GlobalStringId: Int = 0 extension String: CreatableRawIdType { public static func unique() -> String { GlobalStringId += 1 return String(GlobalStringId) } } // MARK: - typealiases for convenience public typealias ExampleEntity = Entity public typealias ToOne = ToOneRelationship public typealias ToMany = ToManyRelationship // MARK: - A few resource objects (entities) public enum PersonDescription: EntityDescription { public static var jsonType: String { return "people" } public struct Attributes: JSONAPI.Attributes { public let name: Attribute<[String]> public let favoriteColor: Attribute public var fullName: Attribute { return name.map { $0.joined(separator: " ") } } public init(name: Attribute<[String]>, favoriteColor: Attribute) { self.name = name self.favoriteColor = favoriteColor } } public struct Relationships: JSONAPI.Relationships { public let friends: ToMany public let dogs: ToMany public let home: ToOne public init(friends: ToMany, dogs: ToMany, home: ToOne) { self.friends = friends self.dogs = dogs self.home = home } } } public typealias Person = ExampleEntity 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 = Person(id: id ?? Person.Id(), attributes: .init(name: .init(value: name), favoriteColor: .init(value: favoriteColor)), relationships: .init(friends: .init(entities: friends), dogs: .init(entities: dogs), home: .init(entity: home)), meta: .none, links: .none) } } public enum DogDescription: EntityDescription { public static var jsonType: String { return "dogs" } public struct Attributes: JSONAPI.Attributes { public let name: Attribute public init(name: Attribute) { self.name = name } } public struct Relationships: JSONAPI.Relationships { public let owner: ToOne public init(owner: ToOne) { self.owner = owner } } } public typealias Dog = ExampleEntity public enum AlternativeDogDescription: EntityDescription { public static var jsonType: String { return "dogs" } public struct Attributes: JSONAPI.Attributes { public let name: Attribute public init(name: Attribute) { self.name = name } } public struct Relationships: JSONAPI.Relationships { public let human: ToOne public init(human: ToOne) { self.human = human } // define custom key mapping: enum CodingKeys: String, CodingKey { case human = "owner" } } } public typealias AlternativeDog = ExampleEntity public extension Entity where Description == DogDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String { public init(name: String, owner: Person?) throws { self = Dog(attributes: .init(name: .init(value: name)), relationships: DogDescription.Relationships(owner: .init(entity: owner)), meta: .none, links: .none) } public init(name: String, owner: Person.Id) throws { self = Dog(attributes: .init(name: .init(value: name)), relationships: .init(owner: .init(id: owner)), meta: .none, links: .none) } } public enum HouseDescription: EntityDescription { public static var jsonType: String { return "houses" } public typealias Attributes = NoAttributes public typealias Relationships = NoRelationships } public typealias House = ExampleEntity