Skip to content

refactor serialization and state management #3696

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

Merged
merged 4 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/Basics/ConcurrencyHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class ThreadSafeKeyValueStore<Key, Value> where Key: Hashable {
}
}

@discardableResult
public func removeValue(forKey key: Key) -> Value? {
self.lock.withLock {
self.underlying.removeValue(forKey: key)
Expand Down
9 changes: 9 additions & 0 deletions Sources/Basics/JSON+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import class Foundation.DateFormatter
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
import TSCBasic

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
extension DateFormatter {
Expand Down Expand Up @@ -112,3 +113,11 @@ extension JSONEncoder {
return encoder
}
}

extension JSONDecoder {
public func decode<T: Decodable>(path: AbsolutePath, fileSystem: FileSystem, `as` kind: T.Type) throws -> T {
try fileSystem.readFileContents(path).withData { data in
try self.decode(kind, from: data)
}
}
}
49 changes: 0 additions & 49 deletions Sources/PackageGraph/CheckoutState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,52 +71,3 @@ extension CheckoutState: CustomStringConvertible {
}
}
}

// MARK: - JSON

extension CheckoutState: JSONMappable, JSONSerializable {
public init(json: JSON) throws {
let revision: Revision = try json.get("revision")
let version: Version? = json.get("version")
let branch: String? = json.get("branch")

switch (version, branch) {
case (.none, .none):
self = .revision(revision)
case (.some(let version), .none):
self = .version(version, revision: revision)
case (.none, .some(let branch)):
self = .branch(name: branch, revision: revision)
case (.some(_), .some(_)):
preconditionFailure("Can't set both branch and version.")
}
}

public func toJSON() -> JSON {
let revision: Revision
let version: Version?
let branch: String?

switch self {
case .revision(let _revision):
revision = _revision
version = nil
branch = nil
case .version(let _version, let _revision):
revision = _revision
version = _version
branch = nil
case .branch(let _branch, let _revision):
revision = _revision
version = nil
branch = _branch
}

return .init([
"revision": revision.identifier,
"version": version.toJSON(),
"branch": branch.toJSON(),
])
}
}

Loading