-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from justeattakeaway/decoding-improvements
Improve Decodable implementation
- Loading branch information
Showing
5 changed files
with
101 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Spec+Context.swift | ||
|
||
import Foundation | ||
|
||
extension Spec.Product { | ||
func makeContext() -> [String: Any] { | ||
[ | ||
"name": name, | ||
"productType": productType.rawValue, | ||
"targets": targets | ||
] | ||
} | ||
} | ||
|
||
extension Spec.RemoteDependency { | ||
func makeContext() -> [String: Any] { | ||
var retVal = [ | ||
"name": name, | ||
"url": url | ||
] | ||
if let ref { | ||
switch ref { | ||
case .branch(let value): | ||
retVal["branch"] = value | ||
case .revision(let value): | ||
retVal["revision"] = value | ||
case .version(let value): | ||
retVal["version"] = value | ||
} | ||
} | ||
return retVal.compactMapValues { $0 } | ||
} | ||
} | ||
|
||
extension Spec { | ||
func makeContext() -> [String: Any] { | ||
let values: [String: Any?] = [ | ||
"package_name": name, | ||
"platforms": platforms, | ||
"local_dependencies": localDependencies, | ||
"remote_dependencies": remoteDependencies.map { $0.makeContext() }, | ||
"products": products.map { $0.makeContext() }, | ||
"targets": targets, | ||
"local_binary_targets": localBinaryTargets, | ||
"remote_binary_targets": remoteBinaryTargets, | ||
"swift_tools_version": swiftToolsVersion, | ||
"swift_versions": swiftLanguageVersions | ||
] | ||
return values.compactMapValues { $0 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Ref.swift | ||
|
||
import Foundation | ||
|
||
enum Ref: Decodable { | ||
case version(String) | ||
case revision(String) | ||
case branch(String) | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case version | ||
case revision | ||
case branch | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
if let version = try container.decodeIfPresent(String.self, forKey: .version) { | ||
self = .version(version) | ||
} else if let revision = try container.decodeIfPresent(String.self, forKey: .revision) { | ||
self = .revision(revision) | ||
} else if let branch = try container.decodeIfPresent(String.self, forKey: .branch) { | ||
self = .branch(branch) | ||
} else { | ||
throw DecodingError.dataCorruptedError(forKey: CodingKeys.version, in: container, debugDescription: "Expected one of version, revision, or branch to be present.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters