Skip to content

Commit b0563bc

Browse files
authored
feat: added properties dependencies support to CodedBy macro (#107)
1 parent 1043da0 commit b0563bc

24 files changed

+2298
-401
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Package.resolved
6767
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
6868
#
6969
Pods/
70+
Podfile.lock
7071
#
7172
# Add this line if you want to avoid checking in source code from the Xcode workspace
7273
*.xcworkspace

Examples/Podfile.lock

Lines changed: 0 additions & 164 deletions
This file was deleted.

Package@swift-5.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let package = Package(
1919
.plugin(name: "MetaProtocolCodable", targets: ["MetaProtocolCodable"]),
2020
],
2121
dependencies: [
22-
.package(url: "https://github.com/apple/swift-syntax.git", "509.1.0"..<"601.0.0"),
22+
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"601.0.0"),
2323
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
2424
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
2525
.package(url: "https://github.com/swiftlang/swift-format", from: "600.0.0"),

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Supercharge `Swift`'s `Codable` implementations with macros.
2323
- Allows to create composition of multiple `Codable` types with ``CodedAt(_:)`` passing no arguments.
2424
- Allows to read data from additional fallback `CodingKey`s provided with ``CodedAs(_:_:)``.
2525
- Allows to provide default value in case of decoding failures with ``Default(_:)``, or only in case of failures when missing value with ``Default(ifMissing:)``. Different default values can also be used for value missing and other errors respectively with ``Default(ifMissing:forErrors:)``.
26-
- Allows to create custom decoding/encoding strategies with ``HelperCoder`` and using them with ``CodedBy(_:)``. i.e. ``LossySequenceCoder`` etc.
26+
- Allows to create custom decoding/encoding strategies with ``HelperCoder`` and using them with ``CodedBy(_:)``, ``CodedBy(_:properties:)`` or others. i.e. ``LossySequenceCoder`` etc.
2727
- Allows specifying different case values with ``CodedAs(_:_:)`` and case value/protocol type identifier type different from `String` with ``CodedAs()``.
2828
- Allows specifying enum-case/protocol type identifier path with ``CodedAt(_:)`` and case content path with ``ContentAt(_:_:)``.
2929
- Allows decoding/encoding enums that lack distinct identifiers for each case data with ``UnTagged()``.

Sources/HelperCoders/SequenceCoder/SequenceCoder.swift

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ where
8686
)
8787
}
8888

89-
/// Create a array decoder and encoder based on provided data.
89+
/// Create an array decoder and encoder based on provided data.
9090
///
9191
/// By default, no additional customizations configuration is used.
9292
///
@@ -163,3 +163,75 @@ where
163163
}
164164
}
165165
}
166+
167+
extension SequenceCoder {
168+
/// Create a sequence decoder and encoder based on provided data.
169+
///
170+
/// - Parameters:
171+
/// - output: The resulting sequence type.
172+
/// - elementHelperCreation: The `HelperCoder` creation function.
173+
/// - configuration: The configuration for decoding and encoding.
174+
/// - properties: Values that can be passed to creation function.
175+
public init<each Property>(
176+
output: Sequence.Type,
177+
elementHelperCreation: (repeat each Property) -> ElementHelper,
178+
configuration: Configuration,
179+
properties: repeat each Property
180+
) {
181+
self.init(
182+
output: output,
183+
elementHelper: elementHelperCreation(repeat each properties),
184+
configuration: configuration
185+
)
186+
}
187+
188+
/// Create an array decoder and encoder based on provided data.
189+
///
190+
/// - Parameters:
191+
/// - elementHelperCreation: The `HelperCoder` creation function.
192+
/// - configuration: The configuration for decoding and encoding.
193+
/// - properties: Values that can be passed to creation function.
194+
public init<each Property>(
195+
elementHelperCreation: (repeat each Property) -> ElementHelper,
196+
configuration: Configuration,
197+
properties: repeat each Property
198+
) where Sequence == Array<ElementHelper.Coded> {
199+
#if swift(>=5.10)
200+
self.init(
201+
output: Sequence.self, elementHelperCreation: elementHelperCreation,
202+
configuration: configuration, properties: repeat each properties
203+
)
204+
#else
205+
self.init(
206+
output: Sequence.self,
207+
elementHelper: elementHelperCreation(repeat each properties),
208+
configuration: configuration
209+
)
210+
#endif
211+
}
212+
213+
/// Create an array decoder and encoder based on provided data.
214+
///
215+
/// By default, no additional customizations configuration is used.
216+
///
217+
/// - Parameters:
218+
/// - elementHelperCreation: The `HelperCoder` creation function.
219+
/// - properties: Values that can be passed to creation function.
220+
public init<each Property>(
221+
elementHelperCreation: (repeat each Property) -> ElementHelper,
222+
properties: repeat each Property
223+
) where Sequence == Array<ElementHelper.Coded> {
224+
#if swift(>=5.10)
225+
self.init(
226+
elementHelperCreation: elementHelperCreation,
227+
configuration: .init(), properties: repeat each properties
228+
)
229+
#else
230+
self.init(
231+
output: Sequence.self,
232+
elementHelper: elementHelperCreation(repeat each properties),
233+
configuration: .init()
234+
)
235+
#endif
236+
}
237+
}

0 commit comments

Comments
 (0)