Skip to content

Commit d07019d

Browse files
committed
add decodable struct for Package.resolved version 2
1 parent c98841d commit d07019d

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

Sources/DangerDependenciesResolver/Package.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,42 @@ extension Package.Pinned {
3838
let version: Version
3939
}
4040
}
41+
42+
// MARK: swift-tools-version >= 5.6
43+
extension Package {
44+
struct PinnedV2: Decodable, Equatable {
45+
let name: String
46+
let url: URL
47+
let state: State
48+
}
49+
}
50+
51+
extension Package.PinnedV2 {
52+
enum CodingKeys: String, CodingKey {
53+
case name = "identity"
54+
case url = "location"
55+
case state
56+
}
57+
}
58+
59+
extension Package.PinnedV2 {
60+
struct State: Decodable, Equatable {
61+
let version: Version
62+
}
63+
}
64+
65+
extension Package.PinnedV2 {
66+
func convertToV1() -> Package.Pinned {
67+
.init(
68+
name: name,
69+
url: url,
70+
state: .init(version: state.version)
71+
)
72+
}
73+
}
74+
75+
extension Sequence where Element == Package.PinnedV2 {
76+
func convertToV1() -> [Package.Pinned] {
77+
map { $0.convertToV1() }
78+
}
79+
}

Sources/DangerDependenciesResolver/PackageDataProvider.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct PackageDataProvider: PackageDataProviding {
6363
/// swift-tools-version <= 5.5
6464
case v1(object: Object)
6565
/// swift-tools-version >= 5.6
66-
case v2(pins: [Package.Pinned])
66+
case v2(pins: [Package.PinnedV2])
6767
}
6868
let version: Version
6969

@@ -80,7 +80,9 @@ struct PackageDataProvider: PackageDataProviding {
8080
case 1:
8181
let object = try container.decode(Object.self, forKey: .object)
8282
self.version = .v1(object: object)
83-
// case 2:
83+
case 2:
84+
let pins = try container.decode([Package.PinnedV2].self, forKey: .pins)
85+
self.version = .v2(pins: pins)
8486
default:
8587
throw DecodingError.valueNotFound(Int.self, .init(codingPath: [CodingKeys.version], debugDescription: "Invalid value: \(version)"))
8688
}
@@ -94,7 +96,7 @@ struct PackageDataProvider: PackageDataProviding {
9496
case let .v1(object):
9597
return object.pins
9698
case let .v2(pins):
97-
return pins
99+
return pins.convertToV1()
98100
}
99101
}
100102

0 commit comments

Comments
 (0)