Round off CustomStringConvertible implementations to make print output a lot easier to read.

This commit is contained in:
Mathew Polzin
2018-11-20 07:27:28 -08:00
parent b3a4591a5d
commit 7a3a1b8b65
3 changed files with 102 additions and 6 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ The primary goals of this framework are:
- [x] Support transforms on `Attributes` values (e.g. to support different representations of `Date`)
- [x] Support ability to distinguish between `Attributes` fields that are optional (i.e. the key might not be there) and `Attributes` values that are optional (i.e. the key is guaranteed to be there but it might be `null`).
- [x] Fix `ToOneRelationship` so that it is possible to specify an optional relationship where the value is `null` rather than the key being omitted.
- [ ] Conform to `CustomStringConvertible`
- [x] Conform to `CustomStringConvertible`
- [ ] More tests around failing to decode improperly structured JSON (not bad JSON, but JSON that is not to spec)
- [ ] Use `KeyPath` to specify `Includes` thus creating type safety around the relationship between a primary resource type and the types of included resources????
- [x] For `NoIncludes`, do not even loop over the "included" JSON API section if it exists.
+97 -1
View File
@@ -64,7 +64,7 @@ extension Includes where I == NoIncludes {
}
}
// MARK: - Decoding
// MARK: - Generic Decoding
func decode<Entity: JSONAPI.EntityType>(_ type: Entity.Type, from container: SingleValueDecodingContainer) throws -> Result<Entity, EncodingError> {
let ret: Result<Entity, EncodingError>
@@ -138,6 +138,17 @@ extension Includes where I: _Include1 {
}
}
extension Include1: CustomStringConvertible {
public var description: String {
let str: String
switch self {
case .a(let a):
str = String(describing: a)
}
return "Include(\(str))"
}
}
// MARK: - 2 includes
public protocol _Include2: _Include1 {
associatedtype B: EntityType
@@ -203,6 +214,19 @@ extension Includes where I: _Include2 {
}
}
extension Include2: CustomStringConvertible {
public var description: String {
let str: String
switch self {
case .a(let a):
str = String(describing: a)
case .b(let b):
str = String(describing: b)
}
return "Include(\(str))"
}
}
// MARK: - 3 includes
public protocol _Include3: _Include2 {
associatedtype C: EntityType
@@ -281,6 +305,21 @@ extension Includes where I: _Include3 {
}
}
extension Include3: CustomStringConvertible {
public var description: String {
let str: String
switch self {
case .a(let a):
str = String(describing: a)
case .b(let b):
str = String(describing: b)
case .c(let c):
str = String(describing: c)
}
return "Include(\(str))"
}
}
// MARK: - 4 includes
public protocol _Include4: _Include3 {
associatedtype D: EntityType
@@ -372,6 +411,23 @@ extension Includes where I: _Include4 {
}
}
extension Include4: CustomStringConvertible {
public var description: String {
let str: String
switch self {
case .a(let a):
str = String(describing: a)
case .b(let b):
str = String(describing: b)
case .c(let c):
str = String(describing: c)
case .d(let d):
str = String(describing: d)
}
return "Include(\(str))"
}
}
// MARK: - 5 includes
public protocol _Include5: _Include4 {
associatedtype E: EntityType
@@ -476,6 +532,25 @@ extension Includes where I: _Include5 {
}
}
extension Include5: CustomStringConvertible {
public var description: String {
let str: String
switch self {
case .a(let a):
str = String(describing: a)
case .b(let b):
str = String(describing: b)
case .c(let c):
str = String(describing: c)
case .d(let d):
str = String(describing: d)
case .e(let e):
str = String(describing: e)
}
return "Include(\(str))"
}
}
// MARK: - 6 includes
public protocol _Include6: _Include5 {
associatedtype F: EntityType
@@ -592,3 +667,24 @@ extension Includes where I: _Include6 {
return values.compactMap { $0.f }
}
}
extension Include6: CustomStringConvertible {
public var description: String {
let str: String
switch self {
case .a(let a):
str = String(describing: a)
case .b(let b):
str = String(describing: b)
case .c(let c):
str = String(describing: c)
case .d(let d):
str = String(describing: d)
case .e(let e):
str = String(describing: e)
case .f(let f):
str = String(describing: f)
}
return "Include(\(str))"
}
}
+4 -4
View File
@@ -174,10 +174,10 @@ extension ToManyRelationship {
}
// MARK: CustomStringDescribable
public extension ToOneRelationship {
var description: String { return "Relationship(\(String(describing: id)))" }
extension ToOneRelationship: CustomStringConvertible {
public var description: String { return "Relationship(\(String(describing: id)))" }
}
public extension ToManyRelationship {
var description: String { return "Relationship(\(String(describing: ids)))" }
extension ToManyRelationship: CustomStringConvertible {
public var description: String { return "Relationship(\(String(describing: ids)))" }
}