From ea4a8b1da93e1fa0040527cefa67af85ed1e8a0e Mon Sep 17 00:00:00 2001 From: Tony Parker Date: Tue, 17 Sep 2024 12:52:34 -0700 Subject: [PATCH] PropertyListEncoder: Reference the property on the class itself instead of the options struct --- .../PropertyList/PlistEncoder.swift | 10 ++++++---- .../PropertyListEncoderTests.swift | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Sources/FoundationEssentials/PropertyList/PlistEncoder.swift b/Sources/FoundationEssentials/PropertyList/PlistEncoder.swift index 620035add..49b0ad0b6 100644 --- a/Sources/FoundationEssentials/PropertyList/PlistEncoder.swift +++ b/Sources/FoundationEssentials/PropertyList/PlistEncoder.swift @@ -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: Value) throws -> Data { + let format = self.outputFormat do { - switch options.outputFormat { + switch format { case .binary: return try _encodeBPlist(value) case .xml: @@ -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 { @@ -166,8 +167,9 @@ open class PropertyListEncoder { @available(FoundationPreview 0.1, *) open func encode(_ 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: @@ -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 { diff --git a/Tests/FoundationEssentialsTests/PropertyListEncoderTests.swift b/Tests/FoundationEssentialsTests/PropertyListEncoderTests.swift index 98b4c68c1..063f6b769 100644 --- a/Tests/FoundationEssentialsTests/PropertyListEncoderTests.swift +++ b/Tests/FoundationEssentialsTests/PropertyListEncoderTests.swift @@ -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("