-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Decodable implementation #7
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() }, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor:
Suggested change
tot follow a style that you have |
||||||
"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 } | ||||||
} | ||||||
} |
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.") | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,21 +8,14 @@ struct Spec: Decodable { | |
|
||
struct Product: Decodable { | ||
let name: String | ||
let productType: String | ||
let productType: ProductType | ||
let targets: [String] | ||
|
||
enum CodingKeys: CodingKey { | ||
case name | ||
case productType | ||
case targets | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.name = try container.decode(String.self, forKey: .name) | ||
self.productType = try container.decode(ProductType.self, forKey: .productType).rawValue | ||
self.targets = try container.decode([String].self, forKey: .targets) | ||
} | ||
} | ||
|
||
enum ProductType: String, Decodable { | ||
|
@@ -39,37 +32,24 @@ struct Spec: Decodable { | |
struct RemoteDependency: Decodable { | ||
let name: String | ||
let url: String? | ||
let version: String? | ||
let revision: String? | ||
let branch: String? | ||
let ref: Ref? | ||
|
||
enum CodingKeys: CodingKey { | ||
case name | ||
case url | ||
case version | ||
case revision | ||
case branch | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.name = try container.decode(String.self, forKey: .name) | ||
self.url = try container.decodeIfPresent(String.self, forKey: .url) | ||
self.version = try container.decodeIfPresent(String.self, forKey: .version) | ||
self.revision = try container.decodeIfPresent(String.self, forKey: .revision) | ||
self.branch = try container.decodeIfPresent(String.self, forKey: .branch) | ||
self.ref = (try? Ref(from: decoder)) ?? nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed why 🤣 |
||
} | ||
|
||
init(name: String, url: String, version: String?, revision: String?, branch: String?) { | ||
guard version != nil || revision != nil || branch != nil else { | ||
fatalError("You need to provide at least one of the following: version, revision or branch") | ||
} | ||
|
||
init(name: String, url: String, ref: Ref) { | ||
self.name = name | ||
self.url = url | ||
self.version = version | ||
self.revision = revision | ||
self.branch = branch | ||
self.ref = ref | ||
} | ||
} | ||
|
||
|
@@ -181,21 +161,3 @@ struct Spec: Decodable { | |
self.swiftLanguageVersions = swiftLanguageVersions | ||
} | ||
} | ||
|
||
extension Spec { | ||
func makeContext() -> [String: Any] { | ||
let values: [String: Any?] = [ | ||
"package_name": name, | ||
"platforms": platforms, | ||
"local_dependencies": localDependencies, | ||
"remote_dependencies": remoteDependencies, | ||
"products": products, | ||
"targets": targets, | ||
"local_binary_targets": localBinaryTargets, | ||
"remote_binary_targets": remoteBinaryTargets, | ||
"swift_tools_version": swiftToolsVersion, | ||
"swift_versions": swiftLanguageVersions | ||
] | ||
return values.compactMapValues { $0 } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the keys in the rest of the file use snake casing, I left this one to match the name of the property to avoid breaking backward compatibility with the Stencil file provided as part of the current major version.