Skip to content

PropertyListEncoder: Reference the property on the class itself instead of the options struct #919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Sources/FoundationEssentials/PropertyList/PlistEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ open class PropertyListEncoder {
/// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
/// - throws: An error if any value throws an error during encoding.
open func encode<Value : Encodable>(_ value: Value) throws -> Data {
let format = self.outputFormat
do {
switch options.outputFormat {
switch format {
case .binary:
return try _encodeBPlist(value)
case .xml:
Expand All @@ -105,7 +106,7 @@ open class PropertyListEncoder {
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Property list format .openStep not supported for writing"])
#if FOUNDATION_FRAMEWORK
@unknown default:
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(options.outputFormat)"])
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(format)"])
#endif
}
} catch {
Expand Down Expand Up @@ -166,8 +167,9 @@ open class PropertyListEncoder {

@available(FoundationPreview 0.1, *)
open func encode<T : EncodableWithConfiguration>(_ value: T, configuration: T.EncodingConfiguration) throws -> Data {
let format = self.outputFormat
do {
switch options.outputFormat {
switch format {
case .binary:
return try _encodeBPlist(value, configuration: configuration)
case .xml:
Expand All @@ -176,7 +178,7 @@ open class PropertyListEncoder {
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Property list format .openStep not supported for writing"])
#if FOUNDATION_FRAMEWORK
@unknown default:
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(options.outputFormat)"])
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(format)"])
#endif
}
} catch {
Expand Down
17 changes: 17 additions & 0 deletions Tests/FoundationEssentialsTests/PropertyListEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,15 @@ data1 = <7465
_testRoundTrip(of: input, in: .xml)
}
}

func testCustomSubclass() throws {
// verify we consult the subclass for the output format
let encodeMe = ["hello":"world"]
let encoder = XMLOnlyEncoder()
let data = try encoder.encode(encodeMe)
let dataAsStr = String(data: data, encoding: .utf8)!
XCTAssertTrue(dataAsStr.hasPrefix("<?xml"))
}
}


Expand Down Expand Up @@ -2036,3 +2045,11 @@ private struct MultipleDecodeOptionsTestType : Codable, Equatable {
}
}

// MARK: - Helper Class

class XMLOnlyEncoder : PropertyListEncoder, @unchecked Sendable {
override var outputFormat: PropertyListDecoder.PropertyListFormat {
get { return .xml }
set { }
}
}