Skip to content

Commit 47fe878

Browse files
committed
refactor serialization and state management
motivation: stronger seperation between state managment format and data model, allowing evolution of storeage format changes: * refactor PinsStore, RepositoryManager, and WorkspaceState to model their state storage seperately from the data model * use Codable instead of JSONMappable/JSONSerializable/SimplePersistence * adjust call-sites and tests where necessary
1 parent 40f9df2 commit 47fe878

23 files changed

+1062
-875
lines changed

Package.resolved.lock

Whitespace-only changes.

Sources/Basics/JSON+Extensions.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import class Foundation.DateFormatter
1212
import class Foundation.JSONDecoder
1313
import class Foundation.JSONEncoder
14+
import TSCBasic
1415

1516
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1617
extension DateFormatter {
@@ -112,3 +113,11 @@ extension JSONEncoder {
112113
return encoder
113114
}
114115
}
116+
117+
extension JSONDecoder {
118+
public func decode<T: Decodable>(path: AbsolutePath, fileSystem: FileSystem, `as` kind: T.Type) throws -> T {
119+
try fileSystem.readFileContents(path).withData { data in
120+
try self.decode(kind, from: data)
121+
}
122+
}
123+
}

Sources/PackageGraph/CheckoutState.swift

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -71,52 +71,3 @@ extension CheckoutState: CustomStringConvertible {
7171
}
7272
}
7373
}
74-
75-
// MARK: - JSON
76-
77-
extension CheckoutState: JSONMappable, JSONSerializable {
78-
public init(json: JSON) throws {
79-
let revision: Revision = try json.get("revision")
80-
let version: Version? = json.get("version")
81-
let branch: String? = json.get("branch")
82-
83-
switch (version, branch) {
84-
case (.none, .none):
85-
self = .revision(revision)
86-
case (.some(let version), .none):
87-
self = .version(version, revision: revision)
88-
case (.none, .some(let branch)):
89-
self = .branch(name: branch, revision: revision)
90-
case (.some(_), .some(_)):
91-
preconditionFailure("Can't set both branch and version.")
92-
}
93-
}
94-
95-
public func toJSON() -> JSON {
96-
let revision: Revision
97-
let version: Version?
98-
let branch: String?
99-
100-
switch self {
101-
case .revision(let _revision):
102-
revision = _revision
103-
version = nil
104-
branch = nil
105-
case .version(let _version, let _revision):
106-
revision = _revision
107-
version = _version
108-
branch = nil
109-
case .branch(let _branch, let _revision):
110-
revision = _revision
111-
version = nil
112-
branch = _branch
113-
}
114-
115-
return .init([
116-
"revision": revision.identifier,
117-
"version": version.toJSON(),
118-
"branch": branch.toJSON(),
119-
])
120-
}
121-
}
122-

0 commit comments

Comments
 (0)