Skip to content

Commit 5120866

Browse files
Flipped migration arguments to make writing them more natural
Fixed #177
1 parent 7a11780 commit 5120866

File tree

5 files changed

+48
-96
lines changed

5 files changed

+48
-96
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ struct BookStore: DatastoreFormat {
110110
// version, and convert it to the modern type you use in the app.
111111
// Conversions are typically only done at read time.
112112
migrations: [
113-
.v1: { data, decoder in try Book(decoder.decode(BookV1.self, from: data)) },
114-
.current: { data, decoder in try decoder.decode(Book.self, from: data) }
113+
.v1: { try Book($0.decode(BookV1.self, from: $1)) },
114+
.current: { try $0.decode(Book.self, from: $1) }
115115
]
116116
)
117117
}

Sources/CodableDatastore/Datastore/Datastore.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ extension Datastore where AccessMode == ReadWrite {
14421442
version: Version = Format.currentVersion,
14431443
encoder: JSONEncoder = JSONEncoder(),
14441444
decoder: JSONDecoder = JSONDecoder(),
1445-
migrations: [Version : @Sendable (_ data: Data, _ decoder: JSONDecoder) async throws -> (id: IdentifierType, instance: InstanceType)],
1445+
migrations: [Version : @Sendable (_ decoder: JSONDecoder, _ data: Data) async throws -> (id: IdentifierType, instance: InstanceType)],
14461446
configuration: Configuration = .init()
14471447
) -> Self {
14481448
self.init(
@@ -1451,7 +1451,7 @@ extension Datastore where AccessMode == ReadWrite {
14511451
version: version,
14521452
encoder: { try encoder.encode($0) },
14531453
decoders: migrations.mapValues { migration in
1454-
{ @Sendable data in try await migration(data, decoder) }
1454+
{ @Sendable data in try await migration(decoder, data) }
14551455
},
14561456
configuration: configuration
14571457
)
@@ -1463,7 +1463,7 @@ extension Datastore where AccessMode == ReadWrite {
14631463
key: DatastoreKey = Format.defaultKey,
14641464
version: Version = Format.currentVersion,
14651465
outputFormat: PropertyListSerialization.PropertyListFormat = .binary,
1466-
migrations: [Version : @Sendable (_ data: Data, _ decoder: PropertyListDecoder) async throws -> (id: IdentifierType, instance: InstanceType)],
1466+
migrations: [Version : @Sendable (_ decoder: PropertyListDecoder, _ data: Data) async throws -> (id: IdentifierType, instance: InstanceType)],
14671467
configuration: Configuration = .init()
14681468
) -> Self {
14691469
let encoder = PropertyListEncoder()
@@ -1477,7 +1477,7 @@ extension Datastore where AccessMode == ReadWrite {
14771477
version: version,
14781478
encoder: { try encoder.encode($0) },
14791479
decoders: migrations.mapValues { migration in
1480-
{ @Sendable data in try await migration(data, decoder) }
1480+
{ @Sendable data in try await migration(decoder, data) }
14811481
},
14821482
configuration: configuration
14831483
)
@@ -1491,15 +1491,15 @@ extension Datastore where AccessMode == ReadOnly {
14911491
key: DatastoreKey = Format.defaultKey,
14921492
version: Version = Format.currentVersion,
14931493
decoder: JSONDecoder = JSONDecoder(),
1494-
migrations: [Version : @Sendable (_ data: Data, _ decoder: JSONDecoder) async throws -> (id: IdentifierType, instance: InstanceType)],
1494+
migrations: [Version : @Sendable (_ decoder: JSONDecoder, _ data: Data) async throws -> (id: IdentifierType, instance: InstanceType)],
14951495
configuration: Configuration = .init()
14961496
) -> Self {
14971497
self.init(
14981498
persistence: persistence,
14991499
key: key,
15001500
version: version,
15011501
decoders: migrations.mapValues { migration in
1502-
{ @Sendable data in try await migration(data, decoder) }
1502+
{ @Sendable data in try await migration(decoder, data) }
15031503
},
15041504
configuration: configuration
15051505
)
@@ -1510,7 +1510,7 @@ extension Datastore where AccessMode == ReadOnly {
15101510
format: Format.Type = Format.self,
15111511
key: DatastoreKey = Format.defaultKey,
15121512
version: Version = Format.currentVersion,
1513-
migrations: [Version : @Sendable (_ data: Data, _ decoder: PropertyListDecoder) async throws -> (id: IdentifierType, instance: InstanceType)],
1513+
migrations: [Version : @Sendable (_ decoder: PropertyListDecoder, _ data: Data) async throws -> (id: IdentifierType, instance: InstanceType)],
15141514
configuration: Configuration = .init()
15151515
) -> Self {
15161516
let decoder = PropertyListDecoder()
@@ -1520,7 +1520,7 @@ extension Datastore where AccessMode == ReadOnly {
15201520
key: key,
15211521
version: version,
15221522
decoders: migrations.mapValues { migration in
1523-
{ @Sendable data in try await migration(data, decoder) }
1523+
{ @Sendable data in try await migration(decoder, data) }
15241524
},
15251525
configuration: configuration
15261526
)
@@ -1561,7 +1561,7 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
15611561
version: Version = Format.currentVersion,
15621562
encoder: JSONEncoder = JSONEncoder(),
15631563
decoder: JSONDecoder = JSONDecoder(),
1564-
migrations: [Version : @Sendable (_ data: Data, _ decoder: JSONDecoder) async throws -> InstanceType],
1564+
migrations: [Version : @Sendable (_ decoder: JSONDecoder, _ data: Data) async throws -> InstanceType],
15651565
configuration: Configuration = .init()
15661566
) -> Self {
15671567
self.JSONStore(
@@ -1571,8 +1571,8 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
15711571
encoder: encoder,
15721572
decoder: decoder,
15731573
migrations: migrations.mapValues { migration in
1574-
{ @Sendable data, decoder in
1575-
let instance = try await migration(data, decoder)
1574+
{ @Sendable decoder, data in
1575+
let instance = try await migration(decoder, data)
15761576
return (id: instance.id, instance: instance)
15771577
}
15781578
},
@@ -1586,7 +1586,7 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
15861586
key: DatastoreKey = Format.defaultKey,
15871587
version: Version = Format.currentVersion,
15881588
outputFormat: PropertyListSerialization.PropertyListFormat = .binary,
1589-
migrations: [Version : @Sendable (_ data: Data, _ decoder: PropertyListDecoder) async throws -> InstanceType],
1589+
migrations: [Version : @Sendable (_ decoder: PropertyListDecoder, _ data: Data) async throws -> InstanceType],
15901590
configuration: Configuration = .init()
15911591
) -> Self {
15921592
self.propertyListStore(
@@ -1595,8 +1595,8 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
15951595
version: version,
15961596
outputFormat: outputFormat,
15971597
migrations: migrations.mapValues { migration in
1598-
{ @Sendable data, decoder in
1599-
let instance = try await migration(data, decoder)
1598+
{ @Sendable decoder, data in
1599+
let instance = try await migration(decoder, data)
16001600
return (id: instance.id, instance: instance)
16011601
}
16021602
},
@@ -1634,7 +1634,7 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
16341634
key: DatastoreKey = Format.defaultKey,
16351635
version: Version = Format.currentVersion,
16361636
decoder: JSONDecoder = JSONDecoder(),
1637-
migrations: [Version : @Sendable (_ data: Data, _ decoder: JSONDecoder) async throws -> InstanceType],
1637+
migrations: [Version : @Sendable (_ decoder: JSONDecoder, _ data: Data) async throws -> InstanceType],
16381638
configuration: Configuration = .init()
16391639
) -> Self {
16401640
self.readOnlyJSONStore(
@@ -1643,8 +1643,8 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
16431643
version: version,
16441644
decoder: decoder,
16451645
migrations: migrations.mapValues { migration in
1646-
{ @Sendable data, decoder in
1647-
let instance = try await migration(data, decoder)
1646+
{ @Sendable decoder, data in
1647+
let instance = try await migration(decoder, data)
16481648
return (id: instance.id, instance: instance)
16491649
}
16501650
},
@@ -1657,16 +1657,16 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
16571657
format: Format.Type = Format.self,
16581658
key: DatastoreKey = Format.defaultKey,
16591659
version: Version = Format.currentVersion,
1660-
migrations: [Version : @Sendable (_ data: Data, _ decoder: PropertyListDecoder) async throws -> InstanceType],
1660+
migrations: [Version : @Sendable (_ decoder: PropertyListDecoder, _ data: Data) async throws -> InstanceType],
16611661
configuration: Configuration = .init()
16621662
) -> Self {
16631663
self.readOnlyPropertyListStore(
16641664
persistence: persistence,
16651665
key: key,
16661666
version: version,
16671667
migrations: migrations.mapValues { migration in
1668-
{ @Sendable data, decoder in
1669-
let instance = try await migration(data, decoder)
1668+
{ @Sendable decoder, data in
1669+
let instance = try await migration(decoder, data)
16701670
return (id: instance.id, instance: instance)
16711671
}
16721672
},

Sources/CodableDatastore/Datastore/DatastoreFormat.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ import Foundation
4545
/// .JSONStore(
4646
/// persistence: persistence,
4747
/// migrations: [
48-
/// .v1: { data, decoder in try Book(decoder.decode(BookV1.self, from: data)) },
49-
/// .current: { data, decoder in try decoder.decode(Book.self, from: data) }
48+
/// .v1: { try Book($0.decode(BookV1.self, from: $1)) },
49+
/// .current: { try $0.decode(Book.self, from: $1) }
5050
/// ]
5151
/// )
5252
/// }

0 commit comments

Comments
 (0)