From 1ed34b00dc519dd2c838ee3431f3ec193ee5f89c Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Sun, 20 Jan 2019 16:10:31 -0800 Subject: [PATCH] Add todos for Entity Id and type. Add example usage of OpenAPI entity schema support in Playground --- .../Contents.swift | 13 +++++++++ .../Sources/OpenAPISupport.swift | 16 +++++++++++ JSONAPI.playground/contents.xcplayground | 3 +++ .../JSONAPIOpenAPI/JSONAPIOpenAPITypes.swift | 1 + Sources/JSONAPIOpenAPI/Sampleable.swift | 27 +------------------ 5 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 JSONAPI.playground/Pages/OpenAPI Documentation.xcplaygroundpage/Contents.swift create mode 100644 JSONAPI.playground/Sources/OpenAPISupport.swift diff --git a/JSONAPI.playground/Pages/OpenAPI Documentation.xcplaygroundpage/Contents.swift b/JSONAPI.playground/Pages/OpenAPI Documentation.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..c1b6cc9 --- /dev/null +++ b/JSONAPI.playground/Pages/OpenAPI Documentation.xcplaygroundpage/Contents.swift @@ -0,0 +1,13 @@ +//: [Previous](@previous) + +import Foundation +import JSONAPI +import JSONAPIOpenAPI + +// print Entity Schema +let encoder = JSONEncoder() +encoder.outputFormatting = .prettyPrinted + +let personSchemaData = try? encoder.encode(Person.openAPINode()) + +print(personSchemaData.map { String(data: $0, encoding: .utf8)! } ?? "Schema Construction Failed") diff --git a/JSONAPI.playground/Sources/OpenAPISupport.swift b/JSONAPI.playground/Sources/OpenAPISupport.swift new file mode 100644 index 0000000..5b7e895 --- /dev/null +++ b/JSONAPI.playground/Sources/OpenAPISupport.swift @@ -0,0 +1,16 @@ +import Foundation +import JSONAPI +import JSONAPITesting // for the convenience of literal initialization +import JSONAPIOpenAPI + +extension PersonDescription.Attributes: Sampleable { + public static var sample: PersonDescription.Attributes { + return .init(name: ["Abbie", "Eibba"], favoriteColor: "Blue") + } +} + +extension PersonDescription.Relationships: Sampleable { + public static var sample: PersonDescription.Relationships { + return .init(friends: ["1", "2"], dogs: ["2"], home: "1") + } +} diff --git a/JSONAPI.playground/contents.xcplayground b/JSONAPI.playground/contents.xcplayground index 8df8fa3..a1ec109 100644 --- a/JSONAPI.playground/contents.xcplayground +++ b/JSONAPI.playground/contents.xcplayground @@ -3,5 +3,8 @@ + + + \ No newline at end of file diff --git a/Sources/JSONAPIOpenAPI/JSONAPIOpenAPITypes.swift b/Sources/JSONAPIOpenAPI/JSONAPIOpenAPITypes.swift index 5b2e24c..01702b6 100644 --- a/Sources/JSONAPIOpenAPI/JSONAPIOpenAPITypes.swift +++ b/Sources/JSONAPIOpenAPI/JSONAPIOpenAPITypes.swift @@ -104,6 +104,7 @@ extension ToManyRelationship: OpenAPINodeType { extension Entity: OpenAPINodeType where Description.Attributes: Sampleable, Description.Relationships: Sampleable { public static func openAPINode() throws -> JSONNode { + // TODO: Id, type, metadata, links let attributesNode: JSONNode? = Description.Attributes.self == NoAttributes.self ? nil : try Description.Attributes.genericObjectOpenAPINode() diff --git a/Sources/JSONAPIOpenAPI/Sampleable.swift b/Sources/JSONAPIOpenAPI/Sampleable.swift index 437ccfb..c7fa194 100644 --- a/Sources/JSONAPIOpenAPI/Sampleable.swift +++ b/Sources/JSONAPIOpenAPI/Sampleable.swift @@ -49,32 +49,7 @@ extension Sampleable { let newNode: JSONNode? if let allCases = maybeAllCases, let openAPINode = maybeOpenAPINode { - newNode = try openAPINode.with(allowedValues: allCases) // try { -// if let cases = allCases as? [JSONTypeFormat.BooleanFormat.SwiftType] { -// return try openAPINode.with(allowedValues: cases) -// -// } else if let cases = allCases as? [JSONTypeFormat.ArrayFormat.SwiftType] { -// return try openAPINode.with(allowedValues: cases) -// -// } else if let cases = allCases as? [JSONTypeFormat.ObjectFormat.SwiftType] { -// return try openAPINode.with(allowedValues: cases) -// -// } else if let cases = allCases as? [JSONTypeFormat.NumberFormat.SwiftType] { -// return try openAPINode.with(allowedValues: cases) -// -// } else if let cases = allCases as? [JSONTypeFormat.IntegerFormat.SwiftType] { -// return try openAPINode.with(allowedValues: cases) -// -// } else if let cases = allCases as? [JSONTypeFormat.StringFormat.SwiftType] { -// return try openAPINode.with(allowedValues: cases) -// -// } else if allCases.compactMap({ $0 as? RawStringRepresentable }).count == allCases.count { -// return try openAPINode.with(allowedValues: allCases.compactMap { ($0 as? RawStringRepresentable)?.rawValue }) -// -// } else { -// throw SampleableError.allowedValuesNotOfExpectedType(forNode: openAPINode, allowedValues: allCases) -// } -// }() + newNode = try openAPINode.with(allowedValues: allCases) } else { newNode = maybeOpenAPINode }