Skip to content

Commit

Permalink
Experimental property wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeredpath committed May 10, 2021
1 parent ca6eb5f commit f720f3b
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
91 changes: 91 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/swift-coding.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Coding"
BuildableName = "Coding"
BlueprintName = "Coding"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CodingTests"
BuildableName = "CodingTests"
BlueprintName = "CodingTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CodingTests"
BuildableName = "CodingTests"
BlueprintName = "CodingTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Coding"
BuildableName = "Coding"
BlueprintName = "Coding"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
17 changes: 17 additions & 0 deletions Sources/Coding/Encoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,20 @@ public extension Encoding {
}
}
}

// MARK: - Property Wrapper

@propertyWrapper
public struct UsesEncoding<Value>: Encodable {
public let wrappedValue: Value
public let encoding: Encoding<Value>

public init(wrappedValue: Value, _ encoding: Encoding<Value>) {
self.wrappedValue = wrappedValue
self.encoding = encoding
}

public func encode(to encoder: Encoder) throws {
try encoding.encode(wrappedValue, encoder)
}
}
41 changes: 41 additions & 0 deletions Tests/CodingTests/EncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,35 @@ final class EncodingTests: XCTestCase {
)
}

// MARK: - Encoding Property Wrapper

func testEncodingPropertyWrapper() {
struct User: Encodable {
@UsesEncoding(.lowercased)
var id: UUID = UUID()

@UsesEncoding(.lowercased)
var name: String = ""
}

encoder.outputFormatting = .prettyPrinted

let user = User(
id: UUID(uuidString: "3B0B3AFC-4C61-4251-8FC5-F046D06DC3EC")!,
name: "Joe Bloggs"
)

XCTAssertEqual(
"""
{
"id" : "3b0b3afc-4c61-4251-8fc5-f046d06dc3ec",
"name" : "joe bloggs"
}
""",
stringValue(try encoder.encode(user))
)
}

private func stringValue(_ data: Data) -> String {
String(data: data, encoding: .utf8)!
}
Expand Down Expand Up @@ -573,6 +602,18 @@ struct Manufacturer {
}
}

extension Encoding where Value == String {
static let lowercased: Self = Encoding<String>
.singleValue
.pullback { $0.lowercased() }
}

extension Encoding where Value == UUID {
static let lowercased: Self = Encoding<String>
.lowercased
.pullback { $0.uuidString }
}

extension Encoding where Value == Manufacturer {
static let name: Self = Encoding<String>
.withKey(Model.CodingKeys.name)
Expand Down

0 comments on commit f720f3b

Please sign in to comment.