Skip to content

Commit

Permalink
feat: added properties dependencies support to CodedBy macro (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyamahunt authored Oct 9, 2024
1 parent 1043da0 commit b0563bc
Show file tree
Hide file tree
Showing 24 changed files with 2,298 additions and 401 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Package.resolved
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
Pods/
Podfile.lock
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
*.xcworkspace
Expand Down
164 changes: 0 additions & 164 deletions Examples/Podfile.lock

This file was deleted.

2 changes: 1 addition & 1 deletion Package@swift-5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
.plugin(name: "MetaProtocolCodable", targets: ["MetaProtocolCodable"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", "509.1.0"..<"601.0.0"),
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"601.0.0"),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
.package(url: "https://github.com/swiftlang/swift-format", from: "600.0.0"),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Supercharge `Swift`'s `Codable` implementations with macros.
- Allows to create composition of multiple `Codable` types with ``CodedAt(_:)`` passing no arguments.
- Allows to read data from additional fallback `CodingKey`s provided with ``CodedAs(_:_:)``.
- 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:)``.
- Allows to create custom decoding/encoding strategies with ``HelperCoder`` and using them with ``CodedBy(_:)``. i.e. ``LossySequenceCoder`` etc.
- Allows to create custom decoding/encoding strategies with ``HelperCoder`` and using them with ``CodedBy(_:)``, ``CodedBy(_:properties:)`` or others. i.e. ``LossySequenceCoder`` etc.
- Allows specifying different case values with ``CodedAs(_:_:)`` and case value/protocol type identifier type different from `String` with ``CodedAs()``.
- Allows specifying enum-case/protocol type identifier path with ``CodedAt(_:)`` and case content path with ``ContentAt(_:_:)``.
- Allows decoding/encoding enums that lack distinct identifiers for each case data with ``UnTagged()``.
Expand Down
74 changes: 73 additions & 1 deletion Sources/HelperCoders/SequenceCoder/SequenceCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
)
}

/// Create a array decoder and encoder based on provided data.
/// Create an array decoder and encoder based on provided data.
///
/// By default, no additional customizations configuration is used.
///
Expand Down Expand Up @@ -163,3 +163,75 @@ where
}
}
}

extension SequenceCoder {
/// Create a sequence decoder and encoder based on provided data.
///
/// - Parameters:
/// - output: The resulting sequence type.
/// - elementHelperCreation: The `HelperCoder` creation function.
/// - configuration: The configuration for decoding and encoding.
/// - properties: Values that can be passed to creation function.
public init<each Property>(
output: Sequence.Type,
elementHelperCreation: (repeat each Property) -> ElementHelper,
configuration: Configuration,
properties: repeat each Property
) {
self.init(
output: output,
elementHelper: elementHelperCreation(repeat each properties),
configuration: configuration
)
}

/// Create an array decoder and encoder based on provided data.
///
/// - Parameters:
/// - elementHelperCreation: The `HelperCoder` creation function.
/// - configuration: The configuration for decoding and encoding.
/// - properties: Values that can be passed to creation function.
public init<each Property>(
elementHelperCreation: (repeat each Property) -> ElementHelper,
configuration: Configuration,
properties: repeat each Property
) where Sequence == Array<ElementHelper.Coded> {
#if swift(>=5.10)
self.init(
output: Sequence.self, elementHelperCreation: elementHelperCreation,
configuration: configuration, properties: repeat each properties
)
#else
self.init(
output: Sequence.self,
elementHelper: elementHelperCreation(repeat each properties),
configuration: configuration
)
#endif
}

/// Create an array decoder and encoder based on provided data.
///
/// By default, no additional customizations configuration is used.
///
/// - Parameters:
/// - elementHelperCreation: The `HelperCoder` creation function.
/// - properties: Values that can be passed to creation function.
public init<each Property>(
elementHelperCreation: (repeat each Property) -> ElementHelper,
properties: repeat each Property
) where Sequence == Array<ElementHelper.Coded> {
#if swift(>=5.10)
self.init(
elementHelperCreation: elementHelperCreation,
configuration: .init(), properties: repeat each properties
)
#else
self.init(
output: Sequence.self,
elementHelper: elementHelperCreation(repeat each properties),
configuration: .init()
)
#endif
}
}
Loading

0 comments on commit b0563bc

Please sign in to comment.