Skip to content

Commit 2b729a7

Browse files
committed
func json(atKeyPath keyPath: String) -> String?
1 parent f6b5932 commit 2b729a7

1 file changed

Lines changed: 30 additions & 55 deletions

File tree

Sources/ProjectSpec/SwiftPackage.swift

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -105,66 +105,41 @@ extension SwiftPackage: JSONEncodable {
105105
extension SwiftPackage.VersionRequirement: JSONUtilities.JSONObjectConvertible {
106106

107107
public init(jsonDictionary: JSONDictionary) throws {
108-
if jsonDictionary["exactVersion"] != nil {
109-
do {
110-
let exactVersion: String = try jsonDictionary.json(atKeyPath: "exactVersion")
111-
self = .exact(exactVersion)
112-
} catch {
113-
let exactVersion: Double = try jsonDictionary.json(atKeyPath: "exactVersion")
114-
self = .exact(String(exactVersion))
115-
}
116-
} else if jsonDictionary["version"] != nil {
117-
do {
118-
let version: String = try jsonDictionary.json(atKeyPath: "version")
119-
self = .exact(version)
120-
} catch {
121-
let version: Double = try jsonDictionary.json(atKeyPath: "version")
122-
self = .exact(String(version))
123-
}
124-
} else if jsonDictionary["revision"] != nil {
125-
do {
126-
let revision: String = try jsonDictionary.json(atKeyPath: "revision")
127-
self = .exact(revision)
128-
} catch {
129-
let revision: Double = try jsonDictionary.json(atKeyPath: "revision")
130-
self = .exact(String(revision))
131-
}
132-
} else if jsonDictionary["branch"] != nil {
133-
do {
134-
let branch: String = try jsonDictionary.json(atKeyPath: "branch")
135-
self = .exact(branch)
136-
} catch {
137-
let branch: Double = try jsonDictionary.json(atKeyPath: "branch")
138-
self = .exact(String(branch))
108+
func json(atKeyPath keyPath: String) -> String? {
109+
if jsonDictionary[keyPath] != nil {
110+
do {
111+
let value: String = try jsonDictionary.json(atKeyPath: .init(rawValue: keyPath))
112+
return value
113+
} catch {
114+
do {
115+
let value: Double = try jsonDictionary.json(atKeyPath: .init(rawValue: keyPath))
116+
return String(value)
117+
} catch {
118+
return nil
119+
}
120+
}
139121
}
122+
return nil
123+
}
124+
125+
if let exactVersion = json(atKeyPath: "exactVersion") {
126+
self = .exact(exactVersion)
127+
} else if let version = json(atKeyPath: "version") {
128+
self = .exact(version)
129+
} else if let revision = json(atKeyPath: "revision") {
130+
self = .revision(revision)
131+
} else if let branch = json(atKeyPath: "branch") {
132+
self = .branch(branch)
140133
} else if jsonDictionary["minVersion"] != nil && jsonDictionary["maxVersion"] != nil {
141134
let minimum: String = try jsonDictionary.json(atKeyPath: "minVersion")
142135
let maximum: String = try jsonDictionary.json(atKeyPath: "maxVersion")
143136
self = .range(from: minimum, to: maximum)
144-
} else if jsonDictionary["minorVersion"] != nil {
145-
do {
146-
let minorVersion: String = try jsonDictionary.json(atKeyPath: "minorVersion")
147-
self = .exact(minorVersion)
148-
} catch {
149-
let minorVersion: Double = try jsonDictionary.json(atKeyPath: "minorVersion")
150-
self = .exact(String(minorVersion))
151-
}
152-
} else if jsonDictionary["majorVersion"] != nil {
153-
do {
154-
let majorVersion: String = try jsonDictionary.json(atKeyPath: "majorVersion")
155-
self = .exact(majorVersion)
156-
} catch {
157-
let majorVersion: Double = try jsonDictionary.json(atKeyPath: "majorVersion")
158-
self = .exact(String(majorVersion))
159-
}
160-
} else if jsonDictionary["from"] != nil {
161-
do {
162-
let from: String = try jsonDictionary.json(atKeyPath: "from")
163-
self = .exact(from)
164-
} catch {
165-
let from: Double = try jsonDictionary.json(atKeyPath: "from")
166-
self = .exact(String(from))
167-
}
137+
} else if let minorVersion = json(atKeyPath: "minorVersion") {
138+
self = .upToNextMinorVersion(minorVersion)
139+
} else if let majorVersion = json(atKeyPath: "majorVersion") {
140+
self = .upToNextMajorVersion(majorVersion)
141+
} else if let from = json(atKeyPath: "from") {
142+
self = .upToNextMajorVersion(from)
168143
} else {
169144
throw SpecParsingError.unknownPackageRequirement(jsonDictionary)
170145
}

0 commit comments

Comments
 (0)