Skip to content

Commit

Permalink
Merge pull request #48 from mattpolzin/feature/indentation
Browse files Browse the repository at this point in the history
Feature/indentation
  • Loading branch information
mattpolzin authored Nov 6, 2019
2 parents 83233a7 + 3e96adf commit 9f42e5f
Show file tree
Hide file tree
Showing 20 changed files with 1,132 additions and 1,050 deletions.
64 changes: 32 additions & 32 deletions Sources/JSONAPI/Document/APIDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,58 @@

/// This is what the JSON API Spec calls the "JSON:API Object"
public protocol APIDescriptionType: Codable, Equatable {
associatedtype Meta
associatedtype Meta
}

/// This is what the JSON API Spec calls the "JSON:API Object"
public struct APIDescription<Meta: JSONAPI.Meta>: APIDescriptionType {
public let version: String
public let meta: Meta
public let version: String
public let meta: Meta

public init(version: String, meta: Meta) {
self.version = version
self.meta = meta
}
public init(version: String, meta: Meta) {
self.version = version
self.meta = meta
}
}

/// Can be used as `APIDescriptionType` for Documents that do not
/// have any API Description (a.k.a. "JSON:API Object").
public struct NoAPIDescription: APIDescriptionType, CustomStringConvertible {
public typealias Meta = NoMetadata
public typealias Meta = NoMetadata

public init() {}
public init() {}

public static var none: NoAPIDescription { return .init() }
public static var none: NoAPIDescription { return .init() }

public var description: String { return "No JSON:API Object" }
public var description: String { return "No JSON:API Object" }
}

extension APIDescription {
private enum CodingKeys: String, CodingKey {
case version
case meta
}
private enum CodingKeys: String, CodingKey {
case version
case meta
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

// The spec says that if a version is not specified, it should be assumed to be at least 1.0
version = (try? container.decode(String.self, forKey: .version)) ?? "1.0"
// The spec says that if a version is not specified, it should be assumed to be at least 1.0
version = (try? container.decode(String.self, forKey: .version)) ?? "1.0"

if let metaVal = NoMetadata() as? Meta {
meta = metaVal
} else {
meta = try container.decode(Meta.self, forKey: .meta)
}
}
if let metaVal = NoMetadata() as? Meta {
meta = metaVal
} else {
meta = try container.decode(Meta.self, forKey: .meta)
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(version, forKey: .version)
try container.encode(version, forKey: .version)

if Meta.self != NoMetadata.self {
try container.encode(meta, forKey: .meta)
}
}
if Meta.self != NoMetadata.self {
try container.encode(meta, forKey: .meta)
}
}
}
Loading

0 comments on commit 9f42e5f

Please sign in to comment.